java break; code example

Example 1: break java

//Conclusion
        break == jump out side the loop
        continue == next loop cycle
        return == return the method/end the method.
          
  for(int i = 0; i < 5; i++) {
      System.out.println(i +"");
      if(i == 3){
        break;
        
      }
    }
  System.out.println("finish!");
/* Output
0
1
2
3
finish!
*/

Example 2: break statement in java

// break statement in java
public class BreakStatementExample
{
   public static void main(String[] args) 
   {
      for(int a = 1; a <= 10; a++)
      {
         if(a == 3)
         {  
            // breaking loop  
            break;  
         }  
         System.out.println(a);  
      }
   }
}

Example 3: break a function java

public void someMethod() {
    //... a bunch of code ...
    if (someCondition()) {
        return; //break the funtion
    }
    //... otherwise do the following...
}

Tags:

Java Example