Download presentation
Presentation is loading. Please wait.
Published byKerry Knight Modified over 9 years ago
1
Pascal Programming Making decisions - Selection Statements National Certificate Unit 4 Carl Smith
2
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?
3
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.
4
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
5
The ‘If’ statement Pascal and most other languages provide the ‘if’ statement to allow the programmer to make decisions
6
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
7
Expressions and Values Boolean ExpressionBoolean Value 7 = (2 * 3.5)True -18 < -15True 13 <= 100True 0.012 > 0.013False ‘A’ <= ‘B’True ‘B’ > ‘A’True -17.32 <> -17.32False
8
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 60) THEN...
9
‘AND’ and ‘OR’ The following table illustrates the possible outcomes of combining two conditions using AND and OR. Condition A Condition BA AND BA OR B false falsefalsefalse false truefalsetrue true falsefalsetrue true truetruetrue
10
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}
11
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.
12
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.
13
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;
14
‘if then else’ - Code sample IFquantity < 500 THEN begin quantity : = quantity + 500; end ELSE begin quantity : = quantity * 1.05; Writeln(quantity); end
15
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…
16
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 ELSEstatement or statement block END Note there is no ‘BEGIN’ to the statement block
17
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;
18
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;
19
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.
20
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;
21
Summary Making decisions Boolean Logic The If statement The If Then Else statement CASE and CASE-ELSE
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.