While loop
While loop is a control flow statement that allows the code to execute the flow of statement repeatedly according to the Boolean conditions.
Syntax:
Flowchart:
While loop starts by checking the given condition first. If the given condition is true, then the statements in the loop executed else the first statement following the loop will be executed. That's why it calls an Entry control Loop.
When the condition is checked and true, all the statements inside the loop body are executed.
When the condition becomes false, then the loop will be terminated and end the cycle.
Examples
1. Print Numbers from 1 to 10 using a while loop.
public class Numbers1t10 {public static void main(String args[]){int n =1;while(n<=10){System.out.println(n);n++;}}}
Output
1
2
3
4
5
6
7
8
9
10
2. Write a program to print the number reversely using a while loop.
public class Numbers1t10 {public static void main(String args[]){int n = 10;while(n>0){System.out.println(n);n--;}}}
Output
10
9
8
7
6
5
4
3
2
1
0 Comments