
==== Step 4:
Step Title     : Program Control
File Name      : MP_4.txt
Author Note    : Pretty simple lesson..
Example Source : none
Author         : Michael Preslar / E_Maus
Date           : October 5th, 1998
Length         :
Layout         : - Repeat / Until
                 - If / Then / Else
                 - For / To
                 - For / Downto
                 - The Case Statement
                 - Goto / Labels
====
Please refer back to "intro.txt" for complete documentation of copyrights,
and author information.

Step 4 Foreword
---------------
In this lesson, Ill show you ways to control your programs. If you look at
the above layout, you can pretty much understand what each does. Although
this lesson may seem a bit simple, mastering the methods discussed isnt as
simple. But once you do, youre programs will be capable of a lot more.

  ============================= Repeat / Until =============================
This is the simplest loop.. Let me show you an example program..

program repeatuntil;
var b:byte;
begin
 b:=1;
 repeat
  writeln(b);
  b:=b+1;
 until b=10;
end.

As you can see, this is a very simple loop. You just tell it to "repeat
<blah blah> until <this>".

  ============================= If / Then / Else ===========================
This would be the next easiest.. Using Pascal's "readability", you can easily
pick up on this one..

program ifthenelse1;
var ch:char;
begin
write('Do you want to continue? ');
readln(ch);
if (ch='y') or (ch='Y') then
 writeln('Yes!')
else
 writeln('No!');
end.

program ifthenelse2;
var ch:char;
    yn:boolean;
begin
 yn:=false;
 repeat
  write('Do you want to continue? ');
  read(ch);
  if (ch='y') or (ch='Y') then
   begin
    writeln('Yes!');
    yn:=true;
   end
  else
 if (ch='n') or (ch='N') then
   begin
    writeln('No!');
    yn:=true;
   end;
 until yn=true;
end.

If/then/else is simple.. But watch the syntax. The last command before "else"
does not have a semi-colon. For example: In program 1, "writeln('Yes!')"..
In program 2, "end".. And no. You dont have to have an "else" for every
"if/then". "if <blah> then <do this>" is valid..

Take note of the second example. Say your sitting at a menu of some sort,
and you dont get a response from the program until you hit a valid key.
This would be an example of how to do that. It repeats until "yn" is true.
That variable is only set to true if y,Y,n, or N is hit. There are much better
ways to do this, so I wouldnt suggest using example 2 in your program.

  ================================ For / To ================================
Although this loop isnt as readable as repeat/until and if/then, you can
still easily follow along with it.

program forto;
var b:byte;
begin
for b:= 1 to 10 do
  write(b);
end.

Nice and simple. For/to loops a certain number of times. In this example, it
goes from 1 to 10. For/To always goes up scale.. In other words, "for b:= 10
to 1" would be valid, however, it would go from 10 to 11, to 12, to 13, etc
until it hit 1, which is never. Honestly, although I know 10 to 1 is valid,
the compiler will give you an error message since 1 will never come after
10.

For/To only works on sets. 1 through 10 would be a set of numbers. A through
Z would be a set. Youll see other uses of it in other places where it doesnt
use just numbers of letters..

Something to notice about the syntax.. for <this> to <that> do <whatever..
the do is part of the loop..

  ============================= For / Downto ===============================
Im not going to have an example for this one.. Why not? Well, For/Downto is
the same thing as For/To, except instead of going up, it always goes down.
"for b:=10 downto 1 do write(b)" would count down from 10.

  ========================= The Case Statement =============================
The case statement is a very useful part of the pascal language. We all know
that the better programs are able to take one piece of information and do
many things with it. You could accomplish this in your own programs using
either a bunch of if/then/else statements, or one case statement. I suggest
using one case statement.. Why? 1) Case executes faster than if/then's..
2) Case is a lot more readable. Lets look at a few examples.

program CaseExample1;
var ch:char;
begin
writeln;
writeln;
writeln('Choose one:');
writeln(' (A) Option A');
writeln(' (B) Option B');
writeln(' (C) Option C');
writeln(' (D) Option D');
readln(ch);
ch:=upcase(ch);
case ch of
'A' : begin
       writeln('You chose option A');
       {do whatever for this option}
      end;
'B' : begin
       writeln('You chose option B');
       {do whatever for this option}
      end;
'C' : begin
       writeln('You chose option C');
       {do whatever for this option}
      end;
