High-level Programming Language (Pascal)
Repetition Structure
Conditional Loops (While-do Loop and Repeat-until Loop)
¡@while <Boolean expression> do
¡@¡@¡@<statement> ;
can be done by using a while loop in this way :
(1) before the loop, initialize Counter to 0 (i.e. Counter := 0), and
(2) inside the loop, increase Counter by 1 (i.e. Counter := Counter + 1)
can be done by using a while loop in this way :
(1) before the loop, initialize Sum to 0 (i.e. Sum := 0), and
(2) inside the loop, add the number to Sum (i.e. Sum := Sum + Num)
can be done by using a while loop in this way :
- before the loop, initialize Max to a number which is smaller than all numbers in the set (e.g. -9999), and
- inside the loop: if Max < Num then Max := Num
can be done by using a while loop in this way :
- before the loop, initialize Min to a number which is larger than all numbers in the set (e.g. 9999), and
- inside the loop: if Min > Num then Min := Num
¡@repeat
¡@¡@<statement>
¡@until <Boolean expression> ;
validate data entries and prevent unfavorable outcomes, for example
¡@write ('Enter a positive number : ');
¡@repeat
¡@¡@readln (Num);
¡@¡@if Num <= 0 then
¡@¡@writeln ('Invalid ! Please enter a positive number : ')
¡@until Num > 0 ;
offer a list of choices, for example
¡@repeat
¡@¡@writeln ('1. Add Record');
¡@¡@writeln ('2. Modify Record');
¡@¡@writeln ('3. Delete Record');
¡@¡@writeln ('0. Exit');
¡@¡@write ('Please enter your choice (0 - 3) : ');
¡@¡@readln (Choice)
¡@until Choice = 0
continue in running a program, for example
¡@repeat
¡@¡@{ program content expected to be executed }
¡@¡@write ('Do you want to continue (Y/N) ? ');
¡@¡@readln (Choice)
¡@until (Choice = 'N') or (Choice = 'n')
can be done by using a repeat loop in this way :
- before the loop, initialize Counter to 0, and
- inside the loop, increase Counter by 1
can be done by using a repeat loop in this way :
- before the loop, initialize Sum to 0, and
- inside the loop, add the number to Sum
can be done by using a repeat loop in this way :
- before the loop, initialize Max to a number which is smaller than all numbers in the set (e.g. -9999), and
- inside the loop: if Max < Num then Max := Num
Count-controlled Loops (For-to-do Loop and For-downto-do Loop)
for control_variable := initial_value to final_value do
¡@<statement> ;
orfor control_variable := initial_value downto final_value do
¡@<statement> ;
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.