Home » Java Basics » 02 - Java Concepts
2

Control Flow Statements

The definition and usage of control flow statements in java

Control flow statements allow programs to take different paths based on conditions, execute a block of code over and over again, and jump out of a block. Control flow statements break program execution from top to bottom and allow control to branch to different sections and conditionally execute code.

If - Else

if (x == y)
{
System.out.println("IF");
i = 1;
}
else
{
System.out.println("ELSE");
i = 2;
}

In the above statement, if the value of x is equal to the value of y, IF is printed out and i is set to equal 1. Otherwise, ELSE is printed out and i is set to 2.

If-Then-Else

        if (temperature >= 90) {
climate = "Very Hot";
} else if (temperature >= 75) {
climate = "Bearable";
} else if (temperature >= 60) {
climate = "Nice";
} else if (temperature >= 40) {
climate = "Cool";
} else {
climate = "Brrrr!";
}

Else-if statements may be used to allow more than two conditions. In the above code, climate is set to a different value based on temperature intervals more than 90, between 75 and ninety, between sixty and seventy five, between 40 and 60, and below 40.

Switch

       switch (grade) {
case 'A':  System.out.println("Excellent!"); break;
case 'B':  System.out.println("Good"); break;
case 'C':  System.out.println("Satisfactory"); break;
case 'D':  System.out.println("Pass"); break;
case 'F':  System.out.println("Fail"); break;
case 'I':  System.out.println("Incomplete"); break;
default: System.out.println("Invalid grade.");break;
}

A switch is useful when a single variable needs to be tested for the condition, and when the same variable may take many values. The above switch statement prints out a message based on the value of the 'grade' variable. Note that an if loop could be used to do the same thing. The break statement makes the program exit the switch loop. After an initial true condition, every line is executed until a break statement is encountered. Break functionality may be used to the programmer's advantage, as the following code to compute the number of days in a month from sun's Java site illustrates:

        switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
numDays = 31;
break;
case 4:
case 6:
case 9:
case 11:
numDays = 30;
break;
case 2:
if ( ((year % 4 == 0) && !(year % 100 == 0))
|| (year % 400 == 0) )
numDays = 29;
else
numDays = 28;
break;
default:
System.out.println("Invalid month.");
break;

While

          int i = 0;
while (i < 10) {
System.out.println("Value of i: " + i);
i++;
}

The ‘while’ statement executes the code within the block enclosed in curly braces over and over again until the expression right next to it evaluates to be 'false'. The above example causes i's value to be printed out 10 times; once for each time through the loop. Note that complex expressions like 'while (i<10 && j>10)' may also be used.

Do-While

          int i = 10;
do {
System.out.println("Value of i: " + i);
i++;
} while (i < 10);

The ‘do-while’ statement executes at least once. The above example would never be executed if it were a ‘while’ loop since i is initially set to equal 10, but is executed a single time because the ‘while’ condition occurs after the block in a ‘do-while’ loop.

For

The ‘for’ loop allows execution of a block of code over and over again for a specified range of values of a counter. The syntax for a ‘for’ loop is as follows:

 for (initial value of counter; condition; increment/decrement rule)
{
.
.
Code Executed one time for each value of counter
.
.
}

In the following example, the value of i is printed out ten times. Note that the first part of the expression in parenthesis is executed just once at the start of the ‘for’ loop. The condition is tested each time through the loop and the last part is executed each time through the loop.

for (int i = 0; i < 10; i++)
{
System.out.println(i);
}

Break, Continue, and Return Statements

Break, continue, and return statements allow 'jumps' in program execution and should be used with great care to avoid bug-prone applications. The break statement terminates the currently executing loop or block. Control is transferred to just after the loop.

for (int i = 0; i < 5; i++)
{
if (i == 2)
break;
System.out.println(i);
}

In the above code, i is just printed twice; the break makes the loop end when i becomes 2. A continue statement skips a single iteration or time through a loop

for (int i = 0; i < 5; i++)
{
if (i == 2)
continue;
System.out.println(i);
}

Here, i is printed out for values 0, 1, 3, and 4 and the 'continue' statement skips the i = 2 iteration.

Normally, control returns to the caller from an invoked method after the entire method has been executed. Return statements override this principle; control flow returns to the method's caller right after a return statement is encountered. Return statements may also be used to send output back to the calling function.