If-Then
In case of If-Then block, the condition is first checked. The condition can either be True or False. If it is True, the statements inside the If-Then block is executed. Otherwise, it does not execute the code in the If-Then block, the If-Then structure is skipped. It starts executing the statements just after the 'End if' phrase.
Syntax:
If Condition Then
statements
End If
Example: To check if the number is positive.
Dim num1 As Integer
num1 = 30
If num1 > 0 Then
Print "The number is positive"
End If
Output: The number is positive.
Single Line version of If-Then
Single line and single statement
If a > 0 Then b = 1
Single line and multiple statements
If a > 0 Then b = 1: c = 2
If-Else
An If-Else block has two parts. If the condition is True then the first part is executed, otherwise the control goes to the second part and starts executing the lines of statements in the second part of the If-Else block.
Syntax:
If Condition Then
statements1
Else
statements2
End If
If Condition is true then statements1 will be executed and if Condition is false, statements2 will be executed.
Example: To print the largest number.
Dim num1 As Integer, num2 As Integer
num1 = 30
num2 = 50
If num1 > num2 Then
Print num1
Else
Print num2
End If
Output: 50
Single line version of If-Else
If a > 0 Then b = 1 Else b = 0
In the next lesson, you'll learn more about the if-else statements.
If-Else-if
If you have programmed in other languages, you might probably know about nested if-else control flow block. In VB it is the same concept. See the syntax and the examples to capture the concept.
In if-else-if, the structure is little changed, little advanced better to say, from the simple If-Block. Here if the first condition does not satisfy, it looks for the next condition preceded by the ElseIf term, if that too does not satisfy, it looks for the next, and it goes on until the end of the structure.
Syntax:
If Condition Then
statements
ElseIf Condition then
statements
ElseIf Condition Then
statements
.......................
......................
Else
statements
End If
Example:
a = Val(InputBox("Enter a no."))
If a > 0 Then
Print "Positive"
ElseIf a < 0 Then
Print "Negative"
Else
Print "Zero"
End If
In a nested if-else block, one If-Block can reside in another. See the example below.
Nested-if-Else
In Nested-if-Else, the structure is little changed, little advanced better to say, from the simple If-Block. Here if the first condition does not satisfy, it looks for the next condition preceded by the ElseIf term, if that too does not satisfy, it looks for the next, and it goes on until the end of the structure.
Syntax:
If Condition Then
If Condition Then
statements
ElseIf Condition then
statements
End If
End If
Else
If Condition Then
statements
ElseIf Condition then
statements
End If
End If
Example:
'If-statement inside another if-statement
Dim a As Integer
a = Val(txtNumber.Text)
If a > 0 Then
Print "Positive"
If a > 2 Then
Print "Greater than 2"
Else
Print "Less than 2"
End If
Else
If a < 0 Then
Print "Negative"
End If
End If
Select Case Blocks
The Select Case block provides an efficient way for decision making which is used to execute one block of statement(s) among a list of statement blocks according to the determined value of an expression. The expression is preceded by the "Select Case" phrase. The choices are made based on the value of this expression.
Syntax:
Select Case expression
Case value0
statements
Case value1
statements
Case value2
statements
...........
...............
Case else
statements
End select
Example:
Dim num As Integer
num = Val(Text1.Text)
Select Case num
Case 0
Print "You have entered zero"
Case 1
Print "You have entered one"
Case 2
Print "You have entered two"
Case Else
Print "The number you entered is greater than 2"
End Select
One block of statement(s) will be executed depending upon the value of num.
If num=0 then, Case 0 will be evaluated and if num =1 then Case 1 will be evaluated.
See the other forms of Select Case Structure in the following programs.
Example:
Dim score As Integer
score = Val(Text1.Text)
Select Case score
Case 900 To 1000
Print "First position"
Case 800 To 899
Print "Second position"
Case 700 To 799
Print "Third position"
Case Else
Print "No position, try again"
End Select
Example:
Dim num As Integer
num = Val(Text1.Text)
Select Case num
Case Is > 0
Print "Positive number"
Case Is < 0
Print "Negative number"
Case 0
Print "Zero"
End Select
Example:
Select Case value
Case 0, 1, 2, 3, 4, 5
Print "The values are 0,1,2,3,4,5"
Case Else
Print "Other values"
End Select