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.
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:
Syntax:
1
2
3
| If <condition satisfied> Then < Execute Statements> End If |
Example:
1
2
3
4
| 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:
1
2
3
4
5
| If <condition> Then <Statements> Else <Statements> End If |
Example:
Consider a vending machine which has snacks to offer for the customer.
Consider a vending machine which has snacks to offer for the customer.
1
2
3
4
5
6
| 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 |
If…Else-If :
Only one section can be executed even if many conditions are True. Last Else is optional.
Syntax:
Syntax:
1
2
3
4
5
6
7
| If <condition> Then <Statements> ElseIf <condition> Then <Statements> Else <Statements> End If |
Example:
1
2
3
4
5
6
7
8
9
10
11
| 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:
Syntax:
1
2
3
4
5
6
7
8
9
10
11
12
13
| 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:
Syntax:
1
2
3
4
5
6
7
8
9
10
11
12
| 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| 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.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.