0% found this document useful (0 votes)
33 views25 pages

Visual Basic.NET Decision Making Guide

This document provides an overview of decision-making structures in Visual Basic.NET, focusing on the If...Then...Else statement, relational operators, and the IIf function. It also introduces the Select Case statement as an efficient alternative for handling multiple conditions. Examples illustrate the use of these structures in programming contexts.

Uploaded by

Lakeishaa Edward
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)
33 views25 pages

Visual Basic.NET Decision Making Guide

This document provides an overview of decision-making structures in Visual Basic.NET, focusing on the If...Then...Else statement, relational operators, and the IIf function. It also introduces the Select Case statement as an efficient alternative for handling multiple conditions. Examples illustrate the use of these structures in programming contexts.

Uploaded by

Lakeishaa Edward
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

Programming In Visual Basic.

NET

Lesson 5
Decisions and Conditions
If …Then…Else
If …Then…Else
• Used to make decisions
• Always indent for
readability & debugging
• Then must be on same line
as If or ElseIf
• End If and Else must appear
alone on a line
• Notice that ElseIf is 1 word,
End If is 2 words
• Always End with End If

3
If Syntax
If (boolean_expression 1) Then
' Executes when the boolean expression 1 is true
ElseIf (boolean_expression 2) Then
' Executes when the boolean expression 2 is true
ElseIf (boolean_expression 3) Then
' Executes when the boolean expression 3 is true
Else
' executes when the none of the above condition is true
End If

4
Relational Operators
• Greater Than >
• Less Than <
• Equal To =
• Not Equal To <>
• Greater Than or Equal To >=
• Less Than or Equal to <=

5
Comparison Tips
• Negative numbers are always less than
positive numbers
• Strings can be compared also (don't forget to
enclose the strings in quotes)
• JOAN is less than JOHN
• HOPE is less than HOPELESS
• Joan does not equal JOAN
• Numbers are always less than letters
• 300ZX is less than Porsche

6
ASCII
If Example #1
Sub IfDemo1(num1 As Integer) Sub setRadio()
setRadio() [Link] = False
If num1 < 10 Then [Link] = False
[Link] = True [Link] = False
ElseIf num1 < 20 Then [Link] = False
[Link] = True End Sub
ElseIf num1 < 30 Then
[Link] = True Sub demo1() Handles [Link]
Else Dim intCredits As Integer
[Link] = True intCredits = Val([Link])
End If
End Sub IfDemo1(intCredits)
End Sub

8
If Example # 2
If blnSuccessfulOperation = True Then
...
End If

is equivalent to

If blnSuccessfulOperation Then
...
End If

9
Compound Conditions
• Join conditions using Logical Operators
• Or either true evaluates to true
• And both true evaluates to true
• Not reverses the condition, so that true will evaluate false and
false will evaluate true

10
Compound Conditions Example
Sub IfDemo2(ageGroup As Integer)
If ageGroup <= 12 And [Link] Then
[Link] = "Children"
ElseIf ageGroup <= 17 And [Link] Then
[Link] = "Adolescents"
ElseIf ageGroup <= 65 And [Link] Then
[Link] = "Adults"
ElseIf ageGroup <= 100 And [Link] Then
[Link] = "Older Adults"
Else
[Link] = "This survey is for male only"
End If
End Sub

Sub Button1_Click() Handles [Link]


Dim userInput As Integer = Val([Link])
IfDemo2(userInput)
End Sub
11
Nested Ifs
Sub nestedIfDemo(temperature As Integer)
If temperature > 32 Then
If temperature > 80 Then
[Link] = "Hot"
Else
[Link] = "Moderate"
End If
Else
[Link] = "Freezing"
End If
End Sub

Sub demo1() Handles [Link]


Dim temperature As Integer = Val([Link])
nestedIfDemo(temperature)
End Sub
12
Testing Radio Buttons & Check
Boxes
• Instead of coding the CheckChanged events, use IFs to see
which are selected
• Place the IF code in the Click event for a Button, such as
btnOK

