Page 86 - Trackpad_ipro 4.1_Class8
P. 86

The break Statement

                  The break statement forcefully terminates the loop or switch execution within which it lies. It
                  skips the rest of the statements next to the 'break' keyword in the loop and jumps over to the
                  statement following the loop.
                  The syntax of the 'break' statement is as follows:

                  Loop
                  {
                      [statement 1]
                      if(< conditional expression>)

                      {
                      [statement 2]
                      [statement 3]
                      break;
                      [statement 4]
                     }

                  }
                  For example:
                  public class BreakStatement
                  {

                     public static void main(String args[])
                      {
                            int i;
                                for(i=1; i<=10; i++)
                                {
                                       if(i==5)

                                       {
                                               break;
                                       }
                                       else

                                       {
                                               System.out.println("The value of i is: " + i);
                                       }
                                }
                                System.out.println("Loop is terminated");

                      }
                  }
                  In the preceding program, the 'for' loop terminates when the value of variable 'i' becomes 5. This
                  is because the 'break' statement gets executed when the condition given with the ‘if’ keyword
                  becomes true (i == 5), which causes the loop to stop executing and come outside the loop.



                    84     iPro (Ver. 4.1)-VIII
   81   82   83   84   85   86   87   88   89   90   91