|
|
|
Lecture 2 |
|
Introduction To Structured Programming |
|
|
|
|
|
What is Structured Programming? |
|
Sequence |
|
Selection |
|
Iteration |
|
|
|
|
Program instructions are carried out in strict
order, in other words they are carried out in Sequence. |
|
All programs will run in sequence. |
|
Real Life Example: A Recipe |
|
|
|
|
|
|
Selection occurs when there are two or more
possible routes through your code. |
|
Route selection is based on a condition. |
|
The route taken through the program instructions
depends on the result of the condition. |
|
Example:
If the water is cold, boil it. |
|
|
|
|
Iteration occurs when instructions in a computer
program are repeated. |
|
Iteration is useful as it means that repetitive
tasks can be coded without having to enter the same instructions several
times into the program. |
|
Iteration is often called LOOPING |
|
|
|
|
Imagine the same task has to be carried out 10
times. |
|
Rather than entering the instructions into the
program 10 times (which would be repetitive and time-consuming)… |
|
… we write the instructions out once, with a
further instruction noting that they should be executed 10 times rather
than only the once. |
|
|
|
|
|
There are two main types of “loops”. |
|
Deterministic Loops |
|
Here, as in our earlier example, the number of
iterations execute is known in advance. |
|
e.g.
Loop 10 times |
|
Conditional Loop |
|
The number of iterations is based on a
condition. |
|
e.g. Loop until the total is greater than 100 |
|
|
|
|
Program ProgramName; |
|
Uses RequiredUnits; |
|
Var |
|
Variables declared here |
|
Begin |
|
Main program code here |
|
End. |
|
|
|
|
|
Name your program |
|
Name must be alpha-numeric. |
|
Name must be unique. |
|
No punctuation characters allowed. |
|
Must be followed by a semi-colon |
|
E.g. |
|
Program CreditCheck; |
|
|
|
|
Not all Turbo Pascal commands are immediately
available. To access some, it is
necessary to call the Turbo Pascal Unit that stores their instructions. |
|
This is done using the USES clause. |
|
We will nearly always want to call the CRT Unit,
which includes standard routines for formatting the computer screen. |
|
E.g. |
|
Uses Crt; |
|
|
|
|
This section is used to declare variables for
our program. (More on this later) |
|
Virtually all programs will require variables,
though if no variables are required this section can be omitted. |
|
|
|
|
The main program code, where your instructions
reside, must start with the Begin keyword and finish with the End. keyword. |
|
Begins and ends are also used within the program
to delineate program blocks. |
|
Note the full stop at the end of the End. |
|
This indicates that it is the last End in the program. |
|
Only this last End. can have this full stop. |
|
All program instructions between a Begin and an End
must be terminated by a semi-colon (;). |
|
|
|
|
Program Hello1; |
|
Begin |
|
writeln(‘Hello World’); |
|
End. |
|
|
|
Note the Indentation of the writeln command. |
|
This is standard practice to improve the
readability of the program. |
|
|
|
|
|
|
Program Hello2; |
|
Uses Crt; |
|
Begin |
|
clrscr; |
|
writeln(‘Hello World’); |
|
readln; |
|
End. |
|
|
|