Lesson 9:: The Selection Structure
Lesson 9:: The Selection Structure
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
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