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:

  1. First, the condition is checked.
  2. If the condition is true, the loop body is executed.
  3. After execution, control goes back to the condition.
  4. The loop continues until the condition becomes false.
  5. 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:

Java while loop 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.

Welcome to ShikshaSanchar!

ShikshaSanchar is a simple and helpful learning platform made for students who feel stressed by exams, assignments, or confusing topics. Here, you can study with clarity and confidence.

Here, learning is made simple. Notes are written in easy English, filled with clear theory, code examples, outputs, and real-life explanations — designed especially for students like you who want to understand, not just memorize.

Whether you’re from school, college, or someone learning out of curiosity — this site is for you. We’re here to help you in your exams, daily studies, and even to build a strong base for your future.

Each note on this platform is carefully prepared to suit all levels — beginner to advanced. You’ll find topics explained step by step, just like a good teacher would do in class. And the best part? You can study at your pace, anytime, anywhere.

Happy Learning! – Team ShikshaSanchar