Loop and Flow Control

Basic flow control require expressions that evaluate to a boolean.
if-else Be mindful of what code block go with which if and else statement
if ( boolean ) { }
The if statement must have all conditions enclosed by at least one pair of parenthesis.
The code datatype within the bracket must evaluate to a boolean.
switch-case switch( x) {case 0 : …default : …}

x should be an integer constant (32 bits): byte, char, short, int.

Exam Tip The compiler will throw and error if the legal range of the integer type in the switch statement does not cover all the constant used in the case statement.
Exam Tip Constants of case statements can only be integer literals.They can be static and final.
Exam Tip All constants of case statements must be in the range of the integer value used.Ex: If the switch uses a byte value, the case x: constants must be between: -128 – 127.
for loop Useful when the numbers times a statement needs to be executed is known.
Three main parts:declaration and initialization expressionincrementing/decrement or modification
Exam Tip The loop variable processing is as follows:init valueexecute statementincrement variabletest variable
Exam Tip None of these section are required for an expression to execute
for (;;) {}
int I = 0;for (;I<10;) {I++;}Acts like a while loop
Exam Tip All the sections of the for loop are independent of each other.The iterator expression does not even to iterate a variable.
int a = 2;int b = 3;for (a = 1; b !=1; System.out.println(“Iterate”)) {b = b – 1;}

This will compile.

List<Integer> squares = new ArrayList<Integer>);for(Integer square; squares) System.out.println(square);
Exam Tip The variable declared in the for loop is local to the for.
for ( ; ; i++) {int i = 0;System.out.println(“I is: “ + i);}
int squares(1,2,3,r};for(int i; squares) System.out.println(“value $d”, i++); void cancel(Collection c){

for(Time

}

Exam Tip Declaration of variables used in the for statement, for(..), must be declared before or within the for statement.
break and continue Allows the program to “jump” to a different section of the program.
boolean problem = true;
break Cause a program to stop execution of the inner most loop construct or switch statement. Processing restarts at the next line of code after the block.
boolean problem = true;while(true) {if (problem) {System.out.println(“There was a problem”);break;

}

}

//next line of code

The break statement will exit out of the innermost looping construct and proceed with the next line of code. The if statement does not affect the while loop.

Exam Tip For a switch the break jumps to the next command after the switch statement.
If a break is omitted, the program keeps executing the different case block. When the break is found the switch statement ends.
continue Causes the iteration of the innermost loop to cease and the next iteration to start if the condition is met.
for (int i=0; i<10; i++) {System.out.println(“In loop”);continue;}

Continue increments i before the condition is checked again.

Unlabeled Statements while (!EOF) {//read a field from a fileif (there was a problem) {//move to the next field in the filecontinue;

}

}

A file is being read one field at a time. When an error is encountered it moves to the next field in the file and uses the continue statement to go back into the loop.

for (int i =0; i < 2; i++) {case (i)case 0: {System.out.println(“I is “ + i);break;

}

case i : {

System.out.println(“I is “ + i);

break;

}

Exam Tip The break statement does not affect the for loop, but the case statement. I is 0I is 1 is printed.

This would work for the while loop also.

Labeled Statements The names must adhere to valid naming convention of Java. Start with an alphabet, $ or _.
break A break with a label will exit out of the labeled loop as opposed to the innermost loop if the break keyword is combined with a label. Break must follow a statement.
The syntax:label: statement
continue outer: for (int i=0; i<10; i++) {for (int j=0; j<10; j++) {System.out.println(“Hello”);continue outer;

}

}

System.out.println(“Good Bye”);

Hello will be printed nine times.

After the continue statement is executed, the flow continues with the next iteration of the loop identified with the label.

Exam Tip The labeled continue must be inside the loop that has the same label name.
Exam Tip A label must accompany a loop structure. Not simply an if or switch statement
while()
do {} while Code is executed at least once.
Exam Tip int i = 1;do while( i < 1 ) system.out.println(“got it”);while (i > 1);
This strange looping structure will compile, but nothing will be printed.
Enumeration Enumerationfor ( Enumeration e = Object.elements; e.hasMoreElements()) { (Cast) e.nextElement();}