0
0
Javaprogramming~10 mins

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

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

Java
int sum = 0;
int num = 1;
while(num [1] 10) {
    sum += num;
    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 '<' excludes 10 from the sum.
Using '>' or '>=' will cause the loop not to run properly.
3fill in blank
hard

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

Java
for(int i = 0; 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--' with 'i < 5' causes an infinite loop.
Using '>' with 'i--' is correct but not matching the current code.
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
B1
C<=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Starting at 1 will print odd numbers.
Using '<' will exclude 10 from printing.
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'
A1
B>=
C--
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' with decrement causes the loop not to run.
Using '++' instead of '--' will cause an infinite loop.