Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Turbo Pascal - <b> for/to for / next loop pp 60</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 FOR/TO                        For / Next Loop                         pp 60


 Define:  Progressive looping in ascending or descending direction.

 Purpose: Repetitive iteration of a loop controlled by a counter.

 Notes:   The For statement indicates that the component statements is
          to be repeatedly executed while a progression of values is
          assigned to a variable which is called the control variable.
          Upon completion of the loop, the control variable will equal the
          final value unless the loop was never executed.

          The progression can be ascending by using To or descending
          by using Downto the final control value.


          Ascending:  if the initial value is greater than the final value
          then the loop won't execute at all.
          Descending: if the initial value is less than the final value
          then the loop won't execute at all.


          The control variable, the initial value, and the final value must
          all be of the same Type.  All simple types are legal except Real.

          Assignments must not be made to the control variable.
          If the repetition is to be terminated before the final value is
          reached, a Goto statement must be executed to leave the loop.
          A better programming practice is to use While or Repeat
          in this circumstance.

          Testing is done at the top of the loop.


 ----------------------------------------------------------------------------


 Usage:
       VAR
          Initial     : Integer            ; { Initial starting value       }
          Control     : Integer            ; { Declare a control variable   }
          Final       : Integer            ; { Final ending value           }

       BEGIN
          Initial     :=  1                ; { Start with 1                 }
          Final       := 10                ; { Execute through 10           }
          For Control := Initial To Final Do { Execute ascending direction  }
            Begin
            Initial   := Initial + 1       ; { Legal to reassign Initial    }
            Final     := Final   - 1       ; { Legal to reassign Final      }
            WriteLn                        ; { Repeat during the loop       }
            End                            ; { End of compound statements   }
                                             { Single statememt For/To loop }
          For Control := Initial Downto Final Do Initial := Initial + 1;
       END.

See Also: Repeat While

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson