Break
public class BreakDemo {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
if (i == 3) {
System.out.println"loop breaks");
break; //exits from for loop
} else {
System.out.println("Value = " + i);
}
}
System.out.println("--------------------------------");
int a = 10;
switch (a) {
case 5:
System.out.println("Value of a is 5");
break; //also used to exit from switch case
case 10:
System.out.println("Value of a is 10");
break;
case 15:
System.out.println"Value of a is 15");
break;
}
}
}
Output:
Value = 0
Value = 1
Value = 2
loop breaks
--------------------------------
Value of a is 10