Introduction to Visual Basic
• 2nd Course in Introduction to Computer Programming Visual
Basic Specialization
© LearnQuest 2015
2021
Decision Branching
In the third module of this course, we will learn how to change
the flow of a program’s execution based on a test.
2 © LearnQuest 2021
Learning Objectives
Decision Branching
Upon completion of this module, learners will be able to:
• Develop programs that utilize if statements
• Develop programs that utilize if/else statements
• Develop programs that utilize case statements
3 © LearnQuest 2021
Lesson 1 In this lesson we look at the
Boolean Expressions evaluation of Boolean expressions
4 © LearnQuest 2021
Relational Operators
Operator Name
= Equal to
<> Not equal to
> Greater than
< Less than
>= Greater than or equal
to
<= Less than or equal to
Is Is
IsNot IsNot
5 © LearnQuest 2021
Example Relational Operator Usage
grade > 90
6 © LearnQuest 2021
Logical Operators
Operator Description
And Returns a True value if both expressions
are True. This operator always evaluates
both expressions.
Or Returns a True value if either expression is
True. This operator always evaluates
both expressions.
AndAlso Returns a True value if both expressions
are True. This operator only evaluates
the second expression if necessary.
OrElse Returns a True value if either expression is
True. This operator only evaluates the
second expression if necessary.
Not Reverses the value of the expression.
7 © LearnQuest 2021
Example Logical Operator Usage
grade > 90 AND status = “Completed”
8 © LearnQuest 2021
Lesson 1
Review Relational operators evaluate to
True or False
Logical operators allow you to build
more complicated expressions
Both operands must be True for the
expression to be True with an And
operator
9 © LearnQuest 2021
Lesson 2 In this lesson we look at how we can
modify the flow of a program with
If Statements an If statement
10 © LearnQuest 2021
Minimum If Statement Syntax
If condition Then
statements
End If
11 © LearnQuest 2021
Example If Statement
1. Dim yourname as String
2. yourname = Console.ReadLine()
3. Console.Write(“Hello “)
4. If yourname = “Sally” Then
5. Console.WriteLine(“Sally”)
6. End if
12 © LearnQuest 2021
Lesson 2
Review If Statements allow the program to
skip lines of code
A condition must evaluate to True
for the statements in the if to be
executed
You can have as many lines as you
like inside the if block
13 © LearnQuest 2021
Lesson 3 In this lesson we look expand our if
Else Clause statement to have an else clause
14 © LearnQuest 2021
Complete If Statement Syntax
If condition Then
statements
[ElseIf condition Then
statements] ...
[Else
statements]
End If
15 © LearnQuest 2021
Example If Statement with Else
1. Dim yourname as String
2. yourname = Console.ReadLine()
3. Console.Write(“Hello “)
4. If yourname = “Sally” Then
5. Console.WriteLine(“Sally my friend”)
6. Else
7. Console.WriteLine(yourname)
8. End if
16 © LearnQuest 2021
Example If Statement with ElseIf
1. Dim yourname as String
2. yourname = Console.ReadLine()
3. Console.Write(“Hello “)
4. If yourname = “Sally” Then
5. Console.WriteLine(“Sally my friend”)
6. Elseif yourname = “Fred” Then
7. Console.WriteLine(“Fred my exfriend”)
8. Else
9. Console.WriteLine(yourname)
10. End if
17 © LearnQuest 2021
Lesson 3
Review The else clause will run if no if or
elseif clause evaluates to True
You can have as many elseif
clauses in the if block
Only one if clause is allowed in an if
statement
18 © LearnQuest 2021
Lesson 4 In this lesson we look at alternative
way to change the flow of a
Case Statements program with a case statement
19 © LearnQuest 2021
Case Statement Syntax
Select Case testexpression
[Case expressionlist
statements] ...
[Case Else
statements]
End Select
20 © LearnQuest 2021
Example Case Statement
1. Dim yourname as String
2. yourname = Console.ReadLine()
3. Console.Write(“Hello “)
4. Select Case yourname
5. Case “Sally”
6. Console.WriteLine(“Sally my friend”)
7. Case “Fred”
8. Console.WriteLine(“Fred my exfriend”)
9. Case Else
10. Console.WriteLine(yourname)
11. End Select
21 © LearnQuest 2021
Third Programming Assignment
• There are one parts to your third programming assignment
1. Write a program that reads in a number and prints the either Small, Medium or
Large depending on if the number is bellow 100 or above 200.
• For example, if the user enters 150 the program should display “Medium”
• Another example, is if the user enters 50 the program should display “Small”
22 © LearnQuest 2021
Lesson 4
Review Case statements can express the
same as if statements
When testing for different values of
an expression a case statement is
more concise
The Case Else runs if no other case
statement evaluates to True
23 © LearnQuest 2021