Page 87 - iprime_V2.2_class8
P. 87

for(i=1; i<=10; i++)

                               {
                                      if(i==5)
                                      {
                                             break;

                                      }
                                      else
                                      {                                                             Output

                                             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.
                 The continue Statement

                 Unlike the 'break' statement, the continue statement forces the next iteration of the loop to take
                 place and skips the current iteration.

                 For example:
                 public class ContinueStatement

                 {
                    public static void main(String args[])

                     {

                           int i;
                               for(i=1; i<=10; i++)

                               {
                                      if(i==5)

                                      {                                                           Output

                                             System.out.println("Loop is continued");
                                             continue;

                                      }

                                      else
                                      {



                                                                  Conditional, Loop and Jump Statements in Java    85
   82   83   84   85   86   87   88   89   90   91   92