0
0
Javaprogramming~10 mins

Why loop control is required in Java - Test Your Understanding

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 for loop.

Java
for(int i = 1; i [1] 5; i++) {
    System.out.println(i);
}
Drag options to blanks, or click blank then click option'
A<=
B>
C==
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' or '>=' will not start the loop correctly.
Using '==' will only run the loop when i equals 5, missing other numbers.
2fill in blank
medium

Complete the code to stop the loop when the number 3 is reached.

Java
for(int i = 1; i <= 5; i++) {
    if(i [1] 3) {
        break;
    }
    System.out.println(i);
}
Drag options to blanks, or click blank then click option'
A<
B>
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' or '>' will break the loop too early or too late.
Using '!=' will break the loop for all values except 3.
3fill in blank
hard

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

Java
int count = 0;
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 '>' or '>=' causes the loop to never run or run infinitely.
Using '==' runs the loop only when count equals 5, which is never true initially.
4fill in blank
hard

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

Java
for(int i = [1]; i [2] 10; i += 2) {
    System.out.println(i);
}
Drag options to blanks, or click blank then click option'
A2
B<=
C<
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Starting at 1 will print odd numbers.
Using '<' will exclude 10 from the output.
5fill in blank
hard

Fill all three blanks to create a loop that prints numbers from 10 down to 1.

Java
for(int i = [1]; i [2] 1; i[3]) {
    System.out.println(i);
}
Drag options to blanks, or click blank then click option'
A10
B>=
C--
D++
Attempts:
3 left
💡 Hint
Common Mistakes
Using '++' will cause an infinite loop counting up.
Using '<=' will never run the loop since 10 is not less than or equal to 1.