0% found this document useful (0 votes)
9 views49 pages

Week 3.1

The document covers selection and loop structures in programming, focusing on relational and logical operators, as well as if statements. It explains how to evaluate expressions using these operators and provides examples and exercises for practice. Additionally, it discusses data validation and nested if statements for handling complex conditions in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views49 pages

Week 3.1

The document covers selection and loop structures in programming, focusing on relational and logical operators, as well as if statements. It explains how to evaluate expressions using these operators and provides examples and exercises for practice. Additionally, it discusses data validation and nested if statements for handling complex conditions in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 49

ITPLA1-B22

LESSON VI: SELECTION AND LOOP STRUCTURES


SELECTION AND LOOP STRUCTURES
LEARNING OUTCOMES

 Determine the results of relational operators


 Evaluate expressions that contain logical operators
RELATIONAL OPERATORS

 Computers are able to compare two values.


 Expressions that compare operands (values or variables) are
called relational expressions.
 The outcome or result of such a comparison always yields a
Boolean value which is either true or false.
 Examples :
 5 > 3 result: true
 16 == 5 result: false
RELATIONAL OPERATORS
RELATIONAL OPERATORS

Relational operators have equal priority, but mathematical


operators have a higher priority than the relational
operators.
Example:
 Determine whether the following expression is true or false:

1. b <= a-3 where b = 5 and a = 7


5 <= 7-3
5 <= 4 Outcome of relational comparison: false
RELATIONAL OPERATORS

2. m + 7 > n-3 * p where m = 4, n = 12 and p = 5


4 + 7 > 12-3 * 5
4 + 7 > 12-15
11 > 12-15
11 >-3
 Outcome of relational comparison: true
RELATIONAL OPERATORS: EXERCISES

 Note : In computing and mathematics, the term ‘evaluate’ is often


used to mean ‘find the result’ for example, the equation evaluates
to zero or another value, or it evaluates to True or False.
 Evaluate the following expressions:
1. a + t <> (a + b) * 4 where a = 4, b = 17 and t = 20
2. k ^ 2 <= m mod 5 + 29-n where k = 5, m = 63, n = 7
3. (x\8-2) + 3 > y / 12 mod 2 where x = 59, y = 96
4. d + 4 * g / 2 < g * 3 + 14 where d = 3, g = 24
RELATIONAL OPERATORS: EXERCISES

a + t <> (a + b) * 4 where a = 4, b = 17 and t = 20


4+20<>(4+17)*4
24<>84
True
RELATIONAL OPERATORS: EXERCISES

k ^ 2 <= m mod 5 + 29-n where k = 5, m = 63, n = 7


5^2<= 63 mod 5+29-7
25<=3+29-7
25<=25
True
LOGICAL OPERATORS
LOGICAL OPERATORS

 When different operators are combined in one expression, all the


mathematical operators (highest priority, from left to right) will be
processed first, then the relational operators (second highest
priority, from left to right), and finally, the logical operators (lowest
priority, from left to right).
 However, the order can be changed using parentheses. For
example, if an OR must be processed before an AND,
parentheses can be inserted.
LOGICAL OPERATORS: EXAMPLE

 Example:

Let A, B, C and X be Boolean variables with the following


values:
A = True, B = False and C = True Using parentheses:
Evaluate the equation:
X = (A OR B) AND C
X = A OR B AND C = (True OR False) AND True
Substitute: = True AND True
= True
X = True OR False AND True
AND operator is done first:
X = True OR False
= True
LOGICAL OPERATORS: EXAMPLE

 Evaluate the following expressions:


D OR K AND NOT S where D = TRUE, K = FALSE, S = TRUE
TRUE OR FALSE AND NOT TRUE
TRUE OR FALSE AND FALSE
TRUE OR FALSE
TRUE
LOGICAL OPERATORS: EXAMPLE

2. M > 7 AND D < S ^ 2 where M = 7, D = 8, S = 4


Substitute:
7 > 7 AND 8 < 4 ^ 2
7 > 7 AND 8 < 16
FALSE AND 8 < 16
FALSE AND TRUE
FALSE
LOGICAL OPERATORS: EXAMPLE
3. D < (E + 7) AND G OR (A * C) >= (D - E + C) where A = 5, C = 12, D = 15, E =17, G = FALSE
Substitute:
-15 < ( 17 + 7) AND FALSE OR (5 * 12) >= ( 15 -17 + 12)
-15 < ( 17 + 7) AND FALSE OR (5 * 12) >= ( 15 + 17 + 12)
-15 < 10 AND FALSE OR (5 * 12) >= ( 15 + 17 + 12)
-15 < 10 AND FALSE OR 60 >= ( 15 + 17 + 12)
-15 < 10 AND FALSE OR 60 >= (2 + 12)
-15 < 10 AND FALSE OR 60 >= 14
TRUE AND FALSE OR 60 >= 14
TRUE AND FALSE OR TRUE
FALSE OR TRUE
TRUE
LOGICAL OPERATORS: EXERCISES

 Evaluate the following expressions where A = 5, B = 7, C = 12, D = -4,


