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 5 - The CASE-OF Statement

*You should have an idea of the 'if statement' before you proceed with this lesson.

The Simple Case Statement

So far, you have learned how to use an 'if statement'. But in some cases the 'case statement' is preferred to the if statement because it reduces some unnecessary code but the same meaning is retained. The case statement is very similar to the if statement, except in that the it does not accept literal conditional expressions (i.e.: strings) but surprisingly enough, it allows single character conditional expressions. Here is how it works:

Case {variable of type: integer or character ONLY} of

         {input statement- within inverted commas if of type char} : {code..}

         {input statement- within inverted commas if of type char} : {code..}

    ...

End;  {End Case}

Now you should note the difference and the intelligent use of the case statement over the if statement.

The Program is written using the if statement:

Program Program1a_Lesson5;
Uses Crt;
Label Return;  {used respectively with the 
                goto statement; beware of it}

Var SEL : Integer;
    YN : Char;
           
Begin
 Return: Clrscr;
 Writeln('[1].PLAY GAME');
 WRITELN('[2].LOAD GAME');
 WRITELN('[3].MULTIPLAYER');
 WRITELN('[4].EXIT GAME');
 Writeln('note: Do note press anything except');
 Writeln('numbers; otherwise an error occurs!');
 Readln(SEL); 
 If SEL = 1 then
  Begin
   Writeln('Are you able to create a game');
   Writeln('of yourself using pascal??');
   Delay(2000); 
   Goto Return; 
  End;
 If SEL = 2 then
  Begin
   Writeln('Ahhh... no saved games');
   Delay(2000); 
   Goto Return; 
  End;
 If SEL = 3 then
  Begin
   Writeln('networking or 2 players?');
   Delay(2000); 
   Goto Return; 
  End;
 If SEL = 4 then
  Begin
   Writeln('Exit?'); 
   YN := Readkey;
   If YN = 'y' then 
    Begin 
     Writeln('Nooooooooooooo...'); 
     Delay(1000); 
     Halt; {EXIT PROGRAM} 
    End;
   If YN = 'n' then 
    Goto Return;
  End;
End. 

Now, the next program is written using the case statement and the output
is almost the same.

Program Program1b_Lesson5;
Uses Crt;
Label Return;  {use of the goto statement 
                is not recommended..avoid it}
Var SEL : Integer;
    YN  : Char;
           
Begin
 Return:Clrscr;
 Writeln('[1].PLAY GAME');
 WRITELN('[2].LOAD GAME');
 WRITELN('[3].MULTIPLAYER');
 WRITELN('[4].EXIT GAME');
 Writeln('note: Do note press anything except'); 
 Writeln('numbers; otherwise an error occurs!');
 Readln(SEL);
 Case SEL of
  1 : Begin
       Writeln('Are you able to create');
       Writeln('a game of yourself using pascal??');
       Delay(2000); 
       Goto Return; 
      End;
  2 : Begin
       Writeln('Ahhh... no saved games');
       Delay(2000); 
       Goto Return; 
      End;
  3 : Begin
       Writeln('networking or 2 players?');
       Delay(2000); 
       Goto Return; 
      End;
  4 : Begin
       Writeln('Exit?'); 
       YN := Readkey;
       Case YN of {a sort of a nested case statement}
        'y' : Begin 
               Writeln('Nooooooooooooo...'); 
               Delay(1000); 
               Halt;
              End;
        'n' : Goto Return;
       End;{End Case2}
      End;{Close Conditional Expression 4}
 End;  {End Case1}
End. 
This source code had a syntax error and is now corrected (Vaar changed to Var)

The Case-Else Statement 

Again this is similar to the if..then..else statement. Study the program below to learn how to use the 'else' term following the 'case statement':

Program Program2_Lesson5;
Uses Crt;
Label Return; { avoid it }
Var YN : Char;
           
Begin
 Return:ClrScr;
 Writeln('Exiting?');
 YN := Readkey;
 Case YN of
  'y' : Halt;
  'n' : Begin
         Writeln('What are you going to do here, anyway?');
         Delay(2000);
         Halt;
        End;
  Else
  Begin
   Writeln('Either press ''y'' for yes');
   Writeln('or ''n'' for no.. please try again..');
   Delay(3500);
   ClrScr;
   Goto Return;
  End;
 End; {CASE}
End. {PROGRAM} 

 
 


User Comments

Post By: AnDeH Posted On: 2009-09-12
this site is really helpful but i dont see anything about while functions :(

Post By: ashamideAngel Posted On: 2009-08-24
Nice one Sir,yet i think we should go by simple examples.

Post By: Muhedin Abdullahi Mohamed Posted On: 2009-05-16
this is really a usefull site, but I see it may be better to organise the lessons more than it is; for example giving a detailed explanation about a particular structure of the language followed by a number of examples increasing in default the simplest one first,then the second simplest......and lastly the most deficult one.....that is my poor point of view.

Post By: Lianne Posted On: 2009-05-09
thanks very much!!

Post By: Omar Posted On: 2009-04-06
Artful and neat works, need maybe to consantrate on the subject

Post By: marvambi Posted On: 2009-03-06
I 'll comment on this later when I 've fully digested this lesson.

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

Post By: jhen Posted On: 2009-02-13
ang gwapo nl sir

Post By: wsh_792 Posted On: 2008-11-10
Thanks. Great site. But how can I avoid using the GOTO statement and the Return Label?

Post By: CHICKEN WINGS Posted On: 2008-03-03
AS IT IS IN MY HONOR I WISH TO THANK THE INVENTOR OF THIS WEBSITE IT IS EASY AND VERY GUI FRIENDLY THANKS KEEP IT UP!!!!!!!!!!!!!!!!!!

Post By: danny Posted On: 2008-03-03
BIG FAT DISGRACE IT IS SOOOOOOO DIFFICULT

Post By: OPONDOH PETER Posted On: 2007-10-26
VERY EXQUISIT AND UP TO DATE ILLUSTRATIONS

Post By: Zeezou Posted On: 2007-05-17
Thanks so much for this tutorials, its really appreciated. Can we have something like this for Java

Post By: Val Idoko Posted On: 2007-05-12
Thanks so much. Keep it up.

Post By: moderator Posted On: 2006-12-09
program1b_lesson5 is now corrected from errors.

Post By: dosse Posted On: 2006-12-09
il programma 1b non funziona e non capisco perchè


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 ]
6216690031
 
 
[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