+ All Categories
Home > Documents > Pascal Programming Making decisions - Selection Statements National Certificate Unit 4 Carl Smith.

Pascal Programming Making decisions - Selection Statements National Certificate Unit 4 Carl Smith.

Date post: 18-Jan-2018
Category:
Upload: kerry-knight
View: 223 times
Download: 0 times
Share this document with a friend
Description:
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.
21
Pascal Programming Making decisions - Selection Statements National Certificate Unit 4 Carl Smith
Transcript
Page 1: Pascal Programming Making decisions - Selection Statements National Certificate Unit 4 Carl Smith.

Pascal Programming Making decisions- Selection Statements

National Certificate Unit 4Carl Smith

Page 2: 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 Falsee.g. Is the quantity less than 500? Is the print at the end of a page? Has the employee worked more than 40

hours?

Page 3: Pascal Programming Making decisions - Selection Statements National Certificate Unit 4 Carl Smith.

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.

Page 4: Pascal Programming Making decisions - Selection Statements National Certificate Unit 4 Carl Smith.

Selection Representation Testing the condition of an

expression within program flow is represented in a flow chart like this:-

No, then End.

Run program again?Yes, then run again

Page 5: Pascal Programming Making decisions - Selection Statements National Certificate Unit 4 Carl Smith.

The ‘If’ statement Pascal and most other languages

provide the ‘if’ statement to allow the programmer to make decisions

Page 6: Pascal Programming Making decisions - Selection Statements National Certificate Unit 4 Carl Smith.

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

Page 7: Pascal Programming Making decisions - Selection Statements National Certificate Unit 4 Carl Smith.

Expressions and ValuesBoolean Expression Boolean Value7 = (2 * 3.5) True-18 < -15 True13 <= 100 True0.012 > 0.013 False‘A’ <= ‘B’ True‘B’ > ‘A’ True-17.32 <> -17.32 False

Page 8: Pascal Programming Making decisions - Selection Statements National Certificate Unit 4 Carl Smith.

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 ...

Page 9: Pascal Programming Making decisions - Selection Statements National Certificate Unit 4 Carl Smith.

‘AND’ and ‘OR’ The following table illustrates the

possible outcomes of combining two conditions using AND and OR.

Condition A Condition B A AND B A OR Bfalse false false falsefalse true false truetrue false false truetrue true true true

Page 10: Pascal Programming Making decisions - Selection Statements National Certificate Unit 4 Carl Smith.

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}

Page 11: Pascal Programming Making decisions - Selection Statements National Certificate Unit 4 Carl Smith.

The IF.. THEN statement in PascalIF 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.

Page 12: Pascal Programming Making decisions - Selection Statements National Certificate Unit 4 Carl Smith.

Example The actions specified after the THEN and ELSE

clauses can be any imperative PASCAL statement e.g.IF month = 12 THEN BEGINMonthname := ‘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.

Page 13: Pascal Programming Making decisions - Selection Statements National Certificate Unit 4 Carl Smith.

The IF..THEN..ELSEstatement 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;

Page 14: Pascal Programming Making decisions - Selection Statements National Certificate Unit 4 Carl Smith.

‘if then else’ - Code sampleIF quantity < 500 THENbeginquantity : = quantity + 500;end

ELSEbeginquantity : = quantity * 1.05; Writeln(quantity);end

Page 15: Pascal Programming Making decisions - Selection Statements National Certificate Unit 4 Carl Smith.

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 the member aged under 18?Is the member aged between 19 and 30?Is the member aged between 30 and 50?Is the member aged over 50?

Etc…

Page 16: Pascal Programming Making decisions - Selection Statements National Certificate Unit 4 Carl Smith.

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 blockcase value 2: statement or statement block : : :case value n: statement or statement blockELSE statement or statement block END Note there is no ‘BEGIN’ to the statement block

Page 17: Pascal Programming Making decisions - Selection Statements National Certificate Unit 4 Carl Smith.

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 OFl: 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;

Page 18: Pascal Programming Making decisions - Selection Statements National Certificate Unit 4 Carl Smith.

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;

Page 19: Pascal Programming Making decisions - Selection Statements National Certificate Unit 4 Carl Smith.

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.

Page 20: Pascal Programming Making decisions - Selection Statements National Certificate Unit 4 Carl Smith.

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;

Page 21: Pascal Programming Making decisions - Selection Statements National Certificate Unit 4 Carl Smith.

SummaryMaking decisionsBoolean LogicThe If statementThe If Then Else statementCASE and CASE-ELSE


Recommended