TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 32


TURBO-LESSON 8: CASE STATEMENT

OBJECTIVES - In lesson 8 you will learn about:

1.  Block statements
2.  CASE statement


1.  Block statement.

As noted in an earlier lesson, the form of the IF statement is:

IF condition THEN statement_1 ELSE statement_2;

IF the condition is true, statement_1 is executed, otherwise 
statement_2 is executed.  However, a single statement may not 
always get the job done.  

The Block statement (also called a Compound statement) allows you 
to substitute a multiple statement block anywhere a simple 
statement is acceptable.  The form of the Block statement is:

BEGIN Statement_1; Statement_2 END;

You can include as many statements as you like between the BEGIN 
and END.   

Notice that the main body of a Pascal program is a single 
statement!  That single statement is a block statement:

BEGIN
  Statement_1;
  Statement_2;
    .
    .
    .
  Statement_n;
END.

To illustrate the use of a Block statement in an IF statement, 
consider the following problem:

If the value of I is greater than the value of J, swap the two in 
memory (a common problem when sorting data).

If you happen to have a single statement to swap the values of 
two memory locations, the IF statement might be:

IF I > J
  THEN SWAP(I,J);

TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 33


Later, you will learn how to make up your own "statements" (by 
creating functions and procedures), but for now, the way to swap 
I and J is:

IF I > J
  THEN
    BEGIN
      Temp := I;
      I := J;
      J := I;
    END;

Notice that the form of this IF statement is still correct:

IF condition THEN statement;  

The statement, in this case, is a Block statement, rather than a 
simple statement.  

##### DO:

Take a look at the program called TEST1.

The sample programs which begin with the word TEST are provided 
to make it quicker for you to test new statements and concepts.  
The program, TEST1, has some integer variables and character 
variables declared and the main BEGIN END.  All you need to do to 
test a statement, or group of statements, is edit them into the 
test program and run the program.

##### DO:

Insert the following statements between the BEGIN and END of 
program TEST1:

Write('Enter two numbers ');
ReadLn(I, J);
WriteLn('I=', I, ' J=', J);

Run the program.

##### DO:

Between the ReadLn and WriteLn statements you just entered, add 
the IF statement to swap I and J if I is larger:

IF I > J
  THEN
    BEGIN
      Temp := I;
      I    := J;
      J    := Temp;
    END;

Run the program several times using several pairs of input 
numbers to test the program.  Does the "swap" work right?

TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 34


##### DO:

Remove the BEGIN and END in the IF statement and run the program 
several times.  

Does the "swap" still work right?

Without the BEGIN and END to make the three statements appear as 
one,  the statements "appear" as follows to Pascal:

IF I > J
  THEN
    Temp := I;
I := J;
J := Temp;

Only the statement, Temp := I, is controlled by the IF condition.


2.  CASE statement.

First, a bit of review from an earlier lesson:

Program sequencing is done in Pascal with 

  (1) Simple Sequence,   one statement follows another,

  (2) Selection Structures,
        IF  for one-way and two-way selection, 
        CASE for many-way selection,

  (3) Repetition Structures,
        REPEAT statement,
        WHILE statement,
        FOR statement.

The CASE statement is useful when there are more than two actions 
or statement sequences needed.  The form of the CASE statement:

CASE variable OF
    value_1  : Statement_1;
    value_2  : Statement_2;
      .
      .
      .
    value_n  : Statement_n;
  ELSE
    Statement;
END; {CASE}

TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 35


Note the following:

The variable must be a simple type such as Integer, CHAR, 
BOOLEAN. (REAL is not allowed).

The values used to determine which Statement to execute must be 
of the same type as the variable.

The values may be a constant, a list of constants, or a subrange 
such as 1..10 (all integers from 1 to 10).

How it works:

If the variable has a value of "value_1" then Statement_1 is 
executed.  If "value_2" then Statement_2, . . . 

If the value of the variable matches none of the values, the 
statement following the ELSE is executed.

There must be an END to mark the end of the CASE statement. 
(It's a good idea to add the comment {CASE} after the END).  

##### DO:

Use the editor to examine PROG8.

This is the same problem as in the previous lesson, with a bit 
more programming flexibility derived from the CASE statement and 
the block statements.  

Notice that the list of variables in the CASE statement allow 
appropriate responses for acceptable responses: A, a, B, b, C, c
                             correct responses: D, d
                        unacceptable responses: anything else.


##### DO:

Modify the CASE statement in PROG8 to accept C as the best 
answer.

Run the program.  Did it work?

What message appears when D is entered?
