Nested Conditional StatementsUntill now, we could program simple conditional statements:
If the password is xxx - do something,
and if not - do other thing.
But what if we want to write more complicated conditional statements:
If the password is xxx - do something,
If the password is yyy - do something else,
If the password is zzz - do something else,
and if the password is none of the above - do something else.
To do that we'll use "ElseIf":
Dim Number As Integer
Number = InputBox("Please enter your age")
If (Number > 50) Then
MsgBox "you are older than 50"
ElseIf (Number < 20) Then
MsgBox "you are younger than 20"
Else
MsgBox "your age is between 20 and 50"
End If
How does that code above work:
First, the first boolean expression is being evaluated:
(Number > 50)
If it's equal to True then the line that following it
(MsgBox "you are older than 50") will be executed, and
the "If statement" will be ended here, so the next
boolean expression (Number < 20) will not be checked.
If it's equal to False, the next boolean expression will be checked:
(Number < 20)
If it's equal to True, the line that following it
(MsgBox "you are younger than 20") will be executed, and
the "If statement" will be ended here.
If it's equal To False, the "Else Statement" will be executed:
MsgBox "your age is between 20 and 50"
Only 1 of the lines will be executed!
You can use more than one "ElseIf" in your "If Statement",
For example:
Dim Name As String
Name = InputBox("Please enter your name")
If (Name = "elvis") Then
MsgBox "your name is elvis"
ElseIf (Name = "tim") Then
MsgBox "your name is tim"
ElseIf (Name = "john") Then
MsgBox "your name is john"
ElseIf (Name = "steve") Then
MsgBox "your name is steve"
Else
MsgBox "I don't know you"
End If
If the password is xxx - do something,
and if not - do other thing.
But what if we want to write more complicated conditional statements:
If the password is xxx - do something,
If the password is yyy - do something else,
If the password is zzz - do something else,
and if the password is none of the above - do something else.
To do that we'll use "ElseIf":
Dim Number As Integer
Number = InputBox("Please enter your age")
If (Number > 50) Then
MsgBox "you are older than 50"
ElseIf (Number < 20) Then
MsgBox "you are younger than 20"
Else
MsgBox "your age is between 20 and 50"
End If
How does that code above work:
First, the first boolean expression is being evaluated:
(Number > 50)
If it's equal to True then the line that following it
(MsgBox "you are older than 50") will be executed, and
the "If statement" will be ended here, so the next
boolean expression (Number < 20) will not be checked.
If it's equal to False, the next boolean expression will be checked:
(Number < 20)
If it's equal to True, the line that following it
(MsgBox "you are younger than 20") will be executed, and
the "If statement" will be ended here.
If it's equal To False, the "Else Statement" will be executed:
MsgBox "your age is between 20 and 50"
Only 1 of the lines will be executed!
You can use more than one "ElseIf" in your "If Statement",
For example:
Dim Name As String
Name = InputBox("Please enter your name")
If (Name = "elvis") Then
MsgBox "your name is elvis"
ElseIf (Name = "tim") Then
MsgBox "your name is tim"
ElseIf (Name = "john") Then
MsgBox "your name is john"
ElseIf (Name = "steve") Then
MsgBox "your name is steve"
Else
MsgBox "I don't know you"
End If
কোন মন্তব্য নেই:
একটি মন্তব্য পোস্ট করুন