how to break out of function java code example
Example 1: how to break out for loop java
//Java Program to demonstrate the use of break statement
//inside the for loop.
public class BreakExample {
public static void main(String[] args) {
//using for loop
for(int i=1;i<=10;i++){
if(i==5){
//breaking the loop
break;
}
System.out.println(i);
}
}
}
Example 2: how to interrupt a void java
public void someMethod() {
//... a bunch of code ...
if (someCondition()) {
return; //This exits the method
}
//... otherwise do the following...
}
Example 3: break a function java
public void someMethod() {
//... a bunch of code ...
if (someCondition()) {
return; //break the funtion
}
//... otherwise do the following...
}