Java While Keyword
Depending on a specified Boolean condition, a while loop in Java allows code to be executed repeatedly. The while loop can be viewed as an iterative version of the if statement. When we need to continually run a block of statements, Java's while loop is used. While loops are viewed as iterative if statements. Use of the while loop is advised if the number of iterations is not fixed. Until the provided Boolean condition is true, a section of the program is iterated again using the while loop in Java. The loop automatically ends when the Boolean condition changes from true to false.
While loops are viewed as iterative if statements. The while loop is advised if the number of iterations is not fixed.
Execution of while loop:
- The while loop takes over control.
- The condition condition flow jumps to is tested.
- The flow enters the Body if Condition results in a true result.
- If the condition returns false, the flow exits the loop.
- The statements contained within the loop's body are performed.
- Updates are made.
- It then returns to Step 2.
- The while loop has come to an end, and the flow has changed.
Syntax:
while (condition) {
//code to be executed
Increment / decrement statement
}
The components of a do-while loop are:
- Condition: An expression is being tested. If the criterion is satisfied, the loop's body is executed before control is passed on to the update expression. We end the while loop when the condition is no longer true.
Example:
I <=100
- Update expression: Every time the loop's body is executed, this expression raises or lowers the loop variable.
Example:
i++;
Flowchart of Java While Loop
While loops are significant in this context because they occasionally fail to run. If the condition being tested returns a false result, the while loop's body is skipped, and the first statement after the while loop is executed.
Example:
We output integer numbers from 1 to 10 in the example below. Unlike the for loop, the variable used in the condition needs to be initialized and increased independently. If not, the loop will run indefinitely.
WhileExample.java
public class WhileEx {
public static void main(String[] s) {
int a=2;
while(a<=11){
System.out.println(a);
a++;
}
}
}
Output:
2
3
4
5
6
7
8
9
10
11
Java Infinitive While Loop
If you include true in the while loop, the loop will continue forever.
Syntax:
while(true){
//code to be executed
}
Example2:
public class WhileEx2 {
public static void main(String[] s) {
// passing true to the condition causes the while loop to be infinite
while(true) {
System.out.println("while loop of infinity");
}
}
}
Output:
while loop of infinity
while loop infinity
while loop infinity
while loop infinity
while loop infinity
In the code above, we must type Ctrl + C to break the infinite loop.
Dry-Running Example 1: The program will execute in the following manner.
- Program is started.
- a is initialized with value 2.
- Condition is verified. 2<7 yields true.
- "Hello Raju" gets executed 1st time.
- Updation is done. Now a = 3.
- Condition is verified. 3<7 yields true.
- "Hello Raju" gets executed 2nd time.
- Updation is done. Now a = 4.
- Checking the condition 4<7 yields true.
- "Hello Raju" gets executed 3rd time
- Updation is done. Now a = 5.
- Checking the condition 5<7 yields true.
- "Hello Raju" gets executed 4th time
- Updation is done. Now a = 6.
- Checking the condition 6<7 yields true.
- "Hello Raju" gets executed 5th time
- Updation is done. Now a = 7.
- Checking the condition that is 7<7 yields false.
- Flow of the loop goes outside the loop. Program terminates.
Example 2: This program will find the summation of numbers from 1 to 10.
// program to declare while loop
class whileLoop {
public static void main(String s[])
{
int k = 1, s = 0;
// Exit the loop when k becomes greater than 4
while (x <= 10) {
// adding up k
s = s + k;
// Incresing the value of k for next loop
k++;
}
System.out.println("Summation is: " + s);
}
}
Output:
Summation is: 55