Sign Guestbook
View Guestbook
Invite Your Friends
Website Awards
Message Forum
Source Codes
Downloads
Links
About Me
 
     
Lesson 1 - The first few steps in Pascal Programming
Lesson 2 - Variables, Constants and the Assignment  Statement
Lesson 3 - Special Functions: ClrScr(), GotoXy(), etc...
Lesson 4 - Program Control
Lesson 5 - The CASE-OF Statement
Lesson 6 - Logical Operators and Boolean Expressions
Lesson 7 - Procedures and Functions
Lesson 8 - BGI Graphics
Lesson 9 - File Handling
Lesson 10 - Arrays
Lesson 11 - Record Data Structure
Lesson 12 - Strings

Add this site to your Favourites

   



 


Lesson 4 - Program Control

 

This lesson will cover:

 

Now, it is time to learn the most important rules of programming: the if statements - decision making, for loops and the repeat-until loop. Almost, these 3 general programming constructs are common in every programming language and you have to make sure that when you have finished reading this lesson, make sure that you have practiced them enough before continuing with learning pascal because they are of outmost importance. If you fall in doubt about these programming constructs, then try to use the forums, describing your problem.

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 this happens(action), then do this(reaction, if action is true).

OR:

If this happens(action), then do this(reaction, if action is true), else do this(reaction, if action is false).

In Pascal, the 'if statement' should be written as follows:

If conditional expression then code ... ;{if one action}

OR:

If conditional expression then Begin instructions ... End; {if more than one action 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. 

-> If..Then..Else

In a normal if statement, the 'reaction' cannot be performed if the condition is not true. But in an if..then..else statement, there is at least one set of statements to be performed. 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 is false,}
ELSE
 writeln('sorry, you''ve got it wrong!');             {then this is true}

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 conditional statement;

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 {variable}* := {original value} to/downto {final value} do

    {code...(for one action)}

- If used for more than one action:

