Page 184 - Cs_withBlue_J_C11_Flipbook
P. 184

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


                182182  Touchpad Computer Science-XI
   179   180   181   182   183   184   185   186   187   188   189