Java While loop

The while loop in Java runs a block of code over and over as long as the specified condition is true. This loop is often used when you don’t know how many times it will run ahead of time, and you want it to keep going based on a condition.

The condition can be any Boolean expression. The loop’s body will execute as long as the condition stays true. When the condition turns false, control moves to the next line of code right after the loop.

Syntax:


while (condition) {
    // Code to be executed
}

Important Points:

1. Condition: The loop checks the condition before executing the block of code. If the condition is false, the loop will terminate.

2. Infinite Loop: If the condition always evaluates to true, the loop will run indefinitely unless explicitly broken.

3. Use Case: Best for scenarios where you don’t know the exact number of iterations beforehand.

Example:


public class Main {
    public static void main(String[] args) {
        int count = 1;
        while (count <= 4) {
            System.out.println("Count is: " + count);
            count++;
        }
    }
}

Output:

Count is: 1
Count is: 2
Count is: 3
Count is: 4

Example 2: How to break the Loop


public class Main {
    public static void main(String[] args) {
        int count = 1;
        while (true) {  // Infinite loop
            System.out.println("Count is: " + count);
            count++;
            if (count > 2) {
                break;  // Exit the loop
            }
        }
    }
}

Output:

Count is: 1
Count is: 2

Example 3: How to use continue in While loop?


public class Main {
    public static void main(String[] args) {
        int count = 0;
        while (count < 5) {
            count++;
            if (count == 2) {
                continue;  // Skip the rest of the loop body for this iteration
            }
            System.out.println("Count is: " + count);
        }
    }
}

Output:

Count is: 1
Count is: 3
Count is: 4
Count is: 5

Java While loop – Interview Questions

Q 1: What is while loop in Java?
Ans: A loop that executes while a condition remains true.
Q 2: Is while loop entry-controlled?
Ans: Yes, condition is checked before execution.
Q 3: What happens if condition is false initially?
Ans: The loop body will not execute.
Q 4: Can while loop run infinitely?
Ans: Yes, if condition never becomes false.
Q 5: When to use while loop?
Ans: When iterations are not predetermined.

Java While Loop – Objective Questions (MCQs)

Q1. Which of the following is the correct syntax for a while loop in Java?






Q2. What will be the output of the following code?

int i = 1;
while (i <= 3) {
System.out.print(i + ' ');
i++;
}






Q3. What will happen if the condition in a while loop never becomes false?






Q4. What is the output of the following code?

int count = 5;
while (count > 0) {
System.out.println('Hello');
count--;
}






Q5. What will be the output of this code?

int x = 10;
while (x < 5) {
System.out.println('Inside Loop');
x++;
}
System.out.println('Outside Loop');






Related Java While Loop Topics