| «‹ July 2026 ›» | | S | M | T | W | T | F | S | | | | 1 | 2 | 3 | 4 | | 5 | 6 | 7 | 8 | 9 | 10 | 11 | | 12 | 13 | 14 | 15 | 16 | 17 | 18 | | 19 | 20 | 21 | 22 | 23 | 24 | 25 | | 26 | 27 | 28 | 29 | 30 | 31 | |
|
2009 年 9 月 11 日 星期五  |
| Repetition Structure |
分類: Programming Language |
High-level Programming Language (Pascal)
Repetition Structure
- makes possible repeated execution of one or more statements until a specified condition is satisfied
- a repetitive technique called looping is used in a repetition structure
- can be implemented in Pascal using a While statement, a Repeat statement or a For statement
Conditional Loops (While-do Loop and Repeat-until Loop)
- it is used when the number of repetition is NOT known in advance
While-do Loop
Count-controlled Loops (For-to-do Loop and For-downto-do Loop)
- it is used when the number of repetition is known in advance
- the loop body will be repeated a specified number of times
- the syntax of the for statement (for-to-do loop and for-downto-do loop)
for control_variable := initial_value to final_value do
<statement> ;
or
for control_variable := initial_value downto final_value do
<statement> ;
- in a for-to loop, no iteration is performed if initial_value > final_value
- similarly,in a for-downto loop, no iteration is done if initial_value < final_value
- the control_variable is assigned the initial_value when a for statement is executed
- the control_variable is automatically updated by the for statement itself
- the statement(s) within a for statement may use the value of the control_variable, but it should not modify this value
- the control variable in a for statement must be declared within the program in which it is used
- the type of control_variable must be ordinal data type (e.g. integer, boolean, char)
- the initial and final values of the control variable are determined before repetitions begins and cannot be changed during execution of the for statement
- the initial_value and the final_value must be compatible in type with the control_variable
- for example
to produce the output 1234554321
the following statements may be used :
var i := integer;
…
for i := 1 to 5 do
write (i);
for i := 5 downto 1 do
write (i);
Sentinel-controlled Loop
- sentinel value is an assigned value that can stop the loop repetition
- whenever this value is entered, the looping will stop immediately
- sentinel value is particularly useful when the number of loop execution is not known in advance
- when the number of data to be inputted is unknown in advance, we can use an unusual datum to indicate the end of data. The is known as the end-of-data indicator technique and this unusual datum is known as sentinel value
Nested Loops
The loop body of a loop may contain any legal statements. In particular, it is possible to place while-do loop, repeat-until loop or for loop in the loop body of another loop. The resulting control structure is a nesting of loops. Nested loops consist of an outer loop with one or inner loops. Each time the outer loop is repeated, the inner loops are reentered, their exit conditions are reevaluated and all required iterations are performed.
|
|