The repeat…until Statement
|
The repeat statement uses the following syntax:
repeat Statement; until Condition
The repeat condition executes a Statement
first. After the first execution of the Statement, it examines the
Condition. If the Condition is true, then it executes the Statement
again. It will keep executing the Statement AS LONG AS the Condition is
true. Once the Condition becomes false, the looping (the execution of
the Statement) would stop.
The Statement of a repeat…until condition can span as many lines as necessary. In this case, although you can, you do not have to start the Statement with the begin keyword and then end it with the end keyword. Another version of the counting program seen previously would be: |
program Project1; {$APPTYPE CONSOLE} var Number: Integer; begin Number := 1; repeat Writeln('Number ', Number); Number := Number + 1; until Number = 15; Write(Chr(10), 'Press any key to continue...'); Readln; end.
The repeat…until conditional statement can be used to insist on
getting a specific value from the user. For example, since our ergonomic
program would like the user to sit down for the subsequent exercise,
you can modify your program to continue only once he is sitting down.
Here is an example on how you would accomplish that:
|
program Project1; {$APPTYPE CONSOLE} var SittingDown: Char; begin Writeln('For the next exercise, you need to be sitting down'); repeat Write('Are you sitting down now(y/n)? '); Readln(SittingDown); until SittingDown = 'y'; Writeln(Chr(10), 'Wonderful!!!'); Write(Chr(10), 'Press any key to continue...'); Readln; end.
Here is an example of running the program:
|
For the next exercise, you need to be sitting down Are you sitting down now(y/n)? d Are you sitting down now(y/n)? k Are you sitting down now(y/n)? n Are you sitting down now(y/n)? y Wonderful!!! Press any key to continue...
The for Statement
|
The for statement is typically used to count a number of items. The
count can be perform in incremental values or in decremental values.
Based on this, one of the syntaxes you can use on the for statement is:
for Counter := InitialValue to FinalValue do Statement;
Using this formula, you can count values from a stating value to an
end. To do this, you must first declare a variable that would hold the
count. In our syntax, such a variable would be the Counter parameter.
The InitialValue is a value assigned to the Counter variable as the
starting value. The to keyword is required to proceed with the counting.
The counting would stop with the value of the FinalValue parameter. The
value of InitialValue should be lower than that of
FinalValue.
The do keyword is required and used to introduce the Statement. After one count, the for loop would execute the Statement. After executing the Statement, the loop would check whether the count has reached FinalValue. If it has not, then the InitialValue would be incremented by 1 and the Statement would be executed again. This checking -> incrementing -> Statement executing would continue until a new incremented value of InitialValue is equal to the value of FinalValue; in which case the loop would stop. Here is an example: |
program Project1; {$APPTYPE CONSOLE} var Number: Integer; begin for Number := 0 to 15 do Writeln('Number ', Number); Write(Chr(10), 'Press any key to continue...'); Readln; end.
This would produce:
|
Number 0 Number 1 Number 2 Number 3 Number 4 Number 5 Number 6 Number 7 Number 8 Number 9 Number 10 Number 11 Number 12 Number 13 Number 14 Number 15 Press any key to continue...
As mentioned, the above for loop is used to count values incrementally. If you want to count value
decrementally, you should use the following syntax:
for Counter := InitialValue downto FinalValue do Statement;
In this case, the loop would start with the
InitialValue and would execute the Statement. The InitialValue should
be greater than of
FinalValue. After this execution, the value of InitialValue would
be compared to that of
FinalValue. If both values are the same, the loop would stop, if
they are not, the value of InitialValue would decremented by 1 and the
Statement would be executed again. This would continue until the
InitialValue and the FinalValue values are the same. Here is an example:
|
program Project1; {$APPTYPE CONSOLE} var Number: Integer; begin for Number := 12 downto 4 do Writeln('Number ', Number); Write(Chr(10), 'Press any key to continue...'); Readln; end.
This would produce:
|
Number 12 Number 11 Number 10 Number 9 Number 8 Number 7 Number 6 Number 5 Number 4 Press any key to continue...
No comments:
Post a Comment