break statement in Java
break statement is used to break or stop the flow of execution in a program.
break can be only used in looping and switch statements.
break;
Example
Imagine you are a gymnast. You are taking 100 push-ups. You are stuck in-between because all your energy got lost i.e tired, so you are coming out from pushups.
What you will do to solve the above using programming ?
public class BreakTest {
public static void main(String args[]) {
int energy_lost=0,energy_you_have=25,pushups_to_do=100;
int energy_per_pushup=5;
int pushups_taken=0;
for(int i=1;i<=pushups_to_do;i++){
energy_lost=energy_lost+energy_per_pushup;
pushups_taken++;
if(energy_lost>=energy_you_have)
break;
}
System.out.println("Push-ups taken is "+pushups_taken);
}
}
Try In EditorOutput
Push-ups taken is 5