5. Flow Control and Exceptions
Unreachable statements produce a compile-time error.
while (false) { x = 3; } // won't compile for (;false;) { x =3; } // won't compile if (false) {x = 3; } // will compile, to provide the ability to conditionally compile the code.
Local variables already declared in an enclosing block, therefore visible in a nested block cannot be re-declared inside the nested block.
A local variable in a block may be re-declared in another local block, if the blocks are disjoint.
Method parameters cannot be re-declared.
1. Loop constructs
- 3 constructs - for, while, do
- All loops are controlled by a boolean expression.
- In while and for, the test occurs at the top, so if the test fails at the first time, body of the loop might not be executed at all.
- In do, test occurs at the bottom, so the body is executed at least once.
- In for, we can declare multiple variables in the first part of the loop separated by commas, also we can have multiple statements in the third part separated by commas.
- In the first section of for statement, we can have a list of declaration statements or a list of expression statements, but not both. We cannot mix them.
- All expressions in the third section of for statement will always execute, even if the first expression makes the loop condition false. There is no short -circuit here.