Difference between revisions of "For"
Line 1: | Line 1: | ||
[[Keyword|keyword]] used with "[[to]]"\"[[downto]]" and "[[Do|do]]" to execute a loop in which the value of a control variable is incremented or decremented by 1 each time: | [[Keyword|keyword]] used with "[[to]]"\"[[downto]]" and "[[Do|do]]" to execute a loop in which the value of a control variable is incremented or decremented by 1 each time: | ||
− | FOR ''control_variable'' := ''start'' to ''final'' | + | FOR ''control_variable'' := ''start'' [[to|TO]] ''final'' DO ''statement'' |
which increases ''control_variable'' by 1 on each execution of the loop until the value is greater than or equal to ''final'' | which increases ''control_variable'' by 1 on each execution of the loop until the value is greater than or equal to ''final'' | ||
or | or | ||
− | + | FOR ''control_variable'' := ''start'' [[downto|DOWNTO]] ''final'' do ''statement'' | |
which decreases ''control_variable'' by 1 on each execution of the loop until the value is less than or equal to ''final'' | which decreases ''control_variable'' by 1 on each execution of the loop until the value is less than or equal to ''final'' | ||
Line 10: | Line 10: | ||
<delphi> | <delphi> | ||
− | + | For I:=1 To 100 Do statement; | |
</delphi> | </delphi> | ||
(repeats ''statement'' one hundred times, increasing I from 1 to 100) | (repeats ''statement'' one hundred times, increasing I from 1 to 100) | ||
Line 19: | Line 19: | ||
(repeats ''statement'' one hundred times, decreasing I from 100 to 1) | (repeats ''statement'' one hundred times, decreasing I from 100 to 1) | ||
− | * FOR will loop only the single statement that follows it. To execute more than one statement, enclose the group of statements in a [[Begin]]/[[End]] block. | + | * FOR will loop through only the single statement that follows it. To execute more than one statement, enclose the group of statements in a [[Begin]]/[[End]] block. |
* In a for..to loop, if ''start'' is greater than ''final'', the loop is not executed | * In a for..to loop, if ''start'' is greater than ''final'', the loop is not executed | ||
* in a for..downto loop, if ''start'' is less than ''final'', the loop is not executed | * in a for..downto loop, if ''start'' is less than ''final'', the loop is not executed |
Revision as of 22:20, 26 October 2010
keyword used with "to"\"downto" and "do" to execute a loop in which the value of a control variable is incremented or decremented by 1 each time:
FOR control_variable := start TO final DO statement
which increases control_variable by 1 on each execution of the loop until the value is greater than or equal to final or
FOR control_variable := start DOWNTO final do statement
which decreases control_variable by 1 on each execution of the loop until the value is less than or equal to final
Where control_variable is the variable to be set to the value of start. The control variable is increased or decreased by 1 on each loop until its value reaches or exceeds final.
<delphi> For I:=1 To 100 Do statement; </delphi> (repeats statement one hundred times, increasing I from 1 to 100)
<delphi>
for I:=100 downto 1 do statement;
</delphi> (repeats statement one hundred times, decreasing I from 100 to 1)
- FOR will loop through only the single statement that follows it. To execute more than one statement, enclose the group of statements in a Begin/End block.
- In a for..to loop, if start is greater than final, the loop is not executed
- in a for..downto loop, if start is less than final, the loop is not executed
After the loop, the value of control_variable will be final. If the loop was not executed, the value of control_variable does not change.
- you can use types instead of numbers.
Keywords: begin — do — else — end — for — if — repeat — then — until — while