Control Flow
# Control Flow
# Conditional Execution
Adding the if statement to grammar:
|
|
In java:
|
|
# Dangling Else Problem
The else clause is optional. Most parsers bind the else to the nearest if that precedes it.
# Logical Operators
Updated grammar:
|
|
# While loops
Updated grammar:
|
|
# For loops
Updated grammar:
|
|
The first clause is the initializer. It is executed exactly once, before anything else. It’s usually an expression, but for convenience, we also allow a variable declaration. In that case, the variable is scoped to the rest of the for loop—the other two clauses and the body.
Next is the condition. It’s evaluated once at the beginning of each iteration, including the first. If the result is truthy, it executes the loop body. Otherwise, it bails.
The last clause is the increment. It’s an arbitrary expression that does some work at the end of each loop iteration. The result of the expression is discarded, so it must have a side effect to be useful. In practice, it usually increments a variable.
Any of these clauses can be omitted. Following the closing parenthesis is a statement for the body, which is typically a block
# Desugaring
We don’t actually need the for loop. It is syntactic sugar for the primitive operations we already have. The for loop can be rewritten to:
|
|