The IF Statement
The 'if statement' executes a the proceeding statement(s) conditionally. This means that if an action comes to be true, then the statement(s) proceeding the if statement are executed, else these statements are skipped. It works like this:
If some <condition> happens, then do this (reaction, if condition is true).
OR:
If some <condition> happens, then
do this (reaction, if condition is true),
else do this (reaction, if condition is false).
In Pascal, the 'if statement' should be written using the following syntax:
If conditional expression then
code ... ;{if one action}
OR:
If <conditional expression> then
Begin <instructions> ...
End; {if more than one instruction is required}
Note that you should not use an assignment statement in the 'if' construct, otherwise the compiler will raise a syntax error. I.e.:
Wrong:
If x := 20 then x := x + 1; {the underlined character must be excluded}
Correct:
If x = 20 then x := x + 1; {only an equal sign is used for comparison}
A program is shown below as an example of how the 'if statement' works:
Program lesson4_Program1;
Uses Crt;
Label 1; {this is used with a goto statement}
Var
Sel: String;
N1,N2, Total : Real;
YN : Char; {this is a character variable type, which holds single characters ONLY}
Begin
1:Clrscr;
Total := 0; {always initialise integer/real variables}
GotoXy(4,3);
Writeln('1.Addition');
GotoXy(4,4);
Writeln('2.Subtraction');
GotoXy(4,5);
Writeln('3.Exit');
GotoXy(6,8);
Write('Select: ');
Sel := Readkey;
If Sel = '1' {action} Then
Begin {more than one statement}
ClrScr;
Write('Input No.1:');
Readln(N1);
Write('Input No.2:');
Readln(N2);
Total := N1 + N2;
Writeln('Addition: ',N1:2:3,' + ',N2:2:3,' = ',Total:2:3);
Write('Press any key to continue...');
Readkey; Goto 1;{this leads back to the beginning of the program, otherwise the program terminates} End; {Closing the if statement(begin)}
If Sel = '2' Then {note that the assignment statement is not used within an if statement}
Begin
ClrScr;
Write('Input No.1:');
Readln(N1);
Write('Input No.2:');
Readln(N2);
Total := N1 - N2;
Write('Subtraction: ');
Write(N1:2:3,' - ',N2:2:3,' = ',Total:2:3);
Write('Press any key to continue...');
Readkey;
Goto 1;
End; {Closing the if statement}
If Sel = '3' Then
Begin
ClrScr;
Write('Are you sure?(Y/N)');
YN := Readkey;
If YN = 'y' Then
Halt; {1 action, so no need of Begin..End}
If YN = 'n' Then
Goto 1; {the goto statement is not recommended for excessive use}
End;
End.
In the above program, the 'goto' statement is used. So far, it has been a real damage to programs and it has produced unwanted confusions. I strongly suggest you not to use it repeatedly (or better yet not at all).
If ..Then .. Else
Normally, the instructions following the if statement cannot be executed if the condition is not true. But in an if..then..else statement, there is at least one set of statements to be executed. Let's take a look at the example below:
Writeln('Who has discovered the land of America?');
Readln(ans);
If (ans = 'Christopher Colombus') Then
score := score + 1 { if this does not execute, }
Else
Writeln('sorry, you''ve got it wrong!'); { then this executes }
Note that if the 'else' term is included with an if statement, then there should be no semi-colon before the 'else' term; just as seen in the above example.
Nested If Statements
The previous program have already shown an example of nested if statements. I.e.:
If Sel = '3' then
Begin
ClrScr;
Write('Are you sure?(Y/N)');
YN := Readkey;
If YN = 'y' Then
HALT; {Nested if statement}
If YN = 'n' Then
Goto 1; {Another Nested if statement}
End;
A nested if statement, is in the form:
If (this happens) then {if 1}
If (this happens) then {if 2}
(do this) etc...
Else (do this) {if 2}
Else (do this) etc... {if 1}
A nested if statement is an if statement within another if statement, as shown above.
The Repeat-Until Loop
This loop is used to repeat the execution of a set of instructions for at least one time. It is repeated until the conditional expression is obeyed. The following example, shows the model of the 'repeat-until' loop:
Repeat
...(code)
...(code)
...(code)
Until <some conditional statement is true>;
Here's an example:
Uses Crt; Var YN : String; Begin Writeln('Y(YES) or N(NO)?'); Repeat {repeat the code for at least one time} YN := Readkey; If YN = 'y' Then Halt; {Halt - exit} If YN = 'n' Then Writeln('Why not? Exiting...'); Delay(1800); { wait a second plus 800 milliseconds } Until (YN = 'y') OR (YN = 'n'); End.
See? It's very simple! In the above program, there is a Boolean expression in the 10th line (or). This will be described later on.
The For Loop
The for loop is a sort of repeat-until loop. The for loop, repeats a set of instructions for a number of times. The for loop is in the form:
- If used for only one action:
for {counter (variable)}* := {initial value} to/downto {final value} do
{code...(for one instruction)}
- If used for more than one instruction:
for {counter (variable)}* := {initial value} to/downto {final value} do Begin
{code...}
{code...}
End;
*Generally, this variable is called the 'loop counter'.
Now, an example of the for loop is shown below, but firstly, you should appreciate the usefulness of the for loop. Consider the following example:
Without for loop:
Program lesson4_Program2a; Uses Crt; Begin Writeln('for loop'); {somewhat boring writing all this!!!} Writeln('for loop'); Writeln('for loop'); Writeln('for loop'); Writeln('for loop'); Writeln('for loop'); Writeln('for loop'); Readln; End.
With for loop:
Program lesson4_Program2b; Uses Crt; Var Counter : Integer; {loop counter declared as integer} Begin For Counter := 1 to 7 do {it's easy and fast!} Writeln('for loop'); Readln; End.
Note that the two programs above perform the same function, but which programming style is more useful?
Suppose we have to make a program which designs a small box with some of the characters of the ASCII, obviously the characters which are most likely to make up a simple box.
Without the for loop:
Program Program3a_lesson4; Uses Crt; Begin Gotoxy(25,5); Writeln('+'); Gotoxy(25,6); Writeln('I'); GotoXy(25,7); Writeln('I'); GotoXy(25,8); Writeln('I'); GotoXy(25,9); Writeln('I'); GotoXy(25,10); Writeln('I'); GotoXy(25,11); Writeln('+'); GotoXy(26,11); Writeln('-'); GotoXy(27,11); Writeln('-'); GotoXy(28,11); Writeln('-'); GotoXy(29,11); Writeln('-'); GotoXy(30,11); Writeln('-'); GotoXy(31,11); Writeln('-'); GotoXy(32,5); Writeln('+'); Gotoxy(32,6); Writeln('I'); GotoXy(32,7); Writeln('I'); GotoXy(32,8); Writeln('I'); GotoXy(32,9); Writeln('I'); GotoXy(32,10); Writeln('I'); GotoXy(32,5); Writeln('+'); GotoXy(26,5); Writeln('-'); GotoXy(27,5); Writeln('-'); GotoXy(28,5); Writeln('-'); GotoXy(29,5); Writeln('-'); GotoXy(30,5); Writeln('-'); GotoXy(31,5); Writeln('-'); {oh my God!!! Phew!} Readln; { wait for user to read } End.
With for loop:
Program Program3b_lesson4; Uses Crt; Var Counter : Integer; {loop counter} Begin For Counter := 1 to 5 do Begin gotoxy(25, 5 + Counter); Writeln('I'); End; For Counter := 5 Downto 1 do Begin {an example of 'downto' instead of 'to', note the 'gotoxy(_,_)'} gotoxy(32, 11 - Counter); Writeln('I'); End; For Counter := 1 to 6 do Begin gotoxy(25 + Counter, 11); Writeln('-'); End; For Counter := 6 Downto 1 do Begin gotoxy(32 - Counter, 5); Writeln('-'); End; {--------------The Corners(+)---------------} Gotoxy(25,5); Writeln('+'); GotoXy(25,11); Writeln('+'); GotoXy(32,5); Writeln('+'); GotoXy(32,11); Writeln('+'); GotoXy(45,7); Writeln('Just as simple as the for loop!!!'); Readln; End.
Again, the two programs above perform the same function.
Nested for loops
A nested for loop is similar to that of the nested if statements. A nested for loop is in the form:
for {loop counter} := {original value} to {final value} do {Begin-if required}
{code if any..begin should be included (i.e more than one action)}
for {loop counter} := {original value} to {final value} do {Begin-if required}
{code..if more than one action, include begin in the second for loop}
{End; - if begin is included in the second for loop)}
{code if any..begin should be included in the first for loop}
{End; - if begin is included in the first for loop)}
The nested for loop is rarely used and it may cause problems.
While-Do Loop
This type of loop is executed 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:
While <condition is true> do the following:
instruction 1;
instruction 2;
instruction 3;
etc...
End; {If while-do loop starts with a begin statement}
Example Program on the While-Do loop:
Program Lesson4_Program4; Uses Crt; Var Ch : Char; Begin Writeln('Press ''q'' to exit...'); Ch := Readkey; While Ch <> 'q' do Begin Writeln('I told you press ''q'' to exit!!'); Ch := Readkey; End; End.