Flow Control
Flow control is basically changing what your program does depending on the circumstances. In pascal there is the if statement which is used like so:

if <condition> then
   statement block
else if <condition> then
   .
   .
   .
else
   statement block

The condition takes the form:

[boolean expression (AND|OR) [NOT] boolean expression ...]

and this can go on for a long time. For instance you could have a condition:

if (i = 1) and (j <= 3) or not (k <> 0) then
   statement block

Note : (not k <> 0) is the same as (k = 0).
Now here is an example program:

This first prompts the user to enter a password and then reads that into intput. First it checks to see if input is 'Pascal'. Note that this is case sensitive. If it is 'Pascal' then it writes 'Pascal is easy!' and goes to the end of the if statement (which happens to be the end of the program) otherwise it checks the next condition. Is it 'Basic'? if it is then write 'Basic is not very hard!' (over two lines).If it is not then try the next condition. The next condition is ELSE. The code in the ELSE part of the if statement is executed if none of the other conditions in the if statement are met.
Another flow control command, which is actually considered bad practice to use, but is quite useful in some situations is goto. To use this declare a label in the label section and use it in your code. Then just say : goto label;

eg:

Easy!
You might have noticed that sometimes the if statement may get a little cumbersome to use. ie:

And so on. This is really cumbersome and annoying (you have to type the same thing over and over). So what you want to use is the case statement. The above example would be done like so: Very handy. If you want to have multiple lines of code for one of the options, then you must put the multiple lines between begin and end. If you try to use the case statement with a String type (or any type that isn't a char or integer) then Pascal will give you an error. You can only use ordinal types with the case statment.
Another useful set of commands are the 'EXIT' commands. These are:

Halt

This does the simple task of ending your program.

Exit

This command exits from the current procedure/function.

Break

This command exits from the current loop.


Previous Lesson (Loops)     Main Menu     Next Lesson (Arrays)