for {variable}* := {original 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 have an idea of 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.

 
 


User Comments

Post By: 12_inch_hamilton Posted On: 2010-02-22
any ladies looking for me. my name is joel hamilton...i got a 12 inch....subway!!

Post By: Anthony Jules Posted On: 2010-02-22
My accounts students better not be on this site or they will be standing on the chair. You keep standing!!!

Post By: prex Posted On: 2010-01-12
can u help me plz in my program?plz..i need your answer,can you give me an example of program that uses while-do-loop in computing average?plz..

Post By: NgoKimPhu Posted On: 2009-12-22
Correct Lesson4_program1 : Bring the 'Label' after the 'Var' That's all ( _ _!)

Post By: lodo Posted On: 2009-09-28
My Comment is your comment

Post By: meelie Posted On: 2009-09-26
this really helped me with my skool SBA thank yew!

Post By: ashamideAngel Posted On: 2009-08-24
Nice one.....I'm having challenges to write the program stated below: Qns:Write a program to create two-dimentional arrays;your program should store values into the two arrays.The product of the corresponding locations in the two arrays should be found and store in the corresponding location of another array C. Transfer the content of C into A.(i.e overwrite A), and print the contents of A.Please you can send it to my email.

Post By: cuteruth Posted On: 2009-08-04
please give me the solution to this program and kindly help me to forward it to my email address. question- write a pascal program to sum two numbers using reset and rewrite.

Post By: ramsey mnjala Posted On: 2009-07-15
its bla bla bla bla i like it cheers!!!!

Post By: bla ma1 Posted On: 2009-06-30
can you include definations pliz for example for variables n etc

Post By: Ryky Posted On: 2009-06-21
can u give me an example with if...then...else

Post By: lucas Posted On: 2009-06-14
in Lesson4_program1 there are some mistakes, but with a small effort it can be corrected.

Post By: karanu Posted On: 2009-06-04
fantastic

Post By: karanu Posted On: 2009-06-04
could you send me a sample petrol station computerized system.

Post By: sykopyro@hotmail.com Posted On: 2009-06-01
It actuallt is indented, sorry, email me if you want to have a look at it and I will send it to you... As I said Im sorry for spamming........

Post By: Sykopyro@hotmail.com Posted On: 2009-06-01
What am I doing wrong???? Program Test; Var Num1, Num2, Sum : Real; Fn, Ans, Ans2, Ans3 : String; Begin Num1 := '0'; Num2 := '0'; Sum := '0'; Fn := '0'; Ans :='0'; Ans2 := '0'; Ans3 := '0'; WriteIn('Hello, I am Windows Vista calculator!'); WriteIn('Please tell me the first number you want to use'); WriteIn('Number 1:'); ReadIn(Num1); WriteIn('Are you sure? Y/N'); ReadIn(Ans); If Ans = 'Y' then Begin WriteIn('Ok, Now number 2'); Write('Number 2: '); ReadIn('Num2'); Write('Are you sure? Y/N'); ReadIn(Ans3); If Ans3 = 'Y' then goto 31; else goto 24; End. WriteIn('Now choose what you want to do!'); WriteIn('You can choose from +, -, * or /'); ReadIn(Fn); If Fn = '+' then Sum := Num1 + Num2; Write('The answer is: ', Sum); Sum := '0'; Write('Do you want to do another one? Y/N'); ReadIn(Ans2); If Ans2 = 'Y' then goto 15; else Ans2 := '0'; End. Else goto 47; End. End If Fn = '-' then Sum := Num1 - Num2; Write('The answer is: ', Sum); Sum := '0'; Write('Do you want to do another one? Y/N'); ReadIn(Ans2); If Ans2 = 'Y' then goto 15; else Ans2 := '0'; goto 59; End. End. If Fn = '*' then Sum := Num1 * Num2; Write('The answer is: ', Sum); Sum := '0'; Write('Do you want to do another one? Y/N'); ReadIn(Ans2); If Ans2 ='Y' then goto 15; Ans2 := '0'; Else goto 70; End. End If Fn = '/' then Sum := Num1 / Num2; Write('The answer is: ', Sum); Sum := '0'; Write('Do you want to do another one? Y/N'); ReadIn(Ans2); If Ans2 = 'Y' then goto 15; else Ans2 :='0'; goto 1; End. Else WriteIn('You have to type one of the symbols I showed you'); goto 32; End. Else goto 16; ReadIn; End.

Post By: FELLOW PASCALIAN! Posted On: 2009-05-19
don't listen to them they're all nerds!!!! i luv pascal to!!! our weddings next week!

Post By: CHIEF PAU wAU Posted On: 2009-05-19
hey my indian friends, little boob, pixie, shisha,hushed one,and the other two that i cant remember. stay true to your spirit guids for when the eagle of enlightenment flys and the todpole of sexy photos on cathy's bed flicks its tail, who knows what may be...

Post By: nerdica Posted On: 2009-05-19
pascal is my life, my soul, my love, I want to marry it, have kids with it but people tell me i'm weird. Help me fellow pascalians

Post By: reply to le cool Posted On: 2009-05-19
AWESOME DUDE!!!!!

Post By: le cool Posted On: 2009-05-19
hewo i got a tamogotchi!!!!

Post By: needhelp Posted On: 2009-05-06
I need help to make this output: First Problem: 7 7 6 7 6 5 7 6 5 4 7 6 5 4 3 7 6 5 4 3 2 7 6 5 4 3 2 1 Second Problem: 7 6 7 5 6 7 4 5 6 7 3 4 5 6 7 2 3 4 5 6 7 1 2 3 4 5 6 7 If anyone can help me, I'll be very grateful. I'm new in Pascal. Thank you very much.

Post By: xerox009 Posted On: 2009-04-27
this lesson is just simple to learn

Post By: pascal me Posted On: 2009-04-15
I'm findig this website really helpful and interesting, can you please send me an example of a source code of a game please? I really appreciate

Post By: marvambi Posted On: 2009-03-06
It gets more and more interesting the further i read through the articles and programming examples that abound in these modules.

Post By: i own Posted On: 2009-02-23
U SUCK TITS MWAHHAHA FUCKING NERDS!!!

Post By: Riu_anne Posted On: 2009-01-04
it's quite good..

Post By: Sunny Posted On: 2008-12-17
This website really helps me in my homework. Thanks a million times for producing a great site

Post By: angela Posted On: 2008-11-25
this website really helps me in my homework in TLE computer subject... thanx a lot...

Post By: mwafrika Posted On: 2008-11-07
you have tried much in this programming language may God bless your activities

Post By: nazanin Posted On: 2008-10-24
hello; i am a pascal learner and i want to improve my programming.if you project for exercise please send for me, regrad.

Post By: Posted On: 2008-10-15
Your Comment

Post By: Slayer665 Posted On: 2008-06-19
This shit rocks!!!!!!!!!!!!!! yea babe!!!!!~Slayer665~

Post By: Yellow Posted On: 2008-04-08
You are doing a good and great work here. But, could you kindly help to get me implementation and uses of: Tree sort, Merge Sort, Binary sort.

Post By: makpahz Posted On: 2008-01-22
this lesson is very interesting and most especially the if, loop and while do aspects. i really appreciate it.

Post By: capybara Posted On: 2008-01-08
this site really helps me a lot, it is highly recommended. i am impressed.

Post By: Zeinab Abbasimazar Posted On: 2008-01-04
Hello I'm a pascal-learner and I want to ask you a question: "If I use the move procedure in graph unit and move the cursur to a place and then I close graph unit, can I read something from a user in the place mentioned?" After that I want you to give me the source of graph unit and teach me how can I use it(I mean where should I copy it? In the BGI folder?), if you can. I did not have it and I have to use it for my homeworks. Thank you very much, Zeinab

Post By: carcrasher Posted On: 2007-11-20
I am a student and I am now introduced to the for loop and while loop what I don't understand is if you don't have numbers to begin and end the counters and all you know is to calculate and print the sum of N numbers how do you approach it.

Post By: lakes nano Posted On: 2007-11-03
this site is really mavelous, i ve been looking for something like this, i am really impressed. please i would like to be cleard on the use of GOTOXY used in most of the programs i encounter, thanks

Post By: Veunauna Jeomba Posted On: 2007-10-23
i like pascal as a lunguage and i want to master it. please e-mail me some thing (latest on pascal) i can read on this topic thank you... you guys are doing a good job

Post By: Yush Posted On: 2007-08-26
i'm actually stuck on a search procedure...and my teacher told me to use loops for that...but i can't find the proper way of doing it..if anybody knows...please tell me...it's really important...!!!

Post By: Posted On: 2007-06-16
Your Comment

Post By: Jake Posted On: 2007-03-20
He says in the turoial: 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. And, obviously, you can use the If..then statements without gotos. Anyway, great tutorial! Thanks for taking the time to type all this stuff, it's really appreciated :D

Post By: cerinicus Posted On: 2006-08-26
I have always been taught that with a language such as Pascal, the GOTO command was a no-no. That pretty much put a limit on if-then statements, but I think made be a better programmer. I am using these tutorials for refreshers

Post By: naplsee Posted On: 2006-08-08
Your Comment


Post Your Comment on this Article!
Your Name/Nickname:
Your Email:
Your Comment:
Insert the BLUE numbers shown below in the text field on the right. [ This is an anti-spam counter-measure ]
49795052840
 
 
[HOME][LESSON INDEX][INVITE A FRIEND][FORUM][ABOUT ME]

modified last: September 2005 (Article Version: 2)

mail to: victorsaliba@hotmail.com

© Victor John Saliba 2006 
| Privacy Statement | Disclaimer | Contact Us |
 
 
 
www.pascalprogramming.byethost15.com © Victor John Saliba 2006