0
0
Javaprogramming~10 mins

While loop execution flow 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 print numbers from 1 to 5 using a while loop.

Java
int i = 1;
while (i [1] 5) {
    System.out.println(i);
    i++;
}
Drag options to blanks, or click blank then click option'
A<=
B<
C>
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'i < 5' which stops before printing 5.
Using 'i > 5' which never runs the loop.
2fill in blank
medium

Complete the code to stop the loop when the variable count reaches 10.

Java
int count = 0;
while (count [1] 10) {
    System.out.println(count);
    count++;
}
Drag options to blanks, or click blank then click option'
A>
B<=
C>=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>=' which causes the loop to never run.
Using '<=' which runs the loop one extra time.
3fill in blank
hard

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

Java
int x = 5;
while (x [1] 0) {
    System.out.println(x);
    x--;
}
Drag options to blanks, or click blank then click option'
A>=
B<
C>
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' causes the loop to never run.
Using '>' alone excludes zero and may cause logic errors.
4fill in blank
hard

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

Java
int num = 2;
while (num [1] 10) {
    System.out.println(num);
    num [2] 2;
}
Drag options to blanks, or click blank then click option'
A<=
B+=
C-=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' excludes 10 from output.
Using '-=' decreases num causing infinite loop.
5fill in blank
hard

Fill all three blanks to create a while loop that counts down from 15 to 5 by 2.

Java
int val = [1];
while (val [2] [3]) {
    System.out.println(val);
    val -= 2;
}
Drag options to blanks, or click blank then click option'
A15
B>=
C5
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' instead of '>=' causes the loop to never run.
Starting at 5 instead of 15 prints wrong numbers.