Pascal-Programming.
info
A step-by-step Pascal tutorial for beginners.
Home
Lesson 1
The First Few Steps in
Pascal Programming
Lesson 2
Variables & Constants
Lesson 5: The CASE-OF
Statement
Lesson 3
Special Functions of the
CRT Unit
Lesson 4
Program Flow Control
Lesson 5
The Case-Of Statement
Lesson 6
Before continuing with this lesson, make sure that you have learnt to use 'if
statement' (lesson 4)
In a nutshell, this lesson will cover:
The Syntax of the CASE-OF Statement
The CASE-OF-ELSE Statement
Logical Operators &
Boolean Expressions
Lesson 7
Procedures & Functions
Lesson 8
open in browser PRO version
The Syntax of the CASE-OF
Are you a developer? Try out the HTML to PDF API
pdfcrowd.com
Lesson 8
File Handling
Lesson 9
The Syntax of the CASE-OF
Statement
Arrays
So
Lesson 10
learned how to use an
'if statement'. But in
Strings & String
Manipulation
Lesson 11
Record Data Structure
Advanced
Programming
Concepts
far,
you
have
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..}
...
open in browser PRO version
Are you a developer? Try out the HTML to PDF API
pdfcrowd.com
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;
Donate 5 via
PayPal and get an
offline PDF
version of this
tutorial in return.
Support PascalProgramming.info
with your
generous
donation!
Click here to
Donate & Get
PDF
open in browser PRO version
Begin
Return: Clrscr;
Writeln('[1].PLAY GAME');
WRITELN('[2].LOAD GAME');
WRITELN('[3].MULTIPLAYER');
WRITELN('[4].EXIT GAME');
Writeln('note: Do not press anything
except');
Writeln('numbers; otherwise an error
occurs!');
Readln(SEL);
If SEL = 1 Then
Begin
Are you a developer? Try out the HTML to PDF API
pdfcrowd.com
PDF
Thank you :)
Writeln('You will soon be able
to create');
Writeln('games using Pascal
Programming :-)');
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('Are you sure you want
to Exit?');
YN := Readkey;
open in browser PRO version
Are you a developer? Try out the HTML to PDF API
pdfcrowd.com
If YN = 'y' Then
Begin
Writeln('Good Bye...');
Delay(1000);
Halt; {EXIT PROGRAM}
End;
If YN = 'n' Then
Goto Return;
End;
End.
Now, the folowing program is written using the case-of statement instead
of the if statements and the output
is exactly 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');
open in browser PRO version
Are you a developer? Try out the HTML to PDF API
pdfcrowd.com
WRITELN('[4].EXIT GAME');
Writeln('note: Do not press anything
except');
Writeln('numbers; otherwise an error
occurs!');
Readln(SEL);
Case SEL of
1 : Begin
Writeln('You
will soon be able to create');
Writeln('games
using Pascal Programming :-)');
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;
open in browser PRO version
Are you a developer? Try out the HTML to PDF API
pdfcrowd.com
End;
4 : Begin
Writeln('Exit?');
YN := Readkey;
Case YN of {a
sort of a nested case statement}
'y' :
Begin
Writeln('Good Bye...');
Delay(1000);
Halt;
End;
'n' :
Goto Return;
End; {End Case
2}
End; {Close Conditional
Expression 4}
End; {End Case 1}
End.
Back To Top
open in browser PRO version
Are you a developer? Try out the HTML to PDF API
pdfcrowd.com
The CASE-OF-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('Press either
''y'' for yes');
Writeln('or ''n'' for
no.. please try again..');
open in browser PRO version
Are you a developer? Try out the HTML to PDF API
pdfcrowd.com
Delay(3500);
ClrScr;
Goto Return;
End;
End; {CASE}
End. {PROGRAM}
Basically, what the code does above is that if the input is none of 'y' or 'n',
then program flow falls to the 'else' of the case statement - its like an 'if all
else fails'. It works like the last 'else' of the if statement.
Back To Top
Spread The Word!
12
12
Have Your Say
4 Comments
Recommend
open in browser PRO version
Pascal-Programming.info
Share
Are you a developer? Try out the HTML to PDF API
pdfcrowd.com
Join the discussion
Carsen 4 months ago
works well and more efficient then the if statement
1
Reply Share
Victor Saliba
Mod
> Carsen 4 months ago
It is very handy and organizes your code in a structured way.
Reply Share
Carsen > Victor Saliba 4 months ago
Yes that to, for sure
Reply Share
Dehvaughnte hinds 5 months ago
thank you so much
1
open in browser PRO version
Reply Share
Are you a developer? Try out the HTML to PDF API
pdfcrowd.com
Pascal Programming: Lesson 11 - The Record Data
Structure
Pascal Programming: Articles - Linked
1 comment 6 months ago
1 comment 6 months ago
Medunoye
rush essay coupon This kind of idea that was being
given from this page about programming looks pretty
useful on those people who are interested in knowing
something more for their skills to
Subscribe
d Add Disqus to your site
Privacy
Back To Top
What's Next ... ?
Go to Lesson 6: Logical Operators and Boolean Expressions
Back To Top
www.Pascal-Programming.info
Victor John Saliba 2016
open in browser PRO version
Are you a developer? Try out the HTML to PDF API
pdfcrowd.com
| Privacy Statement | Disclaimer |
open in browser PRO version
Are you a developer? Try out the HTML to PDF API
pdfcrowd.com