0
0
Javaprogramming~20 mins

While loop execution flow in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
While Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple while loop
What is the output of the following Java code?
Java
int i = 1;
while (i <= 3) {
    System.out.print(i + " ");
    i++;
}
A1 2
B1 2 3 4
C0 1 2 3
D1 2 3
Attempts:
2 left
💡 Hint
Remember the loop runs while the condition is true and increments i each time.
Predict Output
intermediate
2:00remaining
While loop with break statement
What will be printed by this Java code?
Java
int i = 0;
while (i < 5) {
    if (i == 3) {
        break;
    }
    System.out.print(i + " ");
    i++;
}
A0 1 2
B0 1 2 3
C1 2 3
D0 1 2 3 4
Attempts:
2 left
💡 Hint
The break stops the loop when i equals 3.
Predict Output
advanced
2:00remaining
While loop with continue statement
What is the output of this Java code snippet?
Java
int i = 0;
while (i < 5) {
    i++;
    if (i == 3) {
        continue;
    }
    System.out.print(i + " ");
}
A1 2 3 4 5
B0 1 2 4 5
C1 2 4 5
D1 2 3 5
Attempts:
2 left
💡 Hint
The continue skips printing when i is 3.
🧠 Conceptual
advanced
2:00remaining
Understanding while loop condition evaluation
Consider this code: int i = 5; while (i > 0) { System.out.print(i + " "); i -= 2; } How many times does the loop run?
A2 times
B3 times
C4 times
D5 times
Attempts:
2 left
💡 Hint
Count how i changes each loop and when the condition fails.
Predict Output
expert
2:00remaining
While loop with nested conditions and variable changes
What is the output of this Java code?
Java
int i = 1;
int sum = 0;
while (i <= 5) {
    if (i % 2 == 0) {
        i++;
        continue;
    }
    sum += i;
    i++;
}
System.out.println(sum);
A9
B15
C6
D10
Attempts:
2 left
💡 Hint
Only odd numbers are added to sum because even numbers skip the addition.