0% found this document useful (0 votes)
110 views20 pages

Lesson 9:: The Selection Structure

This chapter discusses selection structures in programming, including relational and logical operators, flowcharting selection structures, and coding them in Visual Basic. It covers if-then, if-then-else, if-then-elseif statements, and select case structures. Examples shown include determining the absolute value of a number, qualifying age to vote, high school level based on year, and messages for temperature ranges. The quiz questions ask to create programs using selection structures to check grade ranges and determine vowels vs consonants.

Uploaded by

Clarisse Paras
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)
110 views20 pages

Lesson 9:: The Selection Structure

This chapter discusses selection structures in programming, including relational and logical operators, flowcharting selection structures, and coding them in Visual Basic. It covers if-then, if-then-else, if-then-elseif statements, and select case structures. Examples shown include determining the absolute value of a number, qualifying age to vote, high school level based on year, and messages for temperature ranges. The quiz questions ask to create programs using selection structures to check grade ranges and determine vowels vs consonants.

Uploaded by

Clarisse Paras
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/ 20

LESSON 9:

THE SELECTION STRUCTURE


THIS CHAPTER DISCUSSING HOW TO MAKING DECISION IN A
PROGRAM AND THE USES OF RELATIONAL AND LOGICAL
OPERATOR. IT IS INCLUDING THE FLOWCHARTING AND CODING
THE SELECTION CONTROL STRUCTURE.
OBJECTIVES

• Learn about Selection structures


• • Examine relational and logical operators
• • Explore how to form and evaluate logical (Boolean) expressions
• • Learn how to use the selection control structures if then, if then
else, if then elseif and select case in a program
MAKING DECISIONS IN A PROGRAM

• Altering the flow of program execution by making a selection or


choice
• Use logical relation and answerable by true or false
• Selection structure indicates that a decision (based on some
condition) needs to be made, followed by an appropriate action
derived from that decision.
COMPARISON OPERATORS

• Allows you to make comparisons in a program and also known as


relational Operator
Expression Operator Meaning Meaning Value
8 < 15 = 8 is less than 15 Equal
6 <> 6 > 6 is not equal to 6 Greater than
2.5 > 5.8 < 2.5 is greater than 5.8 Less than
5.9 <= 7.5 >=
5.9 is less than or equal
Greater
to 7.5
than and/or Equal
<= Less than and/or Equal
<> Not Equal
LOGICAL OPERATORS

Operator Meaning
AND Both side must be true
OR One side or other must be true
NOT Negates truth
XOR One side or the other must be true but not both

• Sometimes we might need to make more than one comparison before a


decision can be made and an action taken.
• In this case, using numerical comparison operators alone is not sufficient,
we need to use additional operators, and they are the logical operators.
LOGICAL OPERATOR
Statement 1 Statement 2 Output Statement 1 Statement 2 Output
T T T T T T
T F F T F T
F T F F T T
F F F F F F
AND OPERATOR OR OPERATOR

Statement 1 Statement 2 Output


Statement Output T T F
T F T F T
F T F T T
F F F

NOT OPERATOR XOR OPERATOR


LOGICAL OPERATOR
Statement Output
(14 >= 5) AND (15 < 20)
(24 >= 35) AND (15 < 20)

(24 >= 35) OR (15 < 20)


(-7 <= 10) OR (4 > 29)
NOT(5 <= 7)
NOT(10 < 5)
(21 >= 8) XOR (2 < 7)
(143 < 143 ) XOR (123 > 834)
FLOWCHARTING A SELECTION STRUCTURE

One Way Selection Two Way Selection


CODING SELECTION STRUCTURE
IN VISUAL BASIC
IF THEN STATEMENT
• The if then statement is used for one way selection.
• This is the simplest control structure which instructs the
computer to perform a certain action specified by
the Visual Basic expression if the condition is true.
However, when the condition is false, no action will
be performed.
 The syntax for the If-Then statement is:

If condition Then
expression
End If
Example 1: Create a program the will display the absolute value
of the input number.
Solution to Example 1 using If-Then Statement
IF-THEN-ELSE STATEMENT
• If-Then-Else Statement is used for two way selection
• This control structure will ask the computer to perform
a certain action specified by the Visual Basic
expression if the condition is met. And when the
condition is false, an alternative action will be
executed.
 The syntax for the If-Then statement is:
If condition Then
Visual Basic expression
Else
Visual Basic expression
End If
Example 2: Create a program that will determine if the input age is
qualified to vote or not. The qualifying age is 18 years old and above.
Solution to Example 2 using If-Then-Else Statement
IF-THEN-ELSEIF STATEMENT
• If-Then-ElseIf Statement is used for multiple selection
• If there are more than two alternative choices, using just
If-Then-Else statement will not be enough. In order to
provide more choices, we can use the If-Then-ElseIf
statement executed.
 The general format of the If-Then-ElseIf statement is:
If condition Then
Statement
ElseIf condition Then
Statement
Else
Statement
End If
Example 3: Create a program to display the high school level of a
student, based on its year-entry. For example, the year entry 1 means
the student list a freshmen, 2 for sophomore and so on. Here are the
given criteria
Criteria:
Year-Entry Level High School Level
1 “Freshmen”
2 “Sophomore”
3 “Junior”
4 “Senior”
Other entry “Out of School”
SELECT CASE STATEMENT
•The
The syntax
Select of the Select
Case controlCase control
structure is structure in
slightly different
Visual Basic
from the is as follows:
If-ElseIf control structure.
• TheSelect
difference is that
Case test the Select Case control structure
expression
basically only makelistdecision
Case expression 1 on one expression or
dimension
Blockwhile theorIf-ElseIf
of one statement control
more statements
Case expression
structure list 2 only one expression, each If-
may evaluate
Block of one
ElseIf statement mayor more statementsentirely different
also compute
Case expression list 3
dimensions.
….
Case Else
Block of one or more statements
End Select
Example 4: Create a program that examines the value of a variable called
temperature Then display the following messages, depending on the value
assigned to temp

Criteria:

Temperature Message
Less than 0 ICE
Between 0 and 100 Water
Exceeds 100 Steam
QUIZ #6
DIRECTION: READ THE FOLLOWING
ASSIGNMENT/PROBLEM SOLVING
AND ANSWER IT.
LESSON 9 – THE SELECTION STRUCTURE
1. CREATE A PROGRAM THAT WILL DETERMINE IF THE
GRADE IS “PASSED”, “FAILED” OR “NOT VALID”:
Grade Message
Between 75 to 100 Passed
Less than 75 Failed
Exceeds 100 Not Valid

a.) Create using Flowchart


b.) Create A Program Using VB.NET
- IF THEN ELSEIF STATEMENT
- SELECT CASE STATEMENT
2. CREATE A PROGRAM THAT WILL DETERMINE IF
THE INPUT LETTER IS VOWEL OR CONSONANT

a.) Create using Flowchart


b.) Create A Program Using VB.NET
- IF THEN ELSEIF STATEMENT
- SELECT CASE STATEMENT

You might also like