VBScript6-VBScript Conditional Statements

Conditional Statements Very often when you write code, you want to perform different actions for different decisions. You can use condit...

Conditional Statements

Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this.
In VBScript we have four conditional statements:
  • if statement - use this statement if you want to execute a set of code when a condition is true
  • if...then...else statement - use this statement if you want to select one of two sets of lines to execute
  • if...then...elseif statement - use this statement if you want to select one of many sets of lines to execute
  • select case statement - use this statement if you want to select one of many sets of lines to execute

If....Then.....Else

You should use the If...Then...Else statement if you want to
  • execute some code if a condition is true
  • select one of two blocks of code to execute
If you want to execute only one statement when a condition is true, you can write the code on one line:

if i=10 Then msgbox "Hello"

There is no ..else.. in this syntax. You just tell the code to perform one action if the condition is true (in this case if i=10). 

If you want to execute more than one statement when a condition is true, you must put each statement on separate lines and end the statement with the keyword "End If": 

if i=10 Then
   msgbox "Hello"
   i = i+1
end If

There is no ..else.. in this syntax either. You just tell the code to perform multiple actions if the condition is true. 

If you want to execute a statement if a condition is true and execute another statement if the condition is not true, you must add the "Else" keyword: 

if i=10 then
   msgbox "Hello"
else
   msgbox "Goodbye"
end If

The first block of code will be executed if the condition is true, and the other block will be executed otherwise (if i is not equal to 10).

If....Then.....Elseif

You can use the if...then...elseif statement if you want to select one of many blocks of code to execute:

if payment="Cash" then
   msgbox "You are going to pay cash!"
 elseif payment="Visa" then
   msgbox "You are going to pay with visa."
 elseif payment="AmEx" then
   msgbox "You are going to pay with American Express."
 else
   msgbox "Unknown method of payment."
end If


Select Case

You can also use the SELECT statement if you want to select one of many blocks of code to execute:

select case payment
 case "Cash"
   msgbox "You are going to pay cash"
 case "Visa"
   msgbox "You are going to pay with visa"
 case "AmEx"
   msgbox "You are going to pay with American Express"
 case Else
   msgbox "Unknown method of payment"
end select

This is how it works: First we have a single expression (most often a variable), that is evaluated once. The value of the expression is then compared with the values for each Case in the structure. If there is a match, the block of code associated with that Case is executed.
Name

ADO,131,ASP,3,C++,61,CORE JAVA,1,CSS,115,HTML,297,index,5,JAVASCRIPT,210,OS,47,PHP,65,SAD,53,SERVLETS,23,SOFTWARE ENGINEERING,245,SQL,71,TCP/IP,1,XHTML,9,XML,18,
ltr
item
Best Online Tutorials | Source codes | Programming Languages: VBScript6-VBScript Conditional Statements
VBScript6-VBScript Conditional Statements
Best Online Tutorials | Source codes | Programming Languages
https://www.1000sourcecodes.com/2012/10/vbscript6-vbscript-conditional.html
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/2012/10/vbscript6-vbscript-conditional.html
true
357226456970214079
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content