0
0
Javaprogramming~10 mins

Break statement 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 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'
A3
B5
C1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using a value that never occurs in the loop condition.
Forgetting that break exits the loop immediately.
2fill in blank
medium

Complete 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'
A'x'
B'a'
C'd'
D'b'
Attempts:
3 left
💡 Hint
Common Mistakes
Using single quotes incorrectly.
Breaking on the wrong character.
3fill in blank
hard

Fix 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'
A15
B5
C20
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Using a value not in the array.
Confusing the break condition.
4fill in blank
hard

Fill 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'
A4
Bbreak
Ccontinue
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using continue instead of break.
Checking the wrong count value.
5fill in blank
hard

Fill 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'
A3
Bbreak
Ci
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong values for comparison.
Forgetting to print the correct variable.
Using continue instead of break.