break
The break Keyword is used to get out of the loop or break the given loop when the specific condition is true.
Example:
package quipohouse;
public class BreakStatement {
public static void main(String[] args) {
int i;
for(i=0;i<5;i++)
{
if(i==3)
{
break;
}
System.out.println(i);
}
}
}
Output:
0
1
2
Key Point
- Stops loop execution when the condition is true.
- Exits only the innermost loop when nested loops are used
Explanation:
1. The loop runs from i = 0 to i < 5.
2. When i == 3, the break statement is triggered.
3. The loop stops execution and exits immediately.