# Chapter 9: Control I – Expression and Statements

• An expression returns a value and produces no side effect (in pure mathematical form)
• A statement is executed for its side effects and return no value.

### 9.1 Expression

• If the evaluation of an expression causes no side effects, then the expression will yield the same result, regardless of the order of evaluation of its subexpression. In the presence of side effects, the order of evaluation can make a difference.
• short-circuit evaluation evaluate left-to-right up to the point where the truth value of the entire expression becomes known and then the evaluation stops.
• referential transparency (substitution rule) is any two expressions in a program that have the same value may be substituted for each other anywhere in the program. Their values always remain equal, regardless of the context in which they are evaluated.
• normal order evaluation of an expression means that each operation (or function) begins its evaluation before its operands are evaluated, and each operand is evaluated only if it is needed for the calculation of the value of the operation.
• normal order evaluation is lazy evaluation in Haskell and pass by name in Algo160.

### 9.2 Conditional Statements and Guards

• If-Statements
• {if (e1) if (e2) S1 else S2} has a dangling-else ambiguity where the last else could be associate with either the first or second else.
• Solution: diambiguating rule or bracketing keyword
• Case- and Switch-Statements

### 9.3 Loops and Variations on WHILE

• for loop vs while loop
```for (e1; e2; e3) S;

e1;
while (e2) {
S;
e3;
}```

### 9.4 The GOTO Controversy and Loop Exits

• structured vs unstructured loop exist
```int search(int array[], int target){
boolean found = false;
int index = 0;
while (index < array.length && ! found)
if (array[index] == target)
found = true;
else
index++;
if (found)
return index;
else
return -1;
}

int search(int array[], int target){
for (int index = 0; index < array.length; index++)
if (array(index) == target)
return index;
return -1;
}```

### 9.5 Exception Handling

•  explicit control mechanism has a syntactic indication of the transfer at the point where a transfer control takes place. The opposite is implicit transfer of control where there is no indication.
• exception handling is the control of error conditions or other unusual events during the execution of a program. It involves the declaration of both exceptions and exception handlers.
• exception is any unexpected or infrequent event
• exception handler is a procedure or code sequence that is designed to be executed when a particular exception is raised and that is supposed to make it possible for normal execution to continue in some way.
• asynchronous exceptions occur at any moment and not just in response to the execution of program code.
• synchronous exceptions occur in direct response to actions by the program.
• resumption model of exception handling return to the point at which the exception was first raised and begin execution again.
• termination model of exception handling continue execution with the code immediately following the block or expression in which the handler that is executed was found.