Way2Java

Java Exit Loop

In control structures loops, in any language, it is required to come out of the loop when the required condition is met. For this, Java Exit Loop uses break keyword like C/C++.
Following program explains Java Exit Loop.
public class Demo
{
  public static void main(String args[])
  {              
                                  // break from for loop
    for(int i = 0; i < 10; i++)
    {
      if(i == 5)
      {
        break;
      }
      System.out.println(i);      // prints 0 to 4
    }
                                  // break from while loop
    int i = 0;
    while(i < 10)
    {
      if(i == 5)
      {
        break;
      }
      System.out.println(i);      // prints 0 to 4
      i++;
   }
                                  // break from do-while loop
    i = 0;
    do
    {
      if(i == 5)
      {
        break;
      }
      System.out.println(i);      // prints 0 to 4
      i++;
    } while(i < 10);
  }
}

Java Exit Loop: break keyword Java makes the control to come out of the loop completely and not from the method. For method use return statement.

To terminate or to come out of the program (application) itself, read Java Exit.

===================Extra Reading=============================

1. Interface extend Interface Java
2. Java Interfaces Tutorial
3. Java Tutorial
4. Java int vs Integer
5. Java Abstract class vs Interface
6. Java Learning