Java – Iterative Flow Control

While Statement

Java provides certain loop statements, which you can use to perform an action repeatedly. These statements are called Iterative Statements. One of the loop statements is the while statement.

SYNTAX:

while (condition) {
loopbody
}

The ‘while’ statement contains a condition and a loopbody. The condition is enclosed within parentheses. All the variables used in the condition of the while statement must be initialized before the while loop. The values of the variables used in the condition must be changed in the loopbody. Otherwise, the condition may always remain true and the loop may never end. You change the values of the variables by performing various operations on the variables. You can write a single statement or multiple statements in the loopbody. Multiple statements should be enclosed within braces. Otherwise, the code may generate an unexpected output.

How ‘While’ Works

How While Statement works

Before the loopbody is executed, the condition of the while statement is evaluated. The loopbody is executed only when the condition evaluates to true. Therefore, the while loop is also called a top tested loop. After the loopbody is executed, the while condition is evaluated again. The sequence of the evaluating the condition and executing the loopbody continues until the while condition becomes false.

Example

Lets see a example program that stores two numbers in the variables [Lets say num and n]. The output should display the numbers from 0 to the number stored in the num variable.

In the code displayed below, the while statement specifies the condition that the value of n should be less than or equal to the value of num. Line 1 and Line 5 contain the opening and closing braces of the while loop. In Line 3, the value of n is printed on the screen. In Line 4, the value of n is incremented by 1.

When the program is executed, it displays the numbers from 0 to 5.

In Line 1, the condition is that the value of n should be less than or equal to the value of the variable num. The value of the variable num is 5. The initial value of n is 0, which is less than the value of num. Therefore, the condition is true and the flow of control moves inside the while loop. Inside the loop, the value of the variable n is displayed in the first row. This value is 0. Next, the flow control executes Line 4. As a result, the value of the variable n becomes 1. The flow of control moves back to Line 1, where the value of the variable n is again compared with the value of num. The value of the variable n is 1. Therefore, it is still less than the value of num, which is 5. The condition in Line 1 evaluates again to true, and the flow of control again moves inside the while loop. The value of the variable n is printed in Line 3. This looping continues until the value of n becomes 6. The flow of control moves out of the while loop, and the program terminates.

Infinite While Loops

If a semicolon is added after the condition in the while statement in a program, the program may result in an infinite loop. This is because the control never comes out of the while statement.

For Example:

If we modify the Line 1 of example program like this

while (n <= num); {

the program will result in an infinite loop.

do-while statement

The do-while statement is a loop statement similar to the while statement provided by Java. It is used to perform certain repetitive actions depending on a condition.

SYNTAX:

do {
loopbody
} while (condition);

The do-while statement begins with the ‘do’ keyword followed by the loopbody. In the loopbody, you can write a single statement or multiple statements enclosed within braces. The while statement is written after the closing brace. The while statement contains the condition enclosed in parentheses. In the do-while statement, you add a semicolon after the condition. The semicolon indicates the end of the do-while loop.

How ‘do-while’ Works

The major difference between the do-while statement and the while statement is that in the while statement, the loopbody is executed only when the condition stated in the statement is true. In contrast, the do-while statement in the loopbody is executed at least once, regardless of the condition evaluating to true or false.

'do-while' flow

After the loopbody is executed once, the condition in the do-while statement is checked. If the condition evaluates to true, the loopbody is executed until the condition becomes false. Therefore do-while loop is also called a bottom-tested loop.

You can use the do-while loop in situations where an action must be performed at least once without evaluating the condition.

Example

Consider a program that stores a number in a variable. The program displays all the numbers in descending order starting from the number stored in the variable until it encounters the number 0.

do-while example

The program shown above displays the do keyword and further decrements the value of the count variable that is added. After the count variable is decremented, we specify the condition to check whether the value is equal to 0 like this [while (count != 0);]. The closing brace of the main function and the class declaration is added.

Upon compilation and execution, the output displays all the number from 1 to 4 in descending order.

For Statement

Java provides the for statement to manage iterative actions as an efficient alternative to the while statement. The for statement creates a loop in which a set of statements is repeatedly executed until the specified condition becomes false.

SYNTAX:

for (initialization expressions; test condition; update expressions) {
loopbody;
}

The for statement starts with the ‘for’ keyword. This statement has three elements enclosed within parentheses. The first element of the for statement consists of initialization expressions. This element is an assignment statement. The assignment is done only once before the for statement begins execution. The second element, test condition, is evaluated before each iteration of the for statement. This element determines whether the execution of the for loop should continue or terminate. The third element in the for statement consists of the update expressions. These expressions change the values of the variables used in the test condition. Update expressions are executed at the end of each iteration after the loopbody is executed. The elements of a for statement are seperated by semicolons. You can use multiple statements in the initialization and update expressions of the for statement by seperating them with the comma operator. The multiple statements are evaluated from left to right.

EXAMPLE: Multiple initialization and update expression

for (x = 1, y = 2; test condition; x++, y++) {
loopbody;
}

Another Form

In the for statement, you can omit the initialization expressions and update expressions elements but you must provide two semicolons in the parentheses.

If you are not including the initialization and update expressions elements in the for statement, you must specify the initialization expressions element before the for statement. The update expressions element must then be specified inside the for loop.

SYNTAX:

initialization expressions;

for (; test condition;) {
update expressions;
loopbody;
}

Infinite For Loop

The test condition must be provided in the for statement. If you omit the test condition from the for statement, the program results in an infinite loop when executed. The program will also result in an infinite loop if none of the expressions is specified in the for loop.

SYNTAX:


for (initialization expressions; ; update expressions) {
loopbody;
}

for (;;) {
loopbody;
}

How ‘For’ Works

for (j = 2; j <= 10; j++) {
System.out.println(j);
}

How 'For' Works

In the for loop, the variable j is initialized to 2. Next, the value of j is compared with 10. If the test condition is true, the value of j is displayed on the screen. Next, the value of j is incremented by one. After the value of j is incremented, the test condition is evaluated again. If the test condition evaluates to true, the print function is executed again. If the test condition evaluates to false, the flow of control comes out of the for loop. The for statement creates a loop that is identical in function to the while statement. For loops are more useful when you have a fixed number of iterations and you need a counter. The for loop is also more compact. You can use the for statement to reduce the size of code.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s