Working out a Simple Example

Generally when trying to come up with an algorthim to solve a problem you should consider the following steps:
  1. Break the problem into its sequential constituents.
  2. If parts of the problem depend on some condition being true use an if <condition> then <statement> else <statement> or simply an if <condition> then <statement> as appropriate. If multiple conditions exist with cooresponding actions then a nested if ... then ...else statement must be used.
  3. Identify the parts of the problem that are repeated and code the body of the loops first. Then put the appropriate looping contruct around the body. Choose the looping construct according to the following rules:
    • User a for loop if you have a fixed number of itterations. For example to repeat a process N times you could use:
      for i:=1 to N do begin <body of loop> end;
      Note that you do not need to increment i as it is automaticaly incremented at the ned of the loop.
    • Use a while <condition> do loop whenever you want to test some condition before each execution of the body of the loop. Note that the body will only be executed whenever the condition is true.
    • Use a do <body> repeat <condition> loop whenever you want the condition to be checked at the end of the loop after the body has been exeuted. Note that the body will always be executed at least once and the loop is repeated if the condition is false.
    Make sure that you choose the appropriate condition to ensure that the loop is executed the required number of times.