E = TRUE and F = FALSE:

1. A * 3 >= B –14 \3 AND F OR C <= D + 3


2. NOT F OR E AND D + C = A
3. (C –D) * 2 <> A + B OR E AND F OR B > D + B ^ 2
4. D = C MOD 5 AND NOT F OR NOT (A + D <= A –D)
LEARNING OUTCOMES

 Prepare test data to test all possible conditions for a program that
contains decision structures.
 Write algorithms containing Simple if statements, If-then-else
statements, Compound if statements and Boolean variables.
 Code defensively and do data validation where-ever possible.
if statement

if statements are also called if then statements because


they cause the computer to test a condition before
executing the ‘then’ part of the statement.
If the condition is true, the ‘then’ part is executed.
If not, the if statement is skipped entirely and the computer
continues executing the rest of the program.
PSEUDOCODE EXAMPLE

~ Test if the employee worked overtime


if (hours > 45) then
wage = wage + 300 ~ Executed only if the
condition is true
endif
if statement
Notes about if statement

 There can be any number of statements in the body of an if statement


between if and endif.
 The statements in the body of an if statement execute only if the condition is
TRUE.
 Statements within an if statement are always indented to aid the readability of
the algorithm.
 if and endif are written in the same column, whereas the body of the if
statement is indented.
 An if statement always yields a Boolean value, so the result is always TRUE
or FALSE.
if statement
if statement: example
if statement: example
if statement: example

Planning Input and output variables:


Intermediate variables are used in this solution to help with
the calculations.
These intermediate variables aren’t part of the input or the
output.
if statement: example
IPO CHART

Input Processing Output


Expense Prompt for expense slicePriceInteger
Enter expense
Calculate slicePriceInteger
Display slicePriceInteger
Algorithm

CalcCakePrice
~ Calculate the selling price of cake
display “Please enter the cost of the cake”
enter expense
~ Add profit and calculate price per slice
cakePrice= expense + expense * 0.3 ~ Intermediate variable cakePrice
slicePriceInteger = cakePrice \ 10
slicePriceDecimal = cakePrice / 10
Algorithm

If slicePriceInteger <> slicePriceDecimal then ~ Test if any cents


slicePriceInteger = slicePriceInteger + 1
endif
~ Display the price of a slice of cake
display “The price of a slice of cake is R” ,slicePriceInteger
end
Trace Table
Example
Planning
IPO Chart
Algorithm for if then else statement

ReadingSpeed
display “Provide number of pages per book ”
enter bookPages
display “Provide number of pages Dennis can read per day ”
enter dayPages
~ Calculate days per book
bookDays = bookPages / dayPages
~ Determine days to read 5 books
days = bookDays * 5 ~ An intermediate variable is used
Algorithm for if then else statement

display “Dennis takes ”,bookDays , “ days per book ”


~ Test if five books can be read during holidays and display output
if days <= 14 then
message = “Dennis can read five books during the holidays.”
else
message = “It is not possible for Dennis to read five books during the holidays.”
endif
display message
end
Compound if statements

Programmers often find that more than one condition must


be tested before the result can be true. A compound if
statement, which is based on logical operators, can be
used to resolve this.
Example :
 If a person is 18 years old or older and has a driver’s license,
this person may drive a car.
Compound if statements
Compound if statements
Compound if statements
Compound if statements
Compound if statements
 Algorithm :
DetermineEntranceFee
~ Test age
~ Enter input data if age < 12 OR age > 59 then
actFee = fee-fee * 0.25 ~ Subtract discount
display “Enter the normal fee ” else
enter fee actFee= fee
endif
display “Enter the age in years only ” ~ Display actual fee on a new line
display “This person must pay an entrance fee of R
enter age ”,actFee
end
Data validation

 It is a good programming technique to code defensively, which means that


you try to detect and identify all errors that a user could possibly make.
 Users are often inexperienced so many computer errors are actually a result
of incorrect user input.
 If a program can detect an error before processing, the information will be
accurate and reliable.
 There are a number of data validation tests that a programmer can use to
code defensively.
 At this point, we’ll only test whether a numeric field contains a numeric value
before it can be used for processing. In other words, the program will not be
able to do calculations if the variable does not contain a numeric value.
Data validation
Nested if statements

 Check following if then else statement :


if condition1 then
statement1 ~ Process when condition is true
else
statement2 ~ Process when condition is false
endif
 Statement1 and/or statement2 could also be if then else statements,
depending on the problem statement, which would change the general format
to a nested if statement, as follows:
Nested if statements
if age>=18 then
if license=yes then
display "You can drive"
else ~ If condition1 is true
display "ÿou cant drive a car
endif
else
if äge=16 then
Display "ÿou can drive a motorcycle"
else ~ If condition1 is false
display "you are not qualified to drive"
endif
endif
Nested if statements
NESTED IF STATEMENTS EXAMPLE

 The Bright Light Company is increasing the salaries of its employees according to which department
they work in, as shown in Table 7

Department Code Percentage Increase

A 7.2

B 6.8

All others 6.3


NESTED IF STATEMENTS EXAMPLE

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy