YouTip LogoYouTip

Vbscript Conditionals

* * * ## Conditional Statements Conditional statements are used to perform different actions based on different conditions. In VBScript, we can use four types of conditional statements: * **If statement** - Use this statement if you want to execute a series of code only when a condition is true * **If...Then...Else statement** - Use this statement if you want to execute one of two blocks of code * **If...Then...ElseIf statement** - Use this statement if you want to select one of many blocks of code to execute * **Select Case statement** - Use this statement if you want to select one of many blocks of code to execute * * * ## If...Then...Else You can use the If...Then...Else statement in the following scenarios: * To execute a block of code only when a condition is true * To select one of two blocks of code to execute If you want to execute only **one** statement when the condition is true, you can write the code on a single line: If i=10 Then alert("Hello") In the code above, there is no ..Else.. clause. We simply let the code perform **one action** if the condition is true (when i=10). If you want to execute **more than one** statement when the condition is true, you must write each statement on a separate line and end the statement with the keyword "End If": If i=10 Then alert("Hello") i = i+1 End If In the code above, again there is no ..Else.. clause. We simply let the code perform **multiple actions** if the condition is true. If you want to execute one statement when a condition is true and another statement when the condition is not true, you must add the "Else" keyword: ## Example (For IE only) i=hour(time) If i < 10 Then document.write("Good morning!") Else document.write("Have a nice day!") End If [Try it Β»](#) In the code above, the first block of code will be executed if the condition is true, and the second block will be executed if the condition is false (when i is greater than 10). * * * ## If...Then...ElseIf If you want to select one of many blocks of code to execute, use the If...Then...ElseIf statement: ## Example (For IE only) i=hour(time) If i = j Then document.write("Just started...!") ElseIf i = 11 Then document.write("Hungry!") ElseIf i = 12 Then document.write("Ah, lunch-time!") ElseIf i = 16 Then document.write("Time to go home!") Else document.write("Unknown") End If [Try it Β»](#) * * * ## Select Case If you want to select one of many blocks of code to execute, use the "Select Case" statement: ## Example (For IE only) d=weekday(date) Select Case d Case 1 document.write("Sleepy Sunday") Case 2 document.write("Monday again!") Case 3 document.write("Just Tuesday!") Case 4 document.write("Wednesday!") Case 5 document.write("Thursday...") Case 6 document.write("Finally Friday!") Case else document.write("Super Saturday!!!!") End Select [Try it Β»](#) How the above code works: First, we have a single expression (usually a variable) that is evaluated once. The value of the expression is then compared with the value for each Case. If there is a match, the block of code associated with that Case is executed.
← Vbscript LoopingVbscript Procedures β†’