Pascal Programming Making decisions - Selection Statements

Download Report

Transcript Pascal Programming Making decisions - Selection Statements

Pascal Programming
Making decisions
- Selection Statements
National Certificate Unit 4
Carl Smith
Selection

In most programs there are actions which
are to be executed only when certain
conditions are true. Conditions are
expressions which, when evaluated,
produce a result of either True or False
e.g.
 Is the quantity less than 500?
 Is the print at the end of a page?
 Has the employee worked more than 40
hours?
Selection…

The ability to write a program to make
such decisions is an important tool to add
to your problem-solving techniques.

A decision structure is a programming
construct that allows a deviation from the
sequential processing using a condition to
assist in the selection of one or more
possible execution paths.
Selection Representation

Testing the condition of an
expression within program flow is
represented in a flow chart like
this:Yes, then run again
Run program again?
No, then End.
The ‘If’ statement

Pascal and most other languages
provide the ‘if’ statement to allow
the programmer to make decisions
Conditions


A conditional expression compares two
quantities, normally of the same type and
returns a value of TRUE if the relationship is
valid, and FALSE if it is not.
The operators used for comparison are called
relational operators. They may be one of the
following:
OPERATOR






=
<>
<
>
<=
>=
MEANING
IS EQUAL TO
IS NOT EQUAL TO
IS LESS THAN
IS GREATER THAN
LESS THAN OR EQUAL TO
GREATER THAN OR EQUAL TO
Expressions and Values
Boolean Expression
Boolean Value
7 = (2 * 3.5)
True
-18 < -15
True
13 <= 100
True
0.012 > 0.013
False
‘A’ <= ‘B’
True
‘B’ > ‘A’
True
-17.32 <> -17.32
False
Multiple Conditions



Sometimes an action depends on the evaluation
of more than one condition e.g.
an extra mileage allowance is paid by a company
if the employee is an executive and the engine
size of the car is greater than 1500 cc.
In PASCAL it is possible to combine expressions
together using the Boolean operators AND and
OR. e.g.
IF (driver = ‘exec') AND (engine > 1500)
THEN ...
IF (age < 18) OR (age > 60) THEN ...
‘AND’ and ‘OR’

The following table illustrates the
possible outcomes of combining two
conditions using AND and OR.
Condition A
false
false
true
true
Condition B
false
true
false
true
A AND B
false
false
false
true
A OR B
false
true
true
true
The ‘NOT’ operator

The NOT operator is used to NEGATE
the boolean value of an expression.
E.g.
IF NOT EOF {if not at end of file}
 (18 = (10 + 8))
{true}
 NOT (18 = (10 + 8))
{false}

The IF.. THEN statement in
Pascal
IF condition THEN
BEGIN
Statements
END;
{more statements}

If the condition evaluates as true then the
statements after the THEN clause (between the
Begin and End) are executed otherwise
execution continues at the statement after the
semicolon.
Example

The actions specified after the THEN and ELSE
clauses can be any imperative PASCAL
statement e.g.
IF month = 12 THEN
BEGIN
Monthname := ‘December’
END;


More than one statement enclosed by BEGIN and
END is known as a statement block.
Note: If a statement block consists of only 1
statement it is not essential to use a BEGIN and
END clause (and therefore is no longer a
statement block..!). However be careful with
the use of semicolons.
The IF..THEN..ELSE
statement in Pascal

Sometimes a further alternative action is
required if the condition evaluates as
false. This can be done using the ELSE
clause of the IF statement. e.g.
IF condition THEN
statement block
ELSE
a different statement block;
‘if then else’ - Code sample
IF quantity < 500 THEN
begin
quantity : = quantity + 500;
end
ELSE
begin
quantity : = quantity * 1.05;
Writeln(quantity);
end
The “Case” statement…


Decisions can also be programmed using the “Case”
statement.
Case is useful when a number of decisions have to be
made
Examine this example for a club membership:-
Is
Is
Is
Is
the
the
the
the
Etc…
member
member
member
member
aged
aged
aged
aged
under 18?
between 19 and 30?
between 30 and 50?
over 50?
The “Case” Statement…

The Case structure uses an expression to transfer
program control to any one of a collection of
possible paths.
CASE selector OF {selector is the expression)
case value 1:
statement or statement block
case value 2:
statement or statement block
: : :
case value n:
statement or statement block
ELSE
statement or statement block
END

Note there is no ‘BEGIN’ to the statement block
How “CASE” works…

If the values of the expression and a case constant match,
the associated statement or statement block is executed. If
none of the cases match, the statement(s) associated with
the ELSE are executed. The ELSE statement is optional.
CASE groupnumber OF
l: premium := 97;
2: premium := 115;
3: premium := 165;
4: premium := 176;
ELSE premium := 204;
END;

If there is no ELSE and none of the cases match, execution
continues at the statement following the end;
Case Example…

Case values can be expressed as single values, a
group of values or a range of values: e.g.
CASE marks OF
1 . . 37 :writeln (“outright fail”);
38, 39
:writeln(“near miss”);
100
:writeln (“full marks !”)
ELSE
writeln (“passed”);
END;
Case Example…
Readln(keypress);
CASE keypress OF
‘A’,‘E’,‘I’,‘O’,‘U’:writeln(“vowel”);
‘1’..‘9’
:writeln(“digit”);
‘+’, ‘-‘, ‘/’, ‘*’ :writeln(“operator”);
‘ ‘
:writeln(“space”);
ELSE
writeln(“other character”)
END;

Note: If more than one statement is needed for the
“action” statements use the BEGIN and END delimiters.
Indentation

To make your code easier to read
use ‘indentation’ in loops and
selection statements e.g.
IF month = 12 THEN
BEGIN
Monthname := ‘December’
END;
Summary
 Making
decisions
 Boolean Logic
 The If statement
 The If Then Else statement
 CASE and CASE-ELSE