You've already seen branching with IF ELSE. Now let's look at a different branching method, namely CASE OF in Pascal.
About CASE OF Branching
CASE OF is branching that uses a variable and its value to decide which branch runs. That differs from IF ELSE branching, which has to use a condition in the form of a boolean expression.
Take the example of grading with letters.
- A = Very Good
- B = Good
- C = Fair
- D = Poor
- E = Very Poor
Okay... that's roughly it, grades running from A to E with the labels going from best to worst.
Now, as programmers we have a mission: write a program that prints the label for a given grade. How would you do that?
Some of you are probably thinking we can use an IF ELSE condition. Yep, exactly right. The code would look roughly like this.
program InputNilai;
uses crt;
var
nilai: char;
begin
clrscr;
write('Masukkan Nilai: '); readln(nilai);
write('Predikat: ');
// output the label based on the grade
if (nilai = 'A') then writeln('Sangat Bagus')
else if (nilai = 'B') then writeln('Bagus')
else if (nilai = 'C') then writeln('Cukup')
else if (nilai = 'D') then writeln('Buruk')
else if (nilai = 'E') then writeln('Sangat Buruk')
else writeln('Nilai tidak valid');
readln;
end.
If you run it and enter the grade A, the output will look roughly like this. You can try it with the other grades too.
Masukkan Nilai: A
Predikat: Sangat Bagus
Okay, up to here our problem is solved, but writing that many IF ELSE statements doesn't feel effective. This is where CASE OF earns its keep.
With CASE OF we can shorten a long pile of IF ELSE statements. All it needs is a variable and the value conditions.
Writing CASE OF
The main components of a CASE OF statement are a variable, the value conditions, and the statement to run. The format is as follows.
case ({variable}) of
value-1: {statement 1}
value-2: {statement 2}
.
.
value-n: {statement n}
else {statement if no value matches}
end;
Values 1 through n are the values the variable can hold. The statement then runs according to the value of that variable. Rather than sit here confused, let's go straight to the example.
A CASE OF Example
Now we'll modify the code we wrote earlier. Same grades and labels as before, but this time with a CASE OF condition. How does it look? You can see it in the code below.
program InputNilai;
uses crt;
var
nilai: char;
begin
clrscr;
write('Masukkan Nilai: '); readln(nilai);
write('Predikat: ');
// using a case of condition
case (nilai) of
'A': writeln('Sangat Baik');
'B': writeln('Baik');
'C': writeln('Cukup');
'D': writeln('Buruk');
'E': writeln('Sangat Buruk');
else writeln('Nilai tidak valid');
end;
readln;
end.
If you run it, the output will be the same as the program we wrote before. But with CASE OF the code is clearly easier to read, and certainly shorter than IF ELSE.
Shorter and simpler, yes, but not every condition can be written with CASE OF. If you want a condition that uses a boolean expression, you still have to use IF ELSE.
Well, I think you've got the concept by now, hehe...
Wrapping Up
That's enough for this tutorial, hope it helps :D next time we'll build a simple program using the loops and conditions we've learned so far.