# Logical – IF – CASE OF

```1: Program Tutorial2_if;
2: var pilih: Integer;
3: Begin
4:   write('Pilih 1 atau 2'); readln(pilih);
5:   if pilih=1 then write('Anda memilih 1')
6:   elseif pilih=2 then write('Pilihan anda 2')
7:   else write('Anda memilih yang lain');
8: End.

1: Program Tutorial2_case;
2: var pilih: char;
3: Begin
4:   write('Pilih 1 atau 2'); readln(pilih);
5:   case pilih of
6:     '1': write('Anda memilih 1');
7:     '2': write('Pilihan anda 2');
8:   else write('Anda memilih yang lain');
9:   end;
10: End.

Program CaseOf_Or;
Uses crt;
Var
Hargaperkg:integer;
Kualitas:char;
Begin
Clrscr;
Write(‘ Massukkan Kualitas Buah [A/B/C] : ‘); readln(kualitas);
Case kualitas of
‘A’,’a’: hargaperkg:=1000;
‘B’,’b’: hargaperkg:=750;
‘C’,’c’: hargaperkg:=500;
Else
Hargaperkg:=0;
End;
Writeln(‘ Harga Per Kg : Rp.’,hargaperkg);
End.

Structure Case...Of;```

In the 1970s, programming language designers were looking for simpler ways to write statements that were previously involved expressions. Since the programmers made more errors on complex statements, it seemed reasonable to attempt to distill the BLOCK-IF into a more concise representation. In PASCAL, this representation is called the CASE statement. Instead of relational operators, the CASE statement simply specifies possible values for a variable, together with statements to be executed if that variable has a given value. For example, consider the following CASE statement (written in pseudocode) for a academic grade evaluation:

‘A’: WRITELN(‘Excellent work!’);

‘B’: WRITELN(‘You did well…’);

‘C’: WRITELN(‘Average performance.’);

‘D’: WRITELN(‘Needs some improvement…’);

END-CASE

Certainly this simple instance of the CASE statement is easier to read than the corresponding BLOCK-IF statement:

IF grade == ‘A’ THEN WRITELN(‘Excellent work!’)

ELSEIF grade == ‘B’ THEN WRITELN(‘You did well…’)

ELSEIF grade == ‘C’ THEN WRITELN(‘Average performance.’)

ELSEIF grade == ‘D’ THEN WRITELN(‘Needs some improvement…’)

ENDIF

The problem with CASE statements occurs not when the statement blocks are small, or when there are a few alternatives. Rather, the CASE statement begins to defeat its design goal of conciseness and readability when the blocks of statements become large or when there are many alternatives. In such expressions, a given CASE statement may span several pages or more. When you are trying to debug such statements, it becomes very difficult to remember where you are in the statement, and one tends to feel lost. As a result, we prefer to use only IF or BLOCK-IF statements for decision structures.

```(http://www.cise.ufl.edu)

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}

http://pascalprogramming.byethost15.com