The syntax for the while loop is as follows:
while (loop-continuation-condition) {
statements; // loop body
}
The part of the loop that contains the statements to be repeated is called the loop body. Each loop contains a loop-continuation condition, a Boolean expression that controls the execution of the body. It is evaluated each time to determine if the loop body is executed. If its evaluation is true, the loop body is executed; if its evaluation is false, the entire loop terminates and the program control turns to the statement that follows the while loop.
Example, the following while loop prints "Superman VS Batman" a hundred times.
int count = 0;
while (count < 100) {
System.out.println("Superman VS Batman");
count++;
}
In the above program, the variable count is initially 0. The loop checks whether (count < 100) is true. If so, it executes the loop body to print the message "Superman VS Batman" and increments the count by 1. It repeatedly executes the loop body until (count<100) becomes false. When (count<100) is false (when count reaches 100), the loop terminates, and the next statement after the loop statement is executed.
Another common technique for controlling a loop is to designate a special value when reading and processing a set of values. This special input value, known as a sentinel value, signifies the end of the loop.
System.out.println("Enter data (enter 0 to stop): ");
int data = input.nextInt();
int sum = 0;
while (data != 0) {
sum += data;
System.out.println("Enter data (enter 0 to stop): ");
data = input.nextInt();
}
System.out.println("The sum is " + sum);
In the above program, if data is not 0, it is added to the sum, and the next item of input data is read. If the data is 0, the loop body is no longer executed and the while loop terminates.
The input value 0 is the sentinel value for this loop. Note that if the first input read is 0 the loop body never executes, and the resulting sum is 0.
The do-while loop is a variation of the while loop. Its syntax is:
do {
statements;
} while (loop-continuation-condition);
The loop body is executed first. Then the loop-continuation-condition is evaluated. If the evaluation is true, the loop body is executed again; if it is false, the do-while loop terminates.
The difference between a while loop and a do-while loop is the order in which the loop-continuation-condition is evaluated and the loop body executed.
Example:
int data;
int sum = 0;
Scanner input = new Scanner(System.in);
do {
System.out.print("Enter data (enter 0 to stop):");
data = input.nextInt();
sum += data;
} while (data != 0);
System.out.print("The sum is " + sum);
Write a program to read a sequence of positive integer numbers and find the total and average from that sequence.
import java.util.Scanner;
public class Average{
public static void main(String[] args) {
int num, total;
Scanner sc = new Scanner(System.in);
num = sc.nextInt();
total = 0;
while (num != -1){
total = total + num;
num = sc.nextInt();
}
double aver = (double)total / (double)n;
System.out.printf("%d %.2f\n", total, aver);
}
}
Let say the input are:
1 2 3 4 5 6 -1
Then, the output will be:
21 4.20
A nested while loop is a while statement inside another while statement. In a nested while loop, one iteration of the outer loop is first executed, after which the inner loop is executed. The execution of the inner loop continues till the condition described in the inner loop is satisfied.
while(condition) { // outer loop
while(condition) { // inner loop
// statement of inside loop
}
// statement of outer loop
}
do{
do{
// statement of inside loop
}while(condition);
// statement of outer loop
}while(condition);