Jump to content

[PASCAL] Case .. of statement


CouldnoT
 Share

Recommended Posts

Case .. of statement

There is another method for conditional branching, which is the Case .. Of statement.

It branches execution according to the case ordinal value. The Restaurant program will illustrate the use of the case of statement:

 

Restaurant program

var
 Meal: Byte;
begin
 Writeln('Welcome to Pascal Restaurant. Please select your order');
 Writeln('1 - Chicken (10$)');
 Writeln('2 - Fish (7$)');
 Writeln('3 - Meat (8$)');
 Writeln('4 – Salad (2$)');
 Writeln('5 - Orange Juice (1$)');
 Writeln('6 - Milk (1$)');
 Writeln;
 Write('Please enter your selection: ');
 Readln(Meal);
 case Meal of
 1: Writeln('You have ordered Chicken,',
 ' this will take 15 minutes');
 2: Writeln('You have ordered Fish, this will take 12 minutes');
 3: Writeln('You have ordered meat, this will take 18 minutes');
 4: Writeln('You have ordered Salad, this will take 5 minutes');
 5: Writeln('You have ordered Orange juice,',
 ' this will take 2 minutes');
 6: Writeln('You have ordered Milk, this will take 1 minute');
 else
 Writeln('Wrong entry');
 end;
 Write('Press enter key to close');
 Readln;
end. 

 

Students' Grades program :

var
 Mark: Integer;
begin
 Write('Please enter student mark: ');
 Readln(Mark);
 Writeln;
 case Mark of
 0 .. 39 : Writeln('Student grade is: F');
 40 .. 49: Writeln('Student grade is: E');
 50 .. 59: Writeln('Student grade is: D');
 60 .. 69: Writeln('Student grade is: C');
 70 .. 84: Writeln('Student grade is: B');
 85 .. 100: Writeln('Student grade is: A');
 else
 Writeln('Wrong mark');
 end;
 Write('Press enter key to close');
 Readln;
end.

 

In the previous sample we have used a range, like (0 .. 39), which means the condition will return True if the Mark value exists in this range.

 

Note: The Case statement works only with ordinal types like Integers, char, but it doesn't work with other types like strings, and real numbers.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.