MPL Training
Lesson 6
Black Panther

Let's get Loopy!

-=-=-=-=-=-=-=-=-

Just about any type of MPL program you write, will contain some type of a loop.
Whether it's a game, which has a huge loop for continued gameplay, or reading
information from a file, you will need to loop some instructions.

There are multiple types of loops, depending on what you are trying to do. 

If/Then/Else
For/Do
Repeat/Until
While

All of these can be used with MPL. Let's take a closer look at what each of 
them does, and how they work.

-=-=-=-=-=-=-=-=-

If/Then/Else

If you have a block of code, that you want to run, but only if a certain 
condition is true, this is the loop you want.

If <boolean statement> Then
  <True code here>
Else If <boolean statement> Then   // Optional
  <True code here>
Else                               // Optional
  <False code here>
  
Here is a couple examples to look at:

If Not fEof(fptr) Then
  WriteLn('We''re not at the end of the file.')
  

If fEof(fptr) = False Then
  WriteLn('We''re not at the end of the file.')
Else
  WriteLn('This is the end of the file.')
  
These two examples do exactly the same thing, except the second one let's you
know when you are at the end of the file.

If A = 1 Then
  WriteLn('A is 1')
Else If A = 2 Then
  WriteLn('A is 2')
Else if A = 5 Then
  WriteLn('A is 5')
Else
  WriteLn('A is not 1, 2, or 5...')
  
The If line must be true for the following line to be executed. In the last 
example, 'A = 1' has to be true for it to perform the WriteLn statement.

In the prior example, when we were checking for the end of the file, the 
statement 'Not fEof(fptr)' needs to be true for the 'Then' statement to be
executed. I know we haven't covered th fEof function yet, but it basically 
checks to see if the file pointer is at the end of the file or not. 

-=-=-=-=-=-=-=-=-

For/Do

This loop is very handy if you know exactly how many times you need the loop to
repeat.

For A := 1 to 10 Do
  WriteLn(A)
  
This is telling the compiler, that A now holds the value of 1, perform the task
that is on the next line. Then, it comes back, and A now holds the value of 2,
and performs the task, etc, until A holds the value of 10. At that point it 
will exit the loop and move on to whatever is next.

With the For/Do loop, you can also count down from a number as well, It would 
look like:

For A := 10 DownTo 1 Do
  WriteLn(A)
  
-=-=-=-=-=-=-=-=-

Repeat/Until

This loop is used a lot in programs that wait for user input, such as games. It
is similar to the While loop, but this one performs the check on the boolean
statement at the end of the loop, instead of the beginning.

Repeat
  WriteLn(A)
  A:=A+1
Until A > 5

This will write the numbers from whatever A holds the value of, until it is 
larger than 5.

This is the type of loop that I use in many of my MPL programs, and my games
that I have written. I will usually have something like:

Var
  Done : Boolean=False
Repeat
  ...many lines of game code
  If KeyPressed Then Done:=True
Until Done

In this example, the lines of code will keep repeating until the user hits any
key. Then the MPL will set the boolean variable 'Done' to hold the value of 
'True'. Once it does, the loops exits.

-=-=-=-=-=-=-=-=-

While

The While loop is similar to the Repeat/Until loop, but this one performs the
check on the boolean statement at the beginning of the loop.

While A > 0 and B = 5 Do
  WriteLn(A)
  
or

While A > 0 and B = 5 Do
Begin
  WriteLn(A)
  WriteLn(B)
End

The While loop is very useful when reading files. You can use the functions we
saw earlier in the If/Then loop.

While Not fEof(fptr) Do
Begin
  ..read the file and save to variables
End

With this loop, it will read from the file, until it gets to the end, and then
it exits the loop and moves to the next lines of code.

-=-=-=-=-=-=-=-=-

Loop Summary

While/Do
Repeats a statement or group of statements while a given condition is true. It
tests the condition before executing the loop body.

For/Do
Executes a sequence of statements multiple times and abbreviates the code that
manages the loop variable.

Repeat/Until
Like a While/Do statement, except that it test the condition at the end of the 
loop body. 

-=-=-=-=-=-=-=-=-

While you are inside of a loop, there are multiple ways to get your code to 
exit out of it. I showed one example using a boolean variable to tell the loop
when to finish. There are a couple other ways to get out of a loop.

Break
A break statement terminates the loop or case statement and transfers execution
to the statement immediately following the loop or case statement

A := 10
While A < 20 Do
Begin
  WriteLn('Value of A: '+Int2Str(A))
  A := A + 1
  If A > 15 Then Break
End

This example show the While loop will happen while the value of A is less than
20. Then there is an If/Then statement that will break the look if A is greater
than 15. At that point, the program would continue with the code following the
loop.

Output:
10
11
12
13
14
15

Continue
A continue causes the loop to skip the remainder of its body and immediately 
retest its condition prior to reiterating.

A := 10
Repeat
  if A = 15 Then
  Begin
    A := A + 1
    Continue
  End
  WriteLn('Value of A: '+Int2Str(A))
  A := A + 1
Until (A = 20)

This example will run until A holds the value of 15. At that point, it will get
the 'continue' command, which will stop any other lines of code for that 
iteration.

Output:
10
11
12
13
14
16
17
18
19

-=-=-=-=-=-=-=-=-

Ok, this lesson got a little bit longer than the previous ones. Sorry about 
that. I just wanted to make sure we covered the basics of everything involved
with using loops. There will be further examples of loops in future lessons, as
they are used a lot.
