0
0
Javaprogramming~10 mins

Counter-based 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 initialize the counter variable.

Java
int count = [1];
Drag options to blanks, or click blank then click option'
A-1
B1
C0
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Starting the counter at 1 instead of 0.
Using a negative number as the starting value.
2fill in blank
medium

Complete the code to write the while loop condition to run 5 times.

Java
while (count [1] 5) {
    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 greater than instead of less than.
Using equality which runs only once.
3fill in blank
hard

Fix the error in the loop increment statement.

Java
while (count < 5) {
    System.out.println(count);
    count[1];
}
Drag options to blanks, or click blank then click option'
A++
B+
C--
D+=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' without assignment.
Using '--' which decreases the counter.
4fill in blank
hard

Fill both blanks to create a loop that counts down from 5 to 1.

Java
int count = [1];
while (count [2] 0) {
    System.out.println(count);
    count--;
}
Drag options to blanks, or click blank then click option'
A5
B0
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Starting count at 0 instead of 5.
Using '<' which would cause an infinite loop.
5fill in blank
hard

Fill all three blanks to create a loop that prints even numbers from 2 to 10.

Java
int count = [1];
while (count [2] 10) {
    System.out.println(count);
    count [3] 2;
}
Drag options to blanks, or click blank then click option'
A2
B<=
C+=
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Starting at 1 instead of 2.
Using '<' which excludes 10.
Using '+' without assignment.