Unconditional Control Statements
2.9.3.1 break Statement
✔ break is a keyword.
✔ It can be used within switch or any looping statements.
✔ It is used to terminate the execution of the current loop/switch statement.
✔ break can be used in two ways:
break;
break <label>;
Lab217.java
class Lab217 {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
System.out.println(i);
if (i == 5) break;
System.out.println("***INSIDE LOOP");
}
System.out.println("OUTSIDE LOOP");
}
}
✔ Output:
1
***INSIDE LOOP
2
***INSIDE LOOP
3
***INSIDE LOOP
4
***INSIDE LOOP
5
OUTSIDE LOOP
Lab218.java
class Lab218 {
public static void main(String[] args) {
int a = 10;
if (a == 10) break;
}
}
Result:
- Compilation Error
breakcannot be used outside loop or switch
Lab219.java
class Lab219 {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.println(i);
break;
System.out.println("***INSIDE LOOP");
}
}
}
Result:
- Compilation Error
- Unreachable statement after
break
Lab220.java
class Lab220 {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println("\n" + i);
for (int j = 1; j <= 5; j++, System.out.print(" " + j)) {
if (i == 3) break;
System.out.print(" JavaWorld " + j);
}
}
}
}
✔ Output (conceptual):
- For
i = 3, inner loop breaks immediately - Other values execute normally
Lab221.java
class Lab221 {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 3; j++) {
if (i == 3) break;
System.out.println(i + "\t" + j);
}
}
}
}
✔ Behavior:
- When
i = 3, only inner loop breaks - Outer loop continues
Lab222.java
class Lab222 {
public static void main(String[] args) {
JavaWorld: for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 3; j++) {
if (i == 3) breakJavaWorld;
System.out.println(i + "\t" + j);
}
}
}
}
✔ Behavior:
- When
i = 3, entire outer loop stops - Because of labeled break
🔑 Key Takeaways
✔ break Usage
- Stops current loop or switch
- Cannot be used standalone outside loops/switch
✔ Unreachable Code
break;
System.out.println("Hello");// error
✔ Normal vs Labeled break
| Type | Behavior |
|---|---|
break; | exits current loop only |
break label; | exits outer loop with label |
✔ Interview Tip ⚠️
breakinside nested loops → only inner loop stops- Use labeled break to exit multiple loops
Lab223.java
class Lab223 {
public static void main(String[] args) {
sd: for (int i = 0; i < 5; i++) {
System.out.println(i);
}
for (int i = 10; i < 15; i++) {
System.out.println(i);
breaksd;
}
}
}
Result:
- Compilation Error
- Label
sdis not associated with this loop - Cannot use
break sd;outside its labeled block
Lab224.java
class Lab224 {
public static void main(String[] args) {
int ab = 10;
JavaWorld: {
System.out.println("JavaWorld BLOCK STARTS");
sd: {
System.out.println("SD BLOCK STARTS");
if (ab == 10) breakJavaWorld;
System.out.println("SD Block ENDS");
}
System.out.println("JavaWorld BLOCK ENDS");
}
System.out.println("Completed");
}
}
✔ Output:
JavaWorld BLOCK STARTS
SD BLOCK STARTS
Completed
Lab225.java
class Lab225 {
public static void main(String[] args) {
int ab = 10;
JavaWorld: {
System.out.println("JavaWorld BLOCK STARTS");
sd: {
System.out.println("SD BLOCK STARTS");
if (ab == 10) breaksd;
System.out.println("SD Block ENDS");
}
System.out.println("JavaWorld BLOCK ENDS");
}
System.out.println("Completed");
}
}
✔ Output:
JavaWorld BLOCK STARTS
SD BLOCK STARTS
JavaWorld BLOCK ENDS
Completed
2.9.3.2 continue Statement
✔ continue is a keyword.
✔ It can be used within any looping statements.
✔ It is used to skip the current iteration and continue with the next iteration.
✔ continue can be used in two ways:
continue;
continue <label>;
🔑 Key Takeaways
✔ Labeled break (Important)
- Can break:
- loops
- blocks (not just loops)
✔ Invalid Case
breaklabel;
If label is not in scope → compile error
✔ break vs continue
| Statement | Behavior |
|---|---|
break | exits loop completely |
continue | skips current iteration |
✔ Interview Insight ⚠️
- Labeled
breakcan even exit non-loop blocks - This is a frequently asked tricky concept
Lab226.java
class Lab226 {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.println(i);
if (i == 5) continue;
System.out.println("***AFTER");
}
}
}
✔ Output:
0
***AFTER
1
***AFTER
2
***AFTER
3
***AFTER
4
***AFTER
5
6
***AFTER
7
***AFTER
8
***AFTER
9
***AFTER
Lab227.java
class Lab227 {
public static void main(String[] args) {
int a = 10;
if (a == 10) continue;
}
}
Result:
- Compilation Error
continuecan be used only inside loops
Lab228.java
class Lab228 {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.println(i);
continue;
System.out.println("***AFTER");
}
}
}
Result:
- Compilation Error
- Unreachable statement after
continue
Lab229.java
class Lab229 {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println("\n" + i);
for (int j = 1; j <= 5; j++, System.out.print(" " + j)) {
if (j == 3) continue;
System.out.print(" JavaWorld " + j);
}
}
}
}
✔ Behavior:
- When
j == 3,continueskips"JavaWorld 3" - Loop continues with next iteration
Lab230.java
class Lab230 {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 3; j++) {
if (i == 3) continue;
System.out.println(i + "\t" + j);
}
}
}
}
✔ Behavior:
- When
i == 3, inner loop iterations are skipped - Outer loop continues normally
Lab231.java
class Lab231 {
public static void main(String[] args) {
JavaWorld: for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 3; j++) {
if (i == 3) continueJavaWorld;
System.out.println(i + "\t" + j);
}
}
}
}
✔ Behavior:
- When
i == 3, control jumps to next iteration of outer loop - Entire inner loop is skipped for that iteration
Lab232.java
class Lab232 {
public static void main(String[] args) {
sd: for (int i = 0; i < 5; i++) {
System.out.println(i);
}
for (int i = 10; i < 15; i++) {
System.out.println(i);
continuesd;
}
}
}
Result:
- Compilation Error
continuemust refer to a valid loop label
Lab233.java
class Lab233 {
public static void main(String[] args) {
int i = 0;
while (i < 10) {
System.out.println(i);
if (i == 3) continue;
i++;
System.out.println("***AFTER");
}
}
}
⚠ Behavior:
- When
i == 3,continueskipsi++ - Causes infinite loop (i stays 3 forever)
🔑 Key Takeaways
✔ continue Rules
- Skips current iteration, moves to next
- Works only inside loops
✔ Unreachable Code
continue;
System.out.println("Hello");// error
✔ Labeled continue
- Jumps to next iteration of labeled loop
✔ Common Bug ⚠️
if(i==3)continue;
i++;
➡ May cause infinite loop