VBScript Conditional Statements and VBScript Loop – An Excellent Introduction Guide for VBScript Tutorial 2

vbscript-conditional-statements

VBScript Tutorial – Table of Content

VBScript Tutorial #1: Overview of VBScript Variables 

VBScript Tutorial #2: VBScript Conditional Statements and Loops

VBScript Tutorial #3: VBScript Procedures

VBScript Tutorial #4: VBScript Error Handling and Execute VBScript

VBScript Tutorial #5: VBScript String Functions

VBScript Tutorial #6: VBScript Date Functions

VBScript Tutorial #7: VBScript Time Functions

VBScript Tutorial #8: VBScript Array Functions

In this VBScript Tutorial, we are going to learn about different kinds of VBScript Conditional Statements (vbscript if else statement & vbscript  select case statement) and VBScript Loop Statements (for loop, do while loop & while loop).

VBScript Tutorial #1: VBScript Conditional Statements and VBScript Loop

VBScript Conditional Statements:

Conditions are nothing but some criteria or comparison, based on which we can perform a certain task. VBScript conditional statements are the most important features in VBScript as a programming language. VBScript conditional statements perform different computations or actions based on certain ones or comparisons or conditions. Through the VBScript conditional statements, we can develop functional logic.

In this section, we are going to learn about different VBScript conditional statements, which are frequently used during programming. The frequently used conditional statements are – 

· VBScript If Statement

· VBScript Case Statement

VBScript If Statement:

The VBScript If the statement is used to validate one or more conditions through the program. Multiple conditions can be added using logical boolean operators such as AND, OR, NOT, etc. Here, the conditions are the expressions that compare one value or variable with another with the help of comparison operators like equal(=), not equal(!=), Not (!), etc. Based on the verification result, we can perform a specific task.

The conditions should be kept between “If” and “Then” keywords. If there is any task need to be performed based on the false condition, the task has to be performed after the “Else” statement. At the end of the if statement block, we need to close the VBScript if statement by using the keyword “End If.” Structure of VBScript If Statement – 

If <Condition1> AND <Condition2> AND .. <Condition3> Then

  • actions for success case

Else

  • actions for failure case

End If

VBScript ElseIf Statement:

Through the VBScript ElseIf Statement, we can add multiple VBScript If statements based on the result of the previous conditional result. Nested VBScript ElseIf statements are used while different actions or tasks need to be performed based on each of the conditions. Structure of VBScript If Statement –

If <Condition1> Then

  • actions for condition 1

ElseIF<Condition2> Then

  • actions for condition 2

ElseIF<Condition3> Then

  • actions for condition 3

Else

  • actions for else condition

End If

Example: Identify Saturday, Sunday, and Business Working Days using VBScript IF statement and VBScript ElseIf statement. Here, we will use more than one ElseIf statements to fulfill our requirements. Also, we can put multiple conditions along with the If statement with the help of ‘AND’ or ‘OR’ operators.

VBScript If Statement
VBScript If Statement

VBScript Select Case Statement:

A VBScript Select Case Statement is an alternative approach to using multiple VBScript IfElse statements. VBScript Select Case statements are used while we have different logics/statements based on the different values against any variable. It’s also known as a switch-case statement. It helps us to write code more efficiently and readable.

A VBScript Select Case statement works with a single test expression that is executed once, at the beginning. The result of the expression will be compared in each case statement. For the match, the block of statements associated with that case will be executed. VBScript Select Case block always should end with the keyword “End Select.” Structure of VBScript Select Case Statement –

Select <Expression> // This Expression can have any value between 1-3

Case 1

  • actions for expression value 1

Case 2

  • actions for expression value 2

Case 3

  • actions for expression value 3

Case Else

  • actions else condition

End Select

For example: Identify Saturday, Sunday, and Business Working Days using if conditions.

VB Scripting in UFT - Select - Case Conditional Statements in UFT
VB Scripting in UFT – Select – Case Conditional Statements in UFT

VBScript Loop:

