Page 86 - iPro_trackGPT_V5_Class8
P. 86

If  we  change  the  condition  used  in the  preceding
                  program from (i <= 10) to (i >= 10), the do-while loop
                  executes the statements only once and stops executing.
                  However,  the condition is  false. The output will  be

                  displayed as shown:

                  The for Loop

                  The  for  loop  in  Java  is  a  control  flow                 Start
                  statement  used  to  execute  a  block  of
                  code  a  specific  number  of  times.  It  is
                                                                              Initialisation
                  particularly  useful when  the  number
                  of  iterations  is  known  beforehand.  The
                  for  loop  consists of  three  main parts:
                  initialisation, condition,  and  iteration          False   Conditional
                  expression.                                                  Expression
                  The syntax of the for  loop is as
                                                                                                Increment/Decrement
                  follows:                                                           True
                  for (initialisation; conditional                          Body of for loop
                  expression; increment/decrement)

                  {
                                                                  Stop
                  [statements]
                  }
                  In a for loop, the initialisation section sets up a variable when the loop starts. This happens only
                  once and helps control the loop. It ends with a semicolon. Next, the loop checks a condition to
                  decide if it should keep running. The loop's instructions keep running as long as the condition
                  is true. Finally, the loop updates the variable, which controls how many times it repeats.

                  For example,
                  public class ForLoop
                  {
                      public static void main(String args[])
                      {
                            int i;
                                for(i=1; i<=10; i++)
                                {
                                       System.out.println("TrackGPT");
                                }
                      }
                  }
                  The preceding program prints the word ‘TrackGPT’ ten times.






                    84    TrackGPT iPRO (V5.0)-VIII
   81   82   83   84   85   86   87   88   89   90   91