According to Miklos Cserzo:
"for" is reserved word in all Pascal standards. It is used for introducing
^ a
iterative loops during the execution. At the start of the loop the control
^^^^^^^^^^^^^^^^^^^^ (When else?;-)
variable set to the initial value then incremented by 1 after each
^^^^^^^^^^^^^^^^ Here I am missing something like "with `to'".
execution step of the body of the "do" statement.
I wouldn't look upon this one as a separate "do statement". `do' is part of the `for' construction, and the iterated statement is best referred to by "the statement following `do'".
The control variable should be an integer type.
Nope. It must be of *ordinal* type. With extended syntax enabled, GPC also allows pointer types here.
It must be declared in the most inner enclosing block. According to BP, GPC also allows global variables here, or (as a GPC extension) any other lvalue.
When the value of the control variable equals to the final value the execution continues at the next statement after the body of the "do".
... of the "for statement" (see above).
If the initial value and the final value are equal the loop executed ones. If the initial value higher than the final value the body of the "do" statement not executed.
^ is
In the second form of the command
Better refer to it via `downto' instead of counting the forms we are describing.
the control variable decremented by 1 in each cycle. The relation of the initial and final values are reversed accordingly.
The body of the "do" statement can contain other "for" cycles resulting nested loops.
... can contain any valid Pascal statement including other "for" cycles ...
It is an error to set the value of the loop-control variable inside the loop. The code that violate that rule will compile and run - if there are no other errors - but the result will be unpredictable.
After the execution of the loop the value of the control variable is undefined.
While this is stated in ISO-10206 I have seen some ISO-7185 test programs that require the value to be <final_value>. Somebody knows definitly?
A proper code should not relay on that value.
STANDARDS: All
... except for the scope the control variable must be defined in.
EXAMPLE:
var i,j,k:integer; vec:array[1..10] of integer; cube:array[1..10.1..10.1..10] of real;
^ ^ , ,
I'd prefer an indentation like
var i,j,k:integer; vec:array[1..10] of integer; cube:array[1..10.1..10.1..10] of real;
My own way to write this would be
Var i, j, k: Integer; Vec: array [ 1..10 ] of Integer; Cube: array [ 1..10, 1..10, 1..10 ] of Real;
... but this is a matter of taste, of course.
... for i:= 1 to 10 do begin write('element [',i,']:? '); { prompt for the next value } readln(vec[i]); { read it } end;
Same here. My way to write this would have `begin' and `end' on a line of its own. In this case we have the GNU coding standards requiring the same for the braces in C, so we should use that convention.
As a compromise, I suggest
for i:= 1 to 10 do begin write('element [',i,']:? '); { prompt for the next value } readln(vec[i]); { read it } end;
while my own way would be
for i:= 1 to 10 do begin write ( 'element [ ',i,' ]: ' ); (* prompt for the next value *) readln ( vec [ i ] ); (* read it *) end (* for *);
....
Better only three dots, consistently.
In other examples, I have written:
[...]
What looks better?
[...]
There is one more important syntax for `for': set member iteration
for Ch in [ 'A'..'Z', 'a'..'z' ] do [...];
which is defined in ISO-10206 Extended Pascal only.
(You see: The "Standards" section is taking over ... ;-)
(You also see why I am so slow in writing documentation. ;-)-:
Greetings and thanks,
Peter