0
0
Javaprogramming~10 mins

Do–while loop in Java - Interactive Code Practice

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

Complete the code to start a do-while loop that prints "Hello" once.

Java
do {
    System.out.println("Hello");
} [1] (false);
Drag options to blanks, or click blank then click option'
Awhile
Bfor
Cif
Dswitch
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'if' instead of 'while' after the do block.
Forgetting the parentheses around the condition.
2fill in blank
medium

Complete the code to declare an integer variable i initialized to 0 before the do-while loop.

Java
[1] i = 0;
do {
    System.out.println(i);
    i++;
} while (i < 3);
Drag options to blanks, or click blank then click option'
AString
Bfloat
Cint
Dboolean
Attempts:
3 left
💡 Hint
Common Mistakes
Using float or boolean instead of int.
Using String for a number variable.
3fill in blank
hard

Fix the error in the do-while loop condition to correctly check if i is less than 5.

Java
int i = 0;
do {
    System.out.println(i);
    i++;
} while (i [1] 5);
Drag options to blanks, or click blank then click option'
A>=
B>
C==
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using > or >= which would cause the loop to never run or run incorrectly.
Using == which only runs when i equals 5.
4fill in blank
hard

Fill both blanks to create a do-while loop that prints numbers from 1 to 3.

Java
int num = 1;
do {
    System.out.println(num);
    num[1];
} [2] (num <= 3);
Drag options to blanks, or click blank then click option'
A++
B--
Cwhile
Dif
Attempts:
3 left
💡 Hint
Common Mistakes
Using decrement operator -- which would count down instead of up.
Using if instead of while after the do block.
5fill in blank
hard

Fill all three blanks to create a do-while loop that prints "Count: x" for x from 1 to 4.

Java
int count = [1];
do {
    System.out.println("Count: " + [2]);
    [3];
} while (count <= 4);
Drag options to blanks, or click blank then click option'
A1
Bcount
Ccount++
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Starting count at 0 which would print from 0 instead of 1.
Forgetting to increment count inside the loop.