Chapter 2 - Program StructureContents:Decisions & RepetitionsLinear CodeUp until now, our code has simply been a list of instructions to be followed in sequence, one after the other. This is known as Linear or Sequential code: begin <Statement 1 >; <Statement 2 >; < >; <Statement n-1>; <Statement n > end. This sort of code is for programs which do things step by step (and good program design). More complex problems require more sophisticated constructs. These constructs (think of constructs as structured methods), fall into two categories: Conditional (Branching, Decisions) and Repetition (Looping, Iteration). If - Then - ElseSimple 'if' StatementBranching code allows different sets of instructions to be programmed for different conditions, hence Conditional constructs. The first of these to get to grips with is the if statement. The syntax is as follows:
..>;
<Statement>;
if <Condition> then
<Statement>;
<Statement>;
<..
The line of code after then is only executed if the condition is true. If the condition is not true, then it is ignored. E.g.:
if Age < 18 then
Minor := true;
Note in the syntax that there is no semicolon after then, it is all one line of code. If you do put a semicolon after then, the if construct is ended and following line is executed unconditionally. More Complex 'if'You can put multiple instructions within an if statement if necessary. As with all blocks of Pascal code, they must be between a begin and end though as follows:
..>;
<Statement>;
if <Condition> then
begin
<Statement>;
<.. ;
..>;
<Statement>
end;
<Statement>;
<..
<Statement>;
if Age < 18 then
begin
Minor := True;
WriteLn( 'You're not allowed to vote yet' )
end;
<Statement>
Take a careful note of where all the semicolons go! Pascal can be very unforgiving (quite rightly so!) on this, it is a common pitfall for the inexperienced. The indentation is also important. It allows the code to be examined easily for errors. The way the blocks of code are laid out should reflect the precise design of the solution. The 'else' PartThe simple if statement does nothing if the condition is not true. The else part of the statement allows us to add alternative code to be executed where necessary. The syntax in it's simplest form is as follows:
..>;
<Statement>;
if <Condition> then
<Statement A>
else
<Statement B>;
<Statement>;
<..
If the condition is true, then statement A is executed and statement B ignored. If the condition equates to false however, Statement A is ignored and Statement B is executed. Again, the syntax is very strict. If you were to put a semicolon after either the then, Statement A, or the else, the statement would be ended prematurely and the program would not behave as intended.
if Age < 18 then
Minor := true
else
Minor := false;
Blocks of code (multiple instructions) can also be inserted in just the same way as the if part, bounded by begin and end. Again, care is needed in placing the semicolons, but by now the pattern should start to seem familiar. Take note also of the way the code is indented to accentuate the logical blocks of code. When creating complex 'decision trees', using multiple blocks of code, it becomes very helpful to label each end statement:
..>;
<Statement>;
if <Condition> then
begin
<Statement>;
<.. ;
..>;
<Statement>
end { if }
else
begin
<Statement>;
<.. ;
..>;
<Statement>
end; { else }
<Statement>;
<..
<Statement>;
if Age < 18 then
begin
Minor := true;
WriteLn( 'You're not allowed to vote yet' )
end { if }
else
begin
Minor := false;
WriteLn( 'You are eligible to vote' )
end;
<Statement>
The 'for' LoopSyntax and usageThe simplest form of repetition is the for loop. The only drawback is that the loop is controlled by a counter. In normal use, we have to know in advance how many times the loop must be executed. The syntax for repeating single and multiple statements is as follows:
{Single statement}
for <counter> := <first> to <last> do
<statement>;
{Multiple statements}
for <counter> := <first> to <last> do
begin
<statement 1>;
<.
.>;
<statement n>
end; {for}
The counter must be of an ordinal type. By this we mean an integer number or perhaps even characters (for Letter := 'a' to 'z' do is perfectly acceptable!). The simplest way to think of this is to realise that we can't execute part iterations e.g. go 3½ times round the loop! The counter can be referenced from within the loop (see the example below), but it is generally considered bad practice to reassign (alter) the counter from within the loop as this can cause confusing and unpredictable results. Example:
<Statement>;
Total := 0;
WriteLn( 'Type three numbers below:' );
for Count := 1 to 3 do
begin
Write( 'Number ', Count, ': ' );
ReadLn( Number );
Total := Total + Number
end; {for}
WriteLn( 'Their total is: ', Total );
<Statement>;
Chapter 2 ExercisesRetirement AgeDesign and code a program which prompts for a persons age and gender and decides whether the person is above official retirement age. Use ages 65 for men and 60 for women. Test your program. Solution
|