|
Counting and Looping |
|
The Object Pascal language provides a set of control statements that allows you to conditionally control data input and output. These controls are referred to as loops. A loop is used to execute a statement over and over again, checking a certain condition. The loop executes as long as the condition is true. Once the condition becomes
false, the loop stops. |
The while statement examines or evaluates a condition. The syntax of the while statement is:
while Condition do Statement;
To execute this expression, the compiler first examines the Condition. If the Condition is false, the Statement will not (will never) execute. If the Condition is true, then it executes the Statement. After executing the Statement, the Condition is checked again. AS LONG AS the Condition is true, the compiler will keep executing the Statement. When, or once, the Condition becomes false, it exits the loop.
Here is an example: |
var
Number: Integer;
begin
while Number <= 5 do
Writeln('Number ', Number);
end.
As done for all the other conditional statements, if the statement spans more than one line, it should be enclosed between a begin and an end keywords.
To effectively execute a while condition, you should make sure you provide a mechanism for the compiler to get a reference value for the condition, variable, or expression being checked. This is sometimes in the form of a variable being initialized although it could be some other expression. Such a while condition could be illustrated as follows: |
program Project1;
{$APPTYPE CONSOLE}
var
Number: Integer;
begin
Number := 0;
while Number <= 15 do
begin
Writeln('Number ', Number);
Number := Number + 1;
end;
Write(Chr(10), 'Press any key to continue...');
Readln;
end.
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
|
|
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 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.
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.
Number 12
Number 11
Number 10
Number 9
Number 8
Number 7
Number 6
Number 5
Number 4
Press any key to continue...