'D' : begin
       writeln('You chose option D');
       {do whatever for this option}
      end;
end;{case}
end.

Note the syntax.. "case <your variable> of" and then each option. Case will
only do what you tell it. In other words, in the above example, if they hit
the letter 'j', nothing will happen. You only told it to do stuff for A-D.
Then after you do all the options, case has an "end;".. Personally, I put the
{case} after each case's "end;" to help me keep track of what the end is for.

Something else to notice. Between what the option could be ('A','D') and
what should be done for the option, there is a colon (:). Why? Let me show
you, and while Im at it, show you something else..

program CaseExample2;
var b:byte;
begin
writeln;
writeln;
writeln('Choose one:');
writeln(' (1) Option 1 (2) Option 2');
writeln(' (3) Option 3 (4) Option 4');
writeln(' (5) Option 3 (6) Option 4');
writeln(' (7) Option 3 (8) Option 4');
writeln(' (9) Option 3 (0) Option 4');
readln(ch);
case ch of
1 : begin
       writeln('You chose option 1');
       {do whatever for this option}
      end;
2 : begin
       writeln('You chose option 2');
       {do whatever for this option}
      end;
3 : begin
       writeln('You chose option 3');
       {do whatever for this option}
      end;
4 : begin
       writeln('You chose option 4');
       {do whatever for this option}
      end;
5,6: begin
      writeln('You chose either option 5 or 6');
      {do whatever for these}
      end;
7,8: begin
      writeln('You chose either option 7 or 8');
      {do whatever for these}
      end;
9,0: begin
      writeln('You chose either option 9 or 0');
      {do whatever for these}
      end;
else
  begin
   writeln('You picked an invalid option.');
   {do whatever}
  end;
end;{case}
end.

Notice the syntax of this second example.. Basically the same as the first,
but it has a few differences.

Difference one: What happened to the single quotes around the possible
options? Single quotes would only be needed with either character or string
variable types.

Difference two: Whats with the commas between the different options? That
explains why you have to have the colon. The colon is a signifier that says
when the possible option is done. The comma is a signifier that says "heres
another possible option"..

Difference three: Whats this "else" thing doing here? Yes, you can put an else
there. In this example and since variable is of type byte, range 0 - 255, the
else would only be for numbers 10 through 255.

A few notes.. In the second example, we used a byte type variable. So only
numbers within the range of 0 to 255 would be valid. In the first example,
had the input been more than one character, or in the second example, had the
input been something other than a number in the right range, you would get
the run time error, "invalid numeric format". However.. In the first example,
we used a character type variable with a readln. No matter what the input is,
the "program" will only get the first character.

Why do I mention this? Well, the case statement only works with the same
types. As in, if youre using a byte type variable, all the possible options
have to be of type byte. If your using a character, all options have to be of
type character.

And no. You dont have to have a begin/end pair for each option. If you only
have one command for an option, just do it like so:

  'A': writeln('You picked option A');

  ============================== Goto / Labels =============================
If any of you has ever done any BASIC programming, "goto" and "labels" will
seem familiar. Those that havent will notice that "labeling" your program
is the easiest of all program controllers. One thing to note tho is that
the pascal programming community is split 50/50 about labels. One half
thinks that labels are a viable part of the language, while the other half
thinks that labels are just a relic. Personally, Ill use them from time to
time, if the program Im working on needs them.

program GotoLabelExample;
var ch:char;
label tryagain;

begin
tryagain:
write('Your name is Bob. Is this right?');
readln(ch);
ch:=upcase(ch);
if ch='N' then goto tryagain;
end.

The syntax.. Like declaring a variable, you must declare the label. You can
have as many labels as you want. Hell, you could even make Pascal code out
to look like basic code using labels. Using the label is simple.
"goto <labelname>".. Of course, youd replace <labelname> with whatever your
label is.

Note.. If you want to use more than one label in a chunk of code, it would
resemble:
         label tryagain, onemoretime, canihaveanother;

Where each label is seperated by a comma. And no. Labels arent case sensitive

  =============================== Retrospect ===============================
In this step, we looked at the different ways to branch and loop our programs.
Using the if / then / else and case statements, we can have multiple roads
for our programs to follow. Using the repeat / until and for / to/downto
statements, we can make our program do a certain thing for a certain amount
of time. And using goto and labels, we can tell our program exactly where to
go..

So.. Whatd ya learn?