Difference between revisions of "Basic Pascal Tutorial/Chapter 3/FOR..DO"

From Lazarus wiki
Jump to navigationJump to search
Line 9: Line 9:
  
 
In Pascal, the fixed repetition loop is the for loop. The general form is:
 
In Pascal, the fixed repetition loop is the for loop. The general form is:
<font color="#006699"><strong>for</strong></font> <font color="#009966"><strong>index</strong></font> <font color="#000000"><strong>:=</strong></font> StartingLow <font color="#006699"><strong>to</strong></font> EndingHigh <font color="#006699"><strong>do</strong></font>
+
<delphi>
  statement<font color="#000000"><strong>;</strong></font>
+
for index := StartingLow to EndingHigh do
 +
  statement;
 +
</delphi>
  
 
The index variable must be of an ordinal data type. You can use the index in calculations within the body of the loop, but you should not change the value of the index. An example of using the index is:
 
The index variable must be of an ordinal data type. You can use the index in calculations within the body of the loop, but you should not change the value of the index. An example of using the index is:
sum <font color="#000000"><strong>:=</strong></font> <font color="#ff0000">0</font><font color="#000000"><strong>;</strong></font>
+
<delphi>
<font color="#006699"><strong>for</strong></font> count <font color="#000000"><strong>:=</strong></font> <font color="#ff0000">1</font> <font color="#006699"><strong>to</strong></font> <font color="#ff0000">100</font> <font color="#006699"><strong>do</strong></font>
+
sum := 0;
  sum <font color="#000000"><strong>:=</strong></font> sum <font color="#000000"><strong>+</strong></font> count<font color="#000000"><strong>;</strong></font>
+
for count := 1 to 100 do
 +
  sum := sum + count;
 +
</delphi>
  
 
The computer would do the sum the long way and still finish it in far less time than it took the mathematician Gauss to do the sum the short way (<tt>1+100 = 101. 2+99 = 101</tt>. See a pattern? There are 100 numbers, so the pattern repeats 50 times. <tt>101*50 = 5050</tt>. This isn't advanced mathematics, its attribution to Gauss is probably apocryphal.).
 
The computer would do the sum the long way and still finish it in far less time than it took the mathematician Gauss to do the sum the short way (<tt>1+100 = 101. 2+99 = 101</tt>. See a pattern? There are 100 numbers, so the pattern repeats 50 times. <tt>101*50 = 5050</tt>. This isn't advanced mathematics, its attribution to Gauss is probably apocryphal.).
  
 
In the <tt>for-to-do</tt> loop, the starting value MUST be lower than the ending value, or the loop will never execute! If you want to count down, you should use the <tt>for-downto-do</tt> loop:
 
In the <tt>for-to-do</tt> loop, the starting value MUST be lower than the ending value, or the loop will never execute! If you want to count down, you should use the <tt>for-downto-do</tt> loop:
<font color="#006699"><strong>for</strong></font> <font color="#009966"><strong>index</strong></font> <font color="#000000"><strong>:=</strong></font> StartingHigh <font color="#006699"><strong>downto</strong></font> EndingLow <font color="#006699"><strong>do</strong></font>
+
<delphi>
  statement<font color="#000000"><strong>;</strong></font>
+
for index := StartingHigh downto EndingLow do
 +
  statement;
 +
</delphi>
  
 
In Pascal, the <tt>for</tt> loop can only count in increments (steps) of 1.
 
In Pascal, the <tt>for</tt> loop can only count in increments (steps) of 1.

Revision as of 16:48, 5 January 2010

3Da - FOR..DO (author: Tao Yue, state: unchanged)

Looping means repeating a statement or compound statement over and over until some condition is met.

There are three types of loops:

  • fixed repetition - only repeats a fixed number of times
  • pretest - tests a Boolean expression, then goes into the loop if TRUE
  • posttest - executes the loop, then tests the Boolean expression

In Pascal, the fixed repetition loop is the for loop. The general form is: <delphi> for index := StartingLow to EndingHigh do

 statement;

</delphi>

The index variable must be of an ordinal data type. You can use the index in calculations within the body of the loop, but you should not change the value of the index. An example of using the index is: <delphi> sum := 0; for count := 1 to 100 do

 sum := sum + count;

</delphi>

The computer would do the sum the long way and still finish it in far less time than it took the mathematician Gauss to do the sum the short way (1+100 = 101. 2+99 = 101. See a pattern? There are 100 numbers, so the pattern repeats 50 times. 101*50 = 5050. This isn't advanced mathematics, its attribution to Gauss is probably apocryphal.).

In the for-to-do loop, the starting value MUST be lower than the ending value, or the loop will never execute! If you want to count down, you should use the for-downto-do loop: <delphi> for index := StartingHigh downto EndingLow do

 statement;

</delphi>

In Pascal, the for loop can only count in increments (steps) of 1.

previous contents next