0
0
Javaprogramming~10 mins

Loop initialization, condition, update 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 loop variable correctly.

Java
for ([1]; i < 5; i++) {
    System.out.println(i);
}
Drag options to blanks, or click blank then click option'
Aint = 0
Bi = 0
Cint i
Dint i = 0
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to declare the variable type.
Using assignment without declaration.
2fill in blank
medium

Complete the code to write the correct loop condition.

Java
for (int i = 0; [1]; i++) {
    System.out.println(i);
}
Drag options to blanks, or click blank then click option'
Ai < 5
Bi == 5
Ci > 5
Di <= 5
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= 5 runs the loop one extra time.
Using == 5 does not run the loop at all.
3fill in blank
hard

Fix the error in the loop update expression.

Java
for (int i = 0; i < 5; [1]) {
    System.out.println(i);
}
Drag options to blanks, or click blank then click option'
Ai++
Bi = i + 1
Ci--
Di += 2
Attempts:
3 left
💡 Hint
Common Mistakes
Using decrement i-- causes an infinite loop.
Using i += 2 skips numbers.
4fill in blank
hard

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

Java
for (int i = [1]; i [2] 0; i--) {
    System.out.println(i);
}
Drag options to blanks, or click blank then click option'
A10
B>
C>=
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Starting at 0 instead of 10.
Using i < 0 as condition causes no iterations.
5fill in blank
hard

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

Java
for (int [1] = [2]; [1] [3] 10; [1] += 2) {
    System.out.println([1]);
}
Drag options to blanks, or click blank then click option'
Ai
B2
C<=
Dj
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names.
Starting at 0 or 1 instead of 2.
Using < instead of <= causes missing 10.