Page 160 - computer science (868) class 11
P. 160

break;
                          }
                      Statements inside loop;
                      increment or decrement;
                  }
                  1st statement after loop;
                For example:
                To find and print whether the given numbers are even numbers or not. The program will terminate if the number
                 entered from the user is 0.

                  import java.util.*;
                  class even
                  {   public static void main()
                      {   Scanner sc= new Scanner(System.in);
                          int n, i, item;
                          System.out.println("Enter number of elements to be checked: ");
                          n=sc.nextInt();
                          for (i=1; i<n; i++)
                          {
                              System.out.println("Enter a number: ");
                              item=sc.nextInt();
                              if(item == 0)
                                  break;
                              if(item%2==0)
                              {   System.out.println(item + " is an even number");    }
                              else
                              {   System.out.println(item + " is not an even number"); }
                          }
                      }
                  }
                The output of the preceding program is as follows:
                Enter the number of elements to be checked: 5
                Enter a number: 12
                12 is an even number
                Enter a number: 230
                230 is an even number
                Enter a number: 45
                45 is not an even number

              •  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;
                               }


                158158  Touchpad Computer Science-XI
   155   156   157   158   159   160   161   162   163   164   165