while Loop
The while loop is used to repeat a block of code as long as a condition remains true. It is commonly used when the number of iterations is not known in advance.
Working of while Loop:
- First, the condition is checked.
- If the condition is true, the loop body is executed.
- After execution, control goes back to the condition.
- The loop continues until the condition becomes false.
- Once false, the loop terminates, and control moves to the next statement after the loop.
Key Points of while Loop:
- Condition is checked before entering the loop body.
- It is called an entry-controlled loop.
- May execute zero or more times, depending on the condition.
- Best used when we don’t know how many times we need to repeat a task.
Syntax:
while (condition) {
// code to run
}
- Initialization – Done outside the loop to set the starting value.
- Condition – Checked before each iteration to control loop execution.
- Loop Body – Executes only if the condition is true.
- Updation – It can be increment/ decrememnt. Done inside the loop to eventually make the condition false.
Example:
class ShikshaSanchar {
public static void main(String[] args) {
int i = 1; //initialization
while(i <= 5) { //condition
System.out.println("Hello from Shiksha Sanchar " + i);
i++; //updation
}
}
}
Output:
Hello from Shiksha Sanchar 1
Hello from Shiksha Sanchar 2
Hello from Shiksha Sanchar 3
Hello from Shiksha Sanchar 4
Hello from Shiksha Sanchar 5
Explanation:
- Initialization( int i = 1): A variable i is declared and initialized to 1. This variable controls how many times the loop will run.
- Condition (while(i <= 5)): This is the condition of the loop. The loop will continue as long as i is less than or equal to 5. If the condition is false, the loop will stop immediately.
- Loop Body (System.out.println("Hello from Shiksha Sanchar " + i);): This line is the loop body. It prints a message with the current value of i.
- Updation (i++): After printing, i is incremented by 1. This step ensures that the condition will eventually become false, so the loop can terminate.
How the Loop Works (Iteration-wise):
| Iteration | i value | Condition i <= 5 | Printed Message | New i value |
|---|---|---|---|---|
| 1 | 1 | true | Hello from Shiksha Sanchar 1 | 2 |
| 2 | 2 | true | Hello from Shiksha Sanchar 2 | 3 |
| 3 | 3 | true | Hello from Shiksha Sanchar 3 | 4 |
| 4 | 4 | true | Hello from Shiksha Sanchar 4 | 5 |
| 5 | 5 | true | Hello from Shiksha Sanchar 5 | 6 |
| 6 | 6 | false | Loop stops | - |
Flow diagram:
Here is the flow diagram for the while loop, showing the proper order:
- Initialization (i = 1) happens before the loop.
- Condition is checked before entering the loop body.
- Inside the loop: Print → Update → Check again.
- If the condition becomes false, it exits to End.
Summary:
- Condition is checked before the loop starts.
- Initialization is done outside the loop.
- Loop runs only if condition is true.
- Updation is done manually inside the loop.
- May run zero or more times.
- Ideal when iteration count is unknown.
- Risk of infinite loop if condition never fails.