0
0
Javaprogramming~10 mins

For loop syntax 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 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 'i < 5' will stop the loop before printing 5.
Using 'i > 5' or 'i >= 5' will not run the loop as expected.
2fill in blank
medium

Complete the code to sum numbers from 1 to 10 using a for loop.

Java
int sum = 0;
for (int num = 1; num [1] 10; num++) {
    sum += num;
}
System.out.println(sum);
Drag options to blanks, or click blank then click option'
A<=
B>=
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'num < 10' will exclude 10 from the sum.
Using 'num > 10' will not run the loop.
3fill in blank
hard

Fix the error in the for loop that counts down from 5 to 1.

Java
for (int i = 5; i [1] 1; 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 '>' will stop the loop before printing 1.
Using '<' or '<=' will not run the loop correctly when counting down.
4fill in blank
hard

Fill both blanks to create a for 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 being printed.
5fill in blank
hard

Fill all three blanks to create a for loop that prints the squares of numbers from 1 to 5.

Java
for (int [1] = 1; [2] [3] 5; [1]++) {
    System.out.println([1] * [1]);
}
Drag options to blanks, or click blank then click option'
Ai
C<=
Dj
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in initialization and condition causes errors.
Using '<' excludes the last number 5.