Java Control Statements
- Java if Statement
- Java Nested if Statement
- Java if-else-if Statement
- Java Switch Case
- Java Switch with String
- Java Nested Switch
- Java Simple for Loop
- Java for each Loop
- Java Nested for Loops
- Java while loop
- Java continue Statement with for Loop
- Java continue in do-while Loop
- Java break to Exit a for Loop
- Java break with Nested Loops
This Java example demonstrates how to use a break statement to exit a while loop in Java with an example.
Using the break to Exit a while Loop
package net.javaguides.corejava.controlstatements.loops;
public class BreakWithWhileLoop {
public static void main(String args[]) {
int i = 0;
while (i < 100) {
if (i == 10)
break; // terminate loop if i is 10
System.out.println("i: " + i);
i++;
}
System.out.println("Loop complete.");
}
}
Output:
i: 0
i: 1
i: 2
i: 3
i: 4
i: 5
i: 6
i: 7
i: 8
i: 9
Loop complete.
Control Statements
Java
Java Tutorial
Comments
Post a Comment