Java Control Flow Statements : Page 2 of 3

Control statements control the order of execution in a java program, based on data values and conditional logic.
There are three main categories of control flow statements;

Selection statements: if, if-else and switch.

Loop statements: while, do-while and for.

Transfer statements: break, continue, return, try-catch-finally and assert.

We use control statements when we want to change the default sequential order of execution

ITERATION STATEMENTS

While Statement

The while statement is a looping construct control statement that executes a block of code while a condition is true. You can either have a single statement or a block of code within the while loop. The loop will never be executed if the testing expression evaluates to false. The loop condition must be a boolean expression.

The syntax of the while loop is

while (<loop condition>)
<statements>

Below is an example that demonstrates the looping construct namely while loop used to print numbers from 1 to 10.

public class WhileLoopDemo {

	public static void main(String[] args) {
		int count = 1;
		System.out.println("Printing Numbers from 1 to 10");
		while (count <= 10) {
			System.out.println(count++);
		}
	}
}

Output

Printing Numbers from 1 to 10
1
2
3
4
5
6
7
8
9
10

Download WhileLoopDemo.java

Do-while Loop Statement

The do-while loop is similar to the while loop, except that the test is performed at the end of the loop instead of at the beginning. This ensures that the loop will be executed at least once. A do-while loop begins with the keyword do, followed by the statements that make up the body of the loop. Finally, the keyword while and the test expression completes the do-while loop. When the loop condition becomes false, the loop is terminated and execution continues with the statement immediately following the loop. You can either have a single statement or a block of code within the do-while loop.

The syntax of the do-while loop is

do
<loop body>
while (<loop condition>);

Below is an example that demonstrates the looping construct namely do-while loop used to print numbers from 1 to 10.

public class DoWhileLoopDemo {

	public static void main(String[] args) {
		int count = 1;
		System.out.println("Printing Numbers from 1 to 10");
		do {
			System.out.println(count++);
		} while (count <= 10);
	}
}

Output

Printing Numbers from 1 to 10
1
2
3
4
5
6
7
8
9
10

Download DoWhileLoopDemo.java

Below is an example that creates A Fibonacci sequence controlled by a do-while loop

public class Fibonacci {

	public static void main(String args[]) {
		System.out.println("Printing Limited set of Fibonacci Sequence");
		double fib1 = 0;
		double fib2 = 1;
		double temp = 0;
		System.out.println(fib1);
		System.out.println(fib2);
		do {
			temp = fib1 + fib2;
			System.out.println(temp);
			fib1 = fib2; //Replace 2nd with first number
			fib2 = temp; //Replace temp number with 2nd number
		} while (fib2 < 5000);
	}
}

Output

Printing Limited set of Fibonacci Sequence

0.0
1.0
1.0
2.0
3.0
5.0
8.0
13.0
21.0
34.0
55.0
89.0
144.0
233.0
377.0
610.0
987.0
1597.0
2584.0
4181.0
6765.0

Download Fibonacci.java

For Loops

The for loop is a looping construct which can execute a set of instructions a specified number of times. It’s a counter controlled loop.

The syntax of the loop is as follows:

for (<initialization>; <loop condition>; <increment expression>)
<loop body>

The first part of a for statement is a starting initialization, which executes once before the loop begins. The <initialization> section can also be a comma-separated list of expression statements. The second part of a for statement is a test expression. As long as the expression is true, the loop will continue. If this expression is evaluated as false the first time, the loop will never be executed. The third part of the for statement is the body of the loop. These are the instructions that are repeated each time the program executes the loop. The final part of the for statement is an increment expression that automatically executes after each repetition of the loop body. Typically, this statement changes the value of the counter, which is then tested to see if the loop should continue.
All the sections in the for-header are optional. Any one of them can be left empty, but the two semicolons are mandatory. In particular, leaving out the <loop condition> signifies that the loop condition is true. The (;;) form of for loop is commonly used to construct an infinite loop.

Below is an example that demonstrates the looping construct namely for loop used to print numbers from 1 to 10.

public class ForLoopDemo {

	public static void main(String[] args) {
		System.out.println("Printing Numbers from 1 to 10");
		for (int count = 1; count <= 10; count++) {
			System.out.println(count);
		}
	}
}

Output

Printing Numbers from 1 to 10
1
2
3
4
5
6
7
8
9
10

 

Download ForLoopDemo.java

Like us on Facebook