# Ch 9: Control Flow

> Study guide to Crafting Interpreters chapter 9. Control flow: if/else, logical operators with short-circuiting, while loops, and for loops implemented by…

Updated October 20, 2026.

## Summary {#summary}

Crafting Interpreters chapter 9, Control Flow: if/else, logical operators with short-circuiting, while loops, and for loops implemented by desugaring into while. Budget about 4 hours; the book's `chap09_control` tests check your code.

Lox becomes Turing-complete.

## Key facts {#key-facts}

- Week 7 of 145 on the cracked.guide path, in [Crafting Interpreters](https://cracked.guide/crafting-interpreters).
- Time budget: about 4 hours.
- Part of the book: jlox (tree-walk interpreter, Java).
- Verified on cracked.guide when the official `chap09_control` tests pass on GitHub Actions.
- 3 challenges at the end of the chapter.

## Week 7 goals {#objectives}

By the end of week 7 you can:

- Implement if, while and the short-circuit logical operators.
- Trace a for loop's desugaring into a block and a while loop.
- Recognize how the parser binds a dangling else.

## This week's steps {#steps}

- Read the chapter: After implementing for-loop desugaring, print the AST of a for loop to see the rewritten while loop.
- Write the code in your lox repo: Type it into jlox/ (Java) yourself as you read, and get it running.
- Do the challenges (1–3): Challenge 3 (add break) is the one to implement; it is a good exercise in parser state and in using an exception or flag to unwind.
- Push and let the tests verify it: Your repo runs the book's chap09_control tests on every push; passing marks this done.

## Why it matters {#why}

Desugaring (implementing a feature by rewriting it into simpler features) is a key compiler technique. Short-circuit evaluation shows up in every language.

## How to approach it {#approach}

After implementing for-loop desugaring, print the AST of a for loop to see the rewritten while loop.

## Challenges {#challenges}

3 challenges at the end of the chapter.

Challenge 3 (add break) is the one to implement; it is a good exercise in parser state and in using an exception or flag to unwind. Challenges 1 and 2 are thought exercises. Do challenges that change Lox's behavior on a separate git branch. The cracked.guide check runs the official test suite for this chapter, and a challenge that changes semantics (new syntax, different errors) can make tests fail on main.

## Common traps {#traps}

- The dangling else: make sure else binds to the nearest if.
- Logical operators must return the operand value, not a converted boolean.

## Links {#links}

- [Read the chapter: Ch 9: Control Flow](https://craftinginterpreters.com/control-flow.html)
- [Official repo: book source, both interpreters, test suite](https://github.com/munificent/craftinginterpreters)
- [Running the official test suite against your own interpreter (repo README)](https://github.com/munificent/craftinginterpreters#testing-your-implementation)
- [Wikipedia: Syntactic sugar](https://en.wikipedia.org/wiki/Syntactic_sugar)
- [Wikipedia: Short-circuit evaluation](https://en.wikipedia.org/wiki/Short-circuit_evaluation)

---

Source: https://cracked.guide/guide/ci-09
cracked.guide: A free, dated 145-week path to getting genuinely cracked at programming.
