文档介绍:作者:jeru
email: jeru@
日期:7/3/2001 10:00:19 AM
2) Flow control and exception Handling
Objective 1)
Write code using if and switch statements and identify legal argument types for these statements.
· Unreachable statements produce pile-time error.
while (false) { x = 3; } // won’pile
for (;false;) { x =3; } // won’pile
if (false) {x = 3; } // pile, to provide the ability to pile the code.
while(1==2) {…} // fine
· A local variable in a block may be re-declared in another local block, if the blocks are disjoint.
· if takes a boolean arguments. Parenthesis required. else part is optional. else if structure provides multiple selective branching.
· switch takes an argument of byte, short, char or int.(patible to int)
· case value should be a constant expression that can be evaluated pile time.
· Compiler checks each case value against the range of the switch expression’s data type. The following code won’pile.
byte b;
switch (b) {
case 200: // 200 not in range of byte
default:
}
· We need to place a break statement in each case block to prevent the execution to fall through other case blocks. But this is not a part of switch statement and not enforced by piler.
· default case can be placed anywhere. It’ll be executed only if none of the case values match.
· switch can be nested. Nested case labels are independent, don’t clash with outer case labels.
· Empty swi