What is Break Statement?
break statement stops loop execution immediately.
for(int i=1; i<=5; i++){
if(i == 3){
break;
}
System.out.println(i);
}
Output
1 2
Continue Statement
continue skips current iteration.
for(int i=1; i<=5; i++){
if(i == 3){
continue;
}
System.out.println(i);
}
Output
1 2 4 5
Break in Switch
switch(day){
case 1:
break;
}
Nested Loop Example
for(int i=1; i<=3; i++){
for(int j=1; j<=3; j++){
if(j == 2){
break;
}
System.out.println(j);
}
}
Best Practices
- Use break carefully
- Avoid unnecessary continue
- Keep loops readable