Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to exit the loop when i equals 3.
Java
for (int i = 0; i < 5; i++) { if (i == [1]) { break; } System.out.println(i); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a value that never occurs in the loop condition.
Forgetting that break exits the loop immediately.
✗ Incorrect
The break statement exits the loop when i equals 3.
2fill in blank
mediumComplete the code to break the loop when the character 'x' is found.
Java
char[] letters = {'a', 'b', 'x', 'd'};
for (char c : letters) {
if (c == [1]) {
break;
}
System.out.print(c);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using single quotes incorrectly.
Breaking on the wrong character.
✗ Incorrect
The loop breaks when the character 'x' is found in the array.
3fill in blank
hardFix the error in the loop to break when number is 10.
Java
int[] numbers = {5, 10, 15};
for (int num : numbers) {
if (num == [1]) {
break;
}
System.out.println(num);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a value not in the array.
Confusing the break condition.
✗ Incorrect
The loop breaks when num equals 10, stopping further printing.
4fill in blank
hardFill both blanks to break the loop when count reaches 4 and print count.
Java
int count = 0; while (count < 10) { if (count == [1]) { [2]; } System.out.println(count); count++; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using continue instead of break.
Checking the wrong count value.
✗ Incorrect
The loop breaks when count is 4 using the break statement.
5fill in blank
hardFill all three blanks to break the loop when i is 2 and print i.
Java
for (int i = 0; i < 5; i++) { if (i == [1]) { [2]; } System.out.println([3]); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong values for comparison.
Forgetting to print the correct variable.
Using continue instead of break.
✗ Incorrect
The loop breaks when i is 2, and prints the current value of i.