0% found this document useful (0 votes)
8 views19 pages

Selection Structures 1

Uploaded by

tabioloadrian356
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views19 pages

Selection Structures 1

Uploaded by

tabioloadrian356
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

SELECTION STRUCTURES

Specific Learning Outcomes

At the end of the lesson, the student will be able to:


• examine relational and logical structures;
• explore how to form and evaluate logical
(Boolean) expressions; and
• discover how to use the selection control
structures.
Relational Operators and Expressions

Relational Operators are used in


Relational Expression/s to compare
the values of any two expressions. A
relational expression will evaluate to a
logical value of either true (.T.) or false
(.F.). A true or false expression is also
called a logical expression or
condition.
The following table contains relational operators:

RELATIONAL OPERATORS
OPERATOR OPERATION PRIORITY
== Equal to (the same) 1
Equality
!= Not equal to (not the same as) 1
Greater than (more, bigger, larger, higher, longer
> 1
than, exceeds, over)

Relational < Less than (smaller, fewer, lower, shorter than, below) 1

>= Greater than or equal to (at least) 1


<= Less than or equal to (at most) 1
The computer evaluates first the operator with the highest priority (1). For same priority, evaluation is
from left to right. Operations inside parentheses () if any are evaluated first.
Example
Relational Expression Used To Determine If
score > 90 score literal value is over 90
surname = “dela Cruz” surName literal value is “dela Cruz”
age <=17 age literal value is below or equal to 17
course != “BSIT” course literal value is not “BSIT”
sales >= 50000 sales has reached the quota of ₱50000.00
failed != .T. failed is not true (.T.)
Logical Operations & Expressions

Logical operators are used in connection


with true (.T.) or false (.F.) relational
expressions that compare the values of
some data.

Logical operators evaluate Boolean


expressions and return a Boolean value.
A logical expression, which consists of one
or more relational expressions and logical
operators, will also evaluate to either true
or false.
The following table contains arithmetic operators:

LOGICAL OPERATORS
OPERATOR OPERATION PRIORITY

.NOT. Negation 1

.AND. Logical Multiplication 2

.OR. Logical Addition 3

The computer evaluates first the operator with the highest priority (1). For same
priority, evaluation is from left to right. Operations inside parentheses ()
if any are evaluated first.
A B .NOT. A .NOT. B A .AND. B A .OR. B

.F. .F. .T. .T. .F. .F.

.F. .T. .T. .F. .F. .T.

.T. .F. .F. .T. .F. .T.

.T. .T. .F. .F. .T. .T.


Selection Structures

One of the most significant structures in programming


is the selection structure.

There are more ways to represent these choices or


options and these are the following:
• One – way selection structure
• Two – way selection structure
• Multiple selection structure
One – way selection structure

In one – way selection structure,


a user is given a certain condition
T F
wherein he is allowed to choose. As condition

the user has chosen, his response Task


will then be evaluated. If the
response satisfies the condition,
then, a certain task will be done.
Write an algorithm that determines if the input number is
positive. Consider 0 as positive number.

I = Num
P = Num >= 0, display “Positive Number”
O = Positive Number
PSEUDOCODE FLOWCHART
START

Input Num Num

F
if (Num >= 0) Num >= 0

T
“Positive
display “Positive Number” Number”

END
Two – way selection structure

In two – way selection structure,


a user will be given two choices of T condition
F
a certain situation wherein he is Task 1 Task 2
allowed to choose one, not both,
from the two (2) choices.
Write an algorithm that determines if the input number is
POSITIVE or NEGATIVE. Consider 0 as positive number.

I = Num
P = Num >= 0, display “Positive Number”
otherwise display “Negative Number”
O = Positive Number, Negative Number
PSEUDOCODE FLOWCHART
START

Input Num Num

T F
if (Num >= 0) Num >= 0

display “Positive Number”


“Positive “Negative
else Number” Number”
display “Negative Number”

END
Multiple/Nested-If Selection Structure

In multiple selection structure,


as the word implies, this structure T condition
F
simply means that three (3) or more Task 1
T condition
F
selections are given in a single
situation. Task 2 Task 3
Write a program that examines the value of a variable called Temp.
Then Display the following messages depending on the value
assigned to Temp.
Temperature Message
Less than 0 ICE
Between 0 and 100, inclusive WATER
Exceeds 100 STREAM

I = Temp
P = Temp < 0, display “ICE” Temp <= 100,
display “WATER”, otherwise display
“STREAM”
O = Ice, Water, Stream
PSEUDOCODE FLOWCHART
START

Input Temp Temp

if (Temp < 0) Temp < 0


T “ICE”
display “ICE”
else if (Temp <= 100) F
display “WATER” Temp <= T “WATER”
100
else F
display “STREAM”
“STREAM”

END
Laboratory Exercise

Write a program that will examine the value of a variable


AGE. display the following messages depending on the value
assigned to AGE.
AGE Message
• Less than 18 MINOR
• greater than equal to 18 and ADULT
less than 60
• Greater than equal to 60 SENIORS

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