Page 86 - iprime_V2.2_class8
P. 86

Tech Funda



                         The for-each loop in Java is a simpler way to iterate over elements of an array or a
                         collection, like an ArrayList. It is also known as an enhanced for loop. The syntax of for-
                         each loop is as follows:

                         for (dataType item : collection)

                         {
                             // body of loop

                         }





                      JUMP STATEMENTS

                  Sometimes,  it is  necessary to transfer  control out of the loop body before all iterations are
                  completed. For this purpose, jump statements are used in Java. Java offers two jump statements—
                  break and continue—which are used within the loops.
                  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;


                  84     iPRIME (Ver. 2.2)–VIII
   81   82   83   84   85   86   87   88   89   90   91