When similar kinds of statements need to be executed repeatedly, it’s advisable to write looping statements to make the code more readable and efficient. The VBScript loop works repeatedly based on conditions or the iteration counter. Every VBScript loop has three parts –

·        Loop Iterations – It’s basically the loop counter based on these statements that are getting executed.

·        Loop Condition – Based on this loop will be executed, and once the condition meets, loop iteration will be completed.

·        Loop Statements – it’s basically the repeated activities that are executed based on the condition.

Below VBScript Looping statements are frequently used during the coding – 

  • VBScript For Loop
  • VBScript While Loop
  • VBScript Do While Loop

VBScript For Loop:

VBScript For Loop statements is used to execute repeated statements based on the predefined iteration counter. In this structure, the loop will continue until the iteration is reached to the predefined counter value as a condition. The VBScript For Loop should always start with the “For” keyword and end with the keyword “Next.”

While defining the counter in the after “For” keyword, we can specify the increment or decrement of the counter using the keyword “Step.” By default, if we do not use this keyword, VBScript For loop defined the increment by 1. Also, we can use the VBScript Exit For the statement to exit from the loop, which can be placed within any VBScript conditional statements within this looping structure. Structure for “VBScript For Loop” – 

For nIteration = <start> to <end> Step <incremet/decrement by>

— Repeatative Statement 1

— Repeatative Statement 2

If  <Condition> Then

            Exit For

End If

Next

Example – here loop will be executed until the counter value has been reached to 10,

For nIteration = 1 to 10 Step 1

Msgbox “Current Iteration – “ & nIteration

Next

VBScript While Loop:

VBScript While Loop statements are used to execute repeated statements based on one or more conditions. In this structure, conditions are checked at the beginning of the loop. So, if the conditions are not met, the loop will not be executed. The keyword “While” is used to check the condition. We can use the “Exit While” statement to exit from VBScript while loop, which can be used in an IF statement within this looping structure. Structure for “VBScript While Loop” – 

While <Loop Condition>

— Repeatative Statement 1

— Repeatative Statement 2

If  <Exit Condition> Then

            Exit While

End If

Wend

Example – here loop will be executed until the counter value has been reached to 10,

VB Scripting in UFT -Looping Statements in UFT (While-Wend)
VBScript Loop – VBScript While Loop Statement

VBScript Do While Loop:

VBScript Do While Loop statements are used to execute repeated statements based on one or more conditions. This loop will be ontinued untill the loop conditions return False. VBScript Do While Loop structure, conditions are checked at the end of the loop. So, irrespective of the conditions are meet or not; the loop statements are always executed for the first iteration. We can use the “Exit Do” statement to exit from this loop, which can be placed in any conditional statement within this looping structure.

The keyword “While” is used to check the condition. The main difference between VBScript Do While Loop and VBScript While Loop statement is the position of the conditional statement. Structure for VBScript “Do While Loop” –

Do

— Repeatative Statement 1

— Repeatative Statement 2

If  <Exit Condition> Then

            Exit Do

End If

Loop While <Loop Condition>

Example – here loop will be executed until the counter value has been reached to 10,

VB Scripting in UFT - Looping Statements in UFT (Do-Loop)
VBScript Loop – VBScript Do While Loop

Conclusion:

In this VBScript article, we have learned about the VBScript Conditional Statements (vbscript if else statement & vbscript  select case statement) and VBScript Loop Statements (for loop, do while loop & while loop).. We hope this tutorial has helped a lot to brush up on your basics of VB Scripting. If you want to learn more about VBScript, please click here.

About K Mondal

Hi, I am K. Mondal, I am associated with a leading organization. I am having 12+ years of working experience across domains e.g., application development, automation testing, IT Consultant. I am very much interested in learning different technologies. I am here to fulfill my aspiration and currently contributing as an Author and Website Developer both in LambdaGeeks.
Connect to LinkedIn- https://www.linkedin.com/in/kumaresh-mondal/

Leave a Comment

Your email address will not be published. Required fields are marked *