A step-by-step Pascal tutorial for beginners.
Lesson 4: Program Flow Control (If Statement, Repeat-Until & For Loop)
Read the lessons FREE & OFFLINE from the convenience of your phone.
Up till now, we have based our programs on the most simple control structure: Sequence. This means that all of our programs so far were made up of sequential instructions executed one after the other. Basing such programming on such a simple control structure, would not be possible for complex programs to be developed. With only sequential instructions, it won’t be possible to enable decision making in code or code repetition.
Thus, it’s now the time to learn the most important branches of programming: the if statements – decision making, for loops – iterations and the repeat-until loop – iterations. These 2 general programming control structures are common in almost every programming language that exists (procedural, event-driven/object oriented programming). Make sure that when you have finished reading this lesson, you have practised them enough before continuing to learn Pascal.
The IF Statement
The If statement executes the subsequent statement(s) conditionally. This means that if a condition evaluates to true, then the statement(s) following the if statement are executed, otherwise these statements are skipped and will not be executed. It works like this:
If <condition evaluates to true> then <execute these statements>,
<execute these other statements>
Therefore, in Pascal the ‘if statement’ should be written using the following syntax:
< use Begin .. End if more than one instruction is required >
Conditional expressions are used a lot in programming. They are always evaluated to a boolean expression, being either True or False. In the if statement, when a conditional expression is evaluated to True, the subsequent commands attached to the if statment are executed. A conditional expression can be a simple comparison between a variable and a value. If they are equal, then the expression evaluates to True, False otherwise.
In Pascal, in order to compare two values, you should not use an assignment statement in the ‘if’ construct, otherwise the compiler will signal a syntax error. A single equal sign is used instead. Let us see how they are used:
A program is shown below as an example of how the ‘if statement’ works:
This program is demonstrating the use of If statements by asking the user to select an arithemtic operation to be applied to two numbers. The Readkey registers a single character instantly and three if statments are used to evaluate the choice of the user. Notice how the variable Sel is compared with corresponding values. Since variable Sel is a character, then a character should also be compared. The data types of both values on either side should be identical, otherwise the compiler won’t allow it.
Also, note that in the above program, the Goto statement is used. Use of the Goto statement is strongly disencouraged because it promotes bad programming and goes against structured programming principles. It was used here for example purposes only..
Besides the use of conditional statements in this program, there is something else of interest. We have trimmed floating point/real numbers for the first time. Notice how the line:
Writeln(‘Addition: ‘,N1:2:3,’ + ‘,N2:2:3,’ = ‘,Total:2:3);
has got its Real variables been formatted. N1:2:3 will represent the floating point value of N1 with at lease 2 positions and 3 precisions (numbers after the decimal point). It is often very useful to use this formatting when representing Real values, otherwise they will be displayed in scientific format and that is not what you want your users to see unless specifically required.
If ..Then .. Else
As you know, the instructions which follow the if statement won’t be executed if the condition does not evaluate to true. We are going to extend the If statement such that at least one branch will be executed, depending on the outcome of the conditional expression. In an if..then..else statement, there is at least one set of statements that will be executed.
Let’s take a look at the example below:
Note that if the ‘else’ keyword is included with an if statement, then there should be no semi-colon before the ‘else’ keyword; as shown in the example above.
Nested If Statements
The previous program has already shown an example of nested if statements.
It is usually found in the form:
A nested if statement is an if statement within another if statement, as shown above. There can be multiple if statements subsequently embedded within each other. It is recommended to use the block statement enclosures (i.e. Begin .. End) to avoid confusions when using nested if statements.
The Repeat-Until Loop
This type of loop is used to repeat the execution of a set of instructions for at least one time. It is repeated until the conditional expression evaluates to true, that is, keeps looping while the condition remains false. Once the loop condition becomes true, the loop ends.
The following example, shows the model of the ‘repeat-until’ loop:
Here’s an example:
See? It’s very simple! In the above program, there is a boolean operator in the 10th line (or). This will be described later on, although one can still get the meaning of that expression. The conditional expression caters for both uppercase and lowercase inputs.
The For Loop
The For loop executes a set of instructions for a specified number of times. In such kind of loop, a counter is always used to count the number of iterations that have been executed so far. Once they have all been executed, the for loop terminates.
The for loop is in the following form:
• When used with one statement
• If used for more than one statement
* This variable is called the ‘loop counter‘.
Now, let us analyse the usage of the for loop. In order to appreciate what this loop do for us, I have created two programs which produce the same output but one uses the for loop and the other does not.
Without for loop: With for loop:
Note that the two programs above perform the same function, but which programming style is more adequate?
Suppose we have to make a program which displays a small box made of the ASCII characters. We shall obviously choose the characters which mostly
resemble the edges and corners of a box.
Without the for loop:
Again, the two programs above perform the same function. However, one can notice the difference between the programming style used in both programs. Once you master the for loop, a lot can be achieved with it!
Nested for loops
A nested for loop is similar to that of nested if statements. A nested for loop is in the followingform:
Although nested for loops are rarely used, they can be useful in certain situations but they may cause problems if designed properly.
This type of loop is executes the enclosed code while the condition is true. It is different from the ‘Repeat-Until’ loop since the loop might not be executed for at least one time. The code works like this:
Example Program using the While-Do loop:
The program above is quite straightforward. It asks the user to hit the ‘q’ button to exit. If other keys are pressed it continues repeating until the ‘q’ key is pressed. Situations like these are often useful in complex programs and using the while loops help us avoid the use of the goto statement.