Difference between revisions of "Basic Pascal Tutorial/Chapter 3/FOR..DO"
(The introduction does not discuss for..do loops at all, just general looping. This is confusing) |
m (bypass language bar/categorization template redirect [cf. discussion]) |
||
(10 intermediate revisions by 6 users not shown) | |||
Line 1: | Line 1: | ||
− | {{FOR..DO}} | + | {{Basic Pascal Tutorial/Chapter 3/FOR..DO}} |
+ | {{TYNavigator|Chapter 3/CASE|Chapter 3/WHILE..DO}} | ||
− | == FOR...DO Loops == | + | == FOR...DO Loops (author: Tao Yue, state: changed) == |
− | FOR...DO is a loop construct in Pascal. | + | FOR...DO is a loop construct in Pascal. Of course, that may raise the question "What is a loop?". |
=== Loops === | === Loops === | ||
Line 15: | Line 16: | ||
=== FOR...DO Loop === | === FOR...DO Loop === | ||
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: | ||
− | <syntaxhighlight> | + | <syntaxhighlight lang=pascal> |
for index := StartingLow to EndingHigh do | for index := StartingLow to EndingHigh do | ||
statement; | statement; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | The index variable must be of an '''ordinal''' data. The index can be used in calculations within the body of the loop, but its value cannot be changed (i.e. <tt>count:=5</tt> will cause a program exception. | + | The index variable must be of an '''ordinal''' data. The index can be used in calculations within the body of the loop, but its value cannot be changed (i.e. <tt>count:=5</tt> will cause a program exception.) |
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. | ||
A loop can be interrupted using the ''break'' statement. | A loop can be interrupted using the ''break'' statement. | ||
An example of using the index is: | An example of using the index is: | ||
− | <syntaxhighlight> | + | <syntaxhighlight lang=pascal> |
sum := 0; | sum := 0; | ||
for count := 1 to 100 do | for count := 1 to 100 do | ||
begin | begin | ||
sum := sum + count; | sum := sum + count; | ||
− | if | + | if sum = 38 then break; |
end; | end; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 37: | Line 38: | ||
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: | ||
− | <syntaxhighlight> | + | <syntaxhighlight lang=pascal> |
for index := StartingHigh downto EndingLow do | for index := StartingHigh downto EndingLow do | ||
statement; | statement; | ||
Line 43: | Line 44: | ||
== See also == | == See also == | ||
− | [[WHILE..DO|While ...Do loops]] | + | [[Basic Pascal Tutorial/Chapter 3/WHILE..DO|While ...Do loops]] |
[[Until|Repeat... Until loops]] | [[Until|Repeat... Until loops]] | ||
Line 49: | Line 50: | ||
[[for-in_loop|For... in loops]] | [[for-in_loop|For... in loops]] | ||
− | + | {{TYNavigator|Chapter 3/CASE|Chapter 3/WHILE..DO}} | |
− | {| | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− |
Latest revision as of 16:19, 20 August 2022
│
български (bg) │
English (en) │
français (fr) │
日本語 (ja) │
中文(中国大陆) (zh_CN) │
FOR...DO Loops (author: Tao Yue, state: changed)
FOR...DO is a loop construct in Pascal. Of course, that may raise the question "What is a loop?".
Loops
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
FOR...DO Loop
In Pascal, the fixed repetition loop is the for loop. The general form is:
for index := StartingLow to EndingHigh do
statement;
The index variable must be of an ordinal data. The index can be used in calculations within the body of the loop, but its value cannot be changed (i.e. count:=5 will cause a program exception.)
In Pascal, the for loop can only count in increments (steps) of 1. A loop can be interrupted using the break statement. An example of using the index is:
sum := 0;
for count := 1 to 100 do
begin
sum := sum + count;
if sum = 38 then break;
end;
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:
for index := StartingHigh downto EndingLow do
statement;