Sub boldText()
If [Link] = True Then
[Link] = New Font([Link], [Link])
End If
End Sub

Sub demo1() Handles [Link]


boldText()
End Sub

13
IIF
Introduction
• The IIf function denotes immediate decision function.
• Syntax:
• IIf(logical_expression, exprIfTrue, exprIfFalse)
• logical_expression represents a logical expression
• exprIfTrue and exprIfFalse represent a numeric or a string expression.

• For example, the IIF(x>y, expression 1, expression 2)


function evaluates the values of x and y, if x>y. then
expression 1 is true, otherwise the expression 2 is true.
Example
Sub IFFDemo1()
Dim a, b, ans As Integer
a = 2
b = 3
ans = IIf(a < b, b - a, a * b)
[Link](ans) 'return 1
End Sub

Sub IFFDemo2()
Dim str1, str2, ans As String
str1 = "A"
str2 = "a"
ans = IIf(str1 < str2, "APPLE", "apple")
[Link](ans) 'return APPLE
End Sub
CASE Statement
SELECT CASE structure
• A Select Case block is an efficient decision-making structure
• Simplifies choosing among several actions
• It avoids complex If constructs
• If blocks make decisions based on the truth value of a
condition
• Select Case choices are determined by the value of an
expression called a selector
• Syntax

18
SELECT CASE Example 1
Sub caseDemo1()
Dim position As Integer = 5

Select Case position


Case 1
[Link] = "Red"
Case 2
[Link] = "Green"
Case 3
[Link] = "Blue"
Case 4, 5
[Link] = "Yellow"
Case Else
[Link] = "Black"
End Select
End Sub
19
SELECT CASE Example 2
Sub caseDemo2()
Dim position As Integer = -399

Select Case position


Case 1, 2, 3
[Link] = "Red"
Case 11 To 15, 21 To 21
[Link] = "Green"
Case Is > 50
[Link] = "Blue"
Case Else
[Link] = "Black"
End Select
End Sub

20
SELECT CASE Example 3
Sub caseDemo3()
Dim num1 As Integer = 3
Dim num2 As Integer = 1
Dim position As Integer = 1

Select Case position


Case num1 - num2, num1
[Link] = "Red"
Case Is <= 4
[Link] = "Green"
Case num1 + num2 To num1 * num2
[Link] = "Blue"
Case 7, 8
[Link] = "Yellow"
Case Else
[Link] = "Black"
End Select
End Sub
21
SELECT CASE Example 4
Sub caseDemo4()
Dim position As String = "Ali"
Select Case position
Case "ALI"
[Link] = 1
Case "M", "MARY", "Mary"
[Link] = 2
Case Else
[Link] = 3
End Select
End Sub

22
SELECT CASE Example 5
Sub caseDemo4()
Dim position As String = "Ali"
Select Case position
Case "ALI"
[Link] = 1
Case "M", "MARY", "Mary"
[Link] = 2
Case Else
[Link] = 3
End Select
End Sub

23
SELECT CASE Example 5
Sub caseDemo5()
Dim position As String = “SAM”
Select Case [Link](0, 1)
Case "S", "Z"
[Link] = “Begins with a sibilant.”
Case "A" To "Z"
[Link] = “Begins with a nonsibilant."
Case 0 To 9
[Link] = “Begins with a digit.”
Case Is < 0
[Link] = “A character of ANSI < 48.”
Case Else
[Link] = “Begins with a symbol.”
End Select
End Sub
24
SELECT CASE Example 6
Sub caseDemo6()
Dim positionX As Integer = 1
Dim positionY As Integer = 3
Select Case positionX
Case 1 To 5
[Link] = "Red"
Select Case positionY
Case 1 To 5
[Link] = "Within the region"
Case Else
[Link] = "Out of the region"
End Select
Case Else
[Link] = "Black"
End Select
End Sub

25

You might also like