Studyon Minte9.com
Fundamentals (Docs)

Study

Control Flow



IF-THEN

It tells your program to execute a certain section of code only if a particular test evaluates to true.
	
	if (isMoving){ 
		currentSpeed--; 
	}

The opening and closing braces are optional, provided that the "then" clause contains only one statement.
	
	if (isMoving) currentSpeed--;


IF-THEN-ELSE

The if-then-else statement provides a secondary path of execution when an "if" clause evaluates to false.

	if (isMoving) {
		currentSpeed--;
	} else {
		System.err.println("The bicycle has already stopped!");
	}


SWITCH

Unlike if-then and if-then-else statements, the switch statement can have a number of possible execution paths.

	int month = 8;
        String monthString;

        switch (month) {
            case 1:  monthString = "January";       break;
            case 2:  monthString = "February";      break;
            case 3:  monthString = "March";         break;
            case 4:  monthString = "April";         break;
            case 5:  monthString = "May";           break;
            case 6:  monthString = "June";          break;
            case 7:  monthString = "July";          break;
            case 8:  monthString = "August";        break;
            case 9:  monthString = "September";     break;
            case 10: monthString = "October";       break;
            case 11: monthString = "November";      break;
            case 12: monthString = "December";      break;
            default: monthString = "Invalid month"; break;
        }
	System.out.println(monthString); // August

Each break statement terminates the enclosing switch statement. 
Control flow continues with the first statement following the switch block.
Technically, the final break is not required because flow falls out of the switch statement. 
Using a break is recommended so that modifying the code is easier and less error prone.

In Java SE 7 and later, you can use a String object in the switch statement's expression.
	
        String month = "August";
	switch (month.toLowerCase()) {
            case "january":    monthNumber =  1; break;
            case "february":   monthNumber =  2; break;
            ...
        }


WHILE

The while statement continually executes a block of statements while a particular condition is true. 

	while (expression) {
		statement(s)
	}

You can implement an infinite loop using the while statement as follows:
	
        while (true){
		// your code goes here
	}


DO-WHILE

The Java programming language also provides a do-while statement, which can be expressed as follows:

	do {
	     statement(s)
	} while (expression);

The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop
 instead of the top. Therefore, the statements within the do block are always executed at least once.


FOR

The for statement provides a compact way to iterate over a range of values.

	for(int i=1; i<11; i++){
		System.out.println("Count is: " + i);
	}

Notice how the code declares a variable within the initialization expression. 
The scope of this variable extends from its declaration to the end of the block governed by the for statement, 
so it can be used in the termination and increment expressions as well. 
If the variable that controls a for statement is not needed outside of the loop, 
it's best to declare the variable in the initialization expression.

The three expressions of the for loop are optional; an infinite loop can be created as follows:

	for ( ; ; ) {    // infinite loop
	     // your code goes here
	}

The for statement also has another form designed for iteration through Collections and arrays.

	int[] numbers = {1,2,3,4,5,6,7,8,9,10};
	for (int item : numbers) {
		System.out.println("Number is: " + item); // Number is 1, ...
	}


BREAK

The break statement has two forms: labeled and unlabeled.
	int[][] ArrayOfInts = { {1,2,3}, {4,5,6},  {5,7,8}};
        int searchFor = 5;
        boolean foundIt = false;
        int i=0;
        int j=0;   

        search: // la
        for (i=0; i<ArrayOfInts.length; i++) {
            for (j=0; j<ArrayOfInts.length; j++) {
                if (ArrayOfInts[i][j] == searchFor) {
                    foundIt = true;
                    break search; // labeled
		    //break; // unlabeled
                }
            }
        }
        
        if (foundIt) {
            System.out.println("Found " + searchFor + " at " + i + " and " + j);

	    // if break labeled: Found 5 at 1 and 1
	    // if break unlabeled: Found 5 at 3 and 0
        }


CONTINUE

The continue statement skips the current iteration of a for, while , or do-while loop. 
This statement also can be labeled or unlabeled.
	int j = 5;
	for (int i = 0; i < 10; i++) {
		if (i != j) {
			continue;
			System.out.println("Found it at " + i);
		}
        }


RETURN

The return statement exits from the current method, and control flow returns to where the method was invoked.
The return statement has two forms: one that returns a value, and one that doesn't.
To return a value, simply put the value.

	return ++count;

When a method is declared void, use the form of return that doesn't return a value.

	return;


http://docs.oracle.com/javase/tutorial/java/nutsandbolts/flow.html