0
0
Javaprogramming~10 mins

Difference between while and do–while in Java - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a while loop that prints numbers from 1 to 3.

Java
int i = 1;
while ([1]) {
    System.out.println(i);
    i++;
}
Drag options to blanks, or click blank then click option'
Ai < 1
Bi <= 3
Ci == 0
Di > 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using a condition that never becomes false, causing an infinite loop.
Using a condition that is false at the start, so the loop never runs.
2fill in blank
medium

Complete the code to create a do-while loop that prints numbers from 1 to 3.

Java
int i = 1;
do {
    System.out.println(i);
    i++;
} while ([1]);
Drag options to blanks, or click blank then click option'
Ai <= 3
Bi == 0
Ci > 3
Di < 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using a condition that is false at the start, which still runs once in do-while but may confuse.
Using a condition that never becomes false, causing an infinite loop.
3fill in blank
hard

Fix the error in the while loop condition to avoid an infinite loop.

Java
int count = 5;
while ([1]) {
    System.out.println(count);
    count--;
}
Drag options to blanks, or click blank then click option'
Acount < 0
Bcount == 5
Ccount >= 0
Dcount > 5
Attempts:
3 left
💡 Hint
Common Mistakes
Using a condition that never changes to false, causing infinite loop.
Using a condition that is false at the start, so loop never runs.
4fill in blank
hard

Fill both blanks to create a while loop that prints even numbers from 2 to 6.

Java
int num = 2;
while ([1]) {
    System.out.println(num);
    num [2] 2;
}
Drag options to blanks, or click blank then click option'
Anum <= 6
Bnum >= 6
C+=
D-=
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong comparison operator that stops the loop too early or never.
Using the wrong operator to change num, causing wrong output.
5fill in blank
hard

Fill all three blanks to create a do-while loop that prints numbers from 1 to 5.

Java
int i = [1];
do {
    System.out.println(i);
    i [2] 1;
} while (i [3] 5);
Drag options to blanks, or click blank then click option'
A0
B+=
C<=
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Starting i at 0 instead of 1.
Using the wrong operator to increase i.
Using a wrong condition that causes infinite loop or no output.