This loop can be used for executing the statements multiple times. A while loop has to be used when we do not know the exact number of iterations.
Syntax:
While(Condition){
Statements;
}
Program:
class WhileDemo{
public static void main(string args[]){
int n=8, i=1;
while(i<=10){
System.out.println(n+"*"+i+"="+(n*i));
i++;
}
}
}
Rule1: Specifying a condition to while loop is mandatory and it should be of boolean type.
Rule2: Specifying the { } is optional. If we do not specify the { } then it will consider only one statement, and that one statement is mandatory. If we want to consider mutliple statements then specifying the { } is mandatory. within the { } we can specify any number of statements.
Note: A java program should not contain any unreachable statements i.e. every statement has to be executed at some point of time.