Page 208 - ComputerScience_Class_11
P. 208

•  continue: The continue statement works the same as the break statement, but instead of sending the statement
                 outside the loop or switch, it is used to direct the control by sending it to the next iteration in the loop or switch.
                 All three types of loops can use Java continue statements.
                Syntax:
                  for(initialisation; condition; increment or decrement)
                  {
                      if(condition)
                      {
                          continue;
                               }
                  Statements inside loop;
                  }
                  1st statement after loop;
                For example:

                Program 11    Write a Java program to calculate the average of the first 10 even numbers.
                1       class average

                2       {
                3           public static void main(String[] args)
                4           {

                5               int i, s=0, c=0;
                6               double avg;
                7               for(i=1; i<=10; i++)

                8               {
                9                   if(i%2==1)
                10                  {

                11                      continue;
                12                  }
                13                  s=s+i;
                14                  c++;

                15              }
                16              avg=s/c;
                17              System.out.println("Average of even numbers "+ avg);

                18          }
                19      }

              The output of the preceding program is as follows:

                     BlueJ: Terminal Window - Java
                 Options

                Average of even numbers 6.0





                  206  Touchpad Computer Science (Ver. 3.0)-XI
   203   204   205   206   207   208   209   210   211   212   213