Page 87 - iPro_trackGPT_V5_Class8
P. 87

Tech Tweak

                  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, you may want to stop a loop before it finishes all its repetitions. To do this, Java uses
                 jump statements. There are two jump statements: break and continue, that help control the
                 flow within loops.

                 The break Statement

                 The break statement in Java is used to stop a loop or switch statement before it finishes all its
                 steps. When the break statement is used, it immediately stops the loop or switch and moves on
                 to the next part of the program.
                 The syntax of the break statement is as follows:
                                                                                Start
                 Loop
                 {
                     [statement 1]
                     if(< conditional expression>)
                                                                            Conditional
                     {                                                                       True
                                                                           Expression of                 break
                         [statement 2]
                                                                                loop
                         [statement 3]
                         break;                                                     False                  Stop

                         [statement 4]
                    }
                 }
                 For example,

                 public class BreakStatement
                 {
                    public static void main(String args[])
                     {

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

                                      if(i==5)


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