CONTROL FLOW STATEMENT:
Java compiler executes the code from top to bottom. The statements in the code are executed according to the order in which they appear. However, Java provides statements that can be used to control the flow of Java code. Such statements are called control flow statements. It is one of the fundamental features of Java, which provides a smooth flow of program.
Loop statements
do while loop
while loop
for loop
for-each loop
while loop:
The while loop is also used to iterate over the number of statements multiple times. However, if we don't know the number of iterations in advance, it is recommended to use a while loop. Unlike for loop, the initialization and increment/decrement doesn't take place inside the loop statement in while loop.
It is also known as the entry-controlled loop since the condition is checked at the start of the loop. If the condition is true, then the loop body will be executed; otherwise, the statements after the loop will be executed.
SYNTAX:
while(condition)
{
//looping statements;
}
FLOWCHART:
Image description
EXAMPLE PROGRAM:
public class Calculation {
public static void main(String[] args) {
int i = 0;
System.out.println("Printing the list of first 10 evennumbers");
while(i<=10) {
System.out.println(i);
i = i + 2;
}
}
}
output:
Printing the list of first 10 evennumbers
0
2
4
6
8
10
TASK1:
program:
public class Count
{
public static void main(String args[])
{
int count=1;
while(count<=5)
{
count=count+1;
System.out.println("count: " +count);
}
}
}
output:
count: 2
count: 3
count: 4
count: 5
count: 6
TASK2:
program:
public class Count1
{
public static void main(String args[])
{
int count=5;
while(count>=1)
{
count=count-1;
System.out.println("count: "+count);
}
}
}
output:
count: 4
count: 3
count: 2
count: 1
count: 0
TASK3:
program:
public class Count2
{
public static void main(String args[])
{
int count=1;
while(count<=10)
{
count=count+2;
System.out.println("count: "+count);
}
}
}
output:
count: 3
count: 5
count: 7
count: 9
count: 11
TASK4:
program:
public class Count3
{
public static void main(String args[])
{
int count=0;
while(count<10)
{
count=count+2;
System.out.println("count: "+count);
}
}
}
output:
count: 2
count: 4
count: 6
count: 8
count: 10
Author Of article : Ilakkiya Read full article