domingo, 24 de março de 2013

Pascal Case.. Of...

Control Structures - Module 3

3. Control Structures

3.2. Case... Of...

This is a Selection Structure, there is a variable which will be used as a selector.

Syntax


CASE <variable> OF

<option1> : {instructions};
<option2> : {instructions};
<option3> : {instructions};

ELSE

 {instructions};
end;


Note: Notice the end;
Example:




This is a simple menu, the user would enter a number and get the name of a fruit. Just like the If.. Then.. Else.. You can use a begin..end block between each option, this is also used if you want to have more than one instruction.

Example:


As you can see I used a begin end block for every option, there's no sense in doing this if the option does not need more than one instruction, however you can still do it (I did so in Melon and Kiwi).

Don't get confused with the begin end block at the bottom, the structure must always have an end; to finish it. I do a jump to x, which is defined as a label and brings the user back to the beginning of the program.

Execution:


0 comentários

Posts a comment

 
© Doc's Workshop
Designed by Blog Thiết Kế
Back to top