Conditional statements are used to execute a group of statements depending on conditions that are satisfied. These are the basics you need to know in order start scripting. Almost every script has one of these statements implemented. Remember, These are not as frightening as they look. Its only because we do not understand them in detail. Now Sit, relax and learn.
Conditional statements :
The below are the commonly used conditional statements.
- If
- If Else
- If Else-if
- Select Case
If :
Performs an operation only if the condition is True. If it is false then the execution ends.
Syntax:
If <condition satisfied> Then < Execute Statements> End If
Example:
Age= 26 If Age= 26 Then Msgbox "not a kid" End If
If… Else :
Performs the If portion only if the condition is True. Otherwise Else Operation is executed. Firstly, it checks for IF condition, if it is satisfied then corresponding statements are executed and then End If. When IF condition fails then the execution goes into Else and the statements are executed.
Syntax:
If <condition> Then <Statements> Else <Statements> End If
Example:
Consider a vending machine which has snacks to offer for the customer.
userInput = InputBox ("Enter number less than 10") If userInput = 10 Then Msgbox "You have selected Uncle Chips" Else Msgbox "Please Choose a number less than 10" End If
Consider the example of vending machine where these conditions can be implemented. Try thinking how a machine operates and how these statements get executed.
If…Else-If :
Only one section can be executed even if many conditions are True. Last Else is optional.
Syntax:
If <condition> Then <Statements> ElseIf <condition> Then <Statements> Else <Statements> End If
Example:
input1 = InputBox ("Enter any number") input2 = InputBox ("Enter any number") result = input1 * input2 If result > 0 Then Msgbox "Both the inputs are either positive or negative" ElseIf result < 0 Then Msgbox "One of the input is negative number" ElseIf result = 0 Then Msgbox "One of the input is 0" End If
Nested If :
If… Else statements can be nested to as many levels as you need. Else block is optional.
Syntax:
If <condition> Then <Statements> If <condition> Then <Statements> If <condition> Then <Statements> Else <Statements> End If Else <Statements> End If End If
Select Case :
Select Case statement is used for selecting one statement from multiple statements.Inclusion of Case Else is optional
Syntax:
Select Case <variable name> Case "value1" <Statements> Case "value2" <Statements> Case "value3" <Statements> Case "value4" <Statements> Case else <Statements> End Select
Example: Display day name for user entered number
userInput = InputBox ("Enter any number <= 7") Select Case userInput Case "1" Msgbox "Monday" Case "2" Msgbox "Tuesday" Case "3" Msgbox "Wednesday" Case "4" Msgbox "Thursday" Case "5" Msgbox "Friday" Case "6" Msgbox "Saturday" Case "7" Msgbox "Sunday" Case Else Msgbox "Invalid Input" End Select
All these conditional statements come very hand and proper usage will make your code more effective and clear. Learn by implementing these statements with your own examples.