Basics of VB Scripting – Designing Loop and Conditional Statements for Establishing Programming Framework.
=> For The QTP Training Tutorials Series, Click Here
Recommended IPTV Service Providers
- IPTVGREAT – Rating 4.8/5 ( 600+ Reviews )
- IPTVRESALE – Rating 5/5 ( 200+ Reviews )
- IPTVGANG – Rating 4.7/5 ( 1200+ Reviews )
- IPTVUNLOCK – Rating 5/5 ( 65 Reviews )
- IPTVFOLLOW -Rating 5/5 ( 48 Reviews )
- IPTVTOPS – Rating 5/5 ( 43 Reviews )
=> Don’t forget to read the entire free VBScripting Tutorial series here
As discussed in our last article on VB Scripting, we touched upon some basic features of the VB script. In the following article, we will delve into more programming aspects before wrapping up our introduction series.
What This Guide Covers:
Conditional Statements
#1) If Statement
If (condition)..Then
{Statement or series of statements}
Else
{Statement or series of statements}
End if
This is the typical syntax in formulating an if statement.
- If..then..else statements can be embedded to any extent.
- The else block can be left out if it is not required.
- Elseif can be applied to select from several alternatives.
if x=0 then
Msgbox “value=0”
Elseif x=1 then msgbox “value=1”
Elseif x=2 then msgbox “value=2”
Else msgbox “value not found”
End if
#2) Select Statement
This statement facilitates you to select from several choices based on a particular condition. The condition is evaluated once, resulting in the execution of the corresponding code block.
Select Case (expression)
Case “case1”
{Block 1}
Case “case 2”
{Block 2}
…..
Case Else
{Else block}
End Select
Looping Statements
Four forms of loop statements are found:
#1) Do…loop
This loop is implemented to repetitively execute a statement or a set of statements while or until a specified condition stands true.
Here is the presentation of the syntax:
Syntax 1:
Do (While | Until) condition
{Statement or several statements}
[Exit Do]
{Statement or several statements}
Loop
Syntax 2:
Do
{statement or group of statements]
[Exit Do]
{statement or group of statements]
Loop [ Until condition]
Condition: A numeric or string expression that evaluates to true or false. If the condition is null, it is counted as false.
Take note of the ‘Exit Do’ statement.
There is a tiny variation between syntax 1 and syntax 2:
In syntax 1, the statements enclosed in the do loop do not execute until the condition becomes or remains true.
In syntax 2, the statements enclosed in the loop are implemented at least once before checking the condition.
Exit Do: Apply this command to break a never-ending loop and proceed with the subsequent code.
#2) While…wend
Syntax:
While
{Statement or a set of statements}
Wend
The statements within the while section are executed as long as the condition remains true.
Although this statement is available, it is suggested to use the Do…Loop command because it provides more versatility.
#3) For…Next
Employ this command if you intend to execute a range of statements a definite number of times, with incrementing or decrementing a counter.
For counter = start To end [Step step]
[statements]
[Exit For]
[statements]
Next
- The optional “Step” clause allows you to determine the increment or decrement value for the counter.
- If not stated, the counter increments by 1 as default.
- The “Exit For” command can be used to quit the loop ahead of time.
- Multiple For statements can be nested within each other.
Example:
For i=1 to 10
……
If x=true then
……
Exit for
End if
Next
Example with a positive step:
For i = 2 To 12 Step 2
total = total + k
Next
Example with a negative step:
For i = 12 To 2 Step -2
total = total + k
Next
#4) For each…next
This command is applied to traverse through collections of objects or arrays. It carries out a series of statements for each object or element in the collection or array.
Similar to ‘For…next’, the ‘For each…next’ command can be quit prematurely employing the ‘Exit For’ command. Multiple ‘For each’ commands can be nested within each other.
Syntax:
For Each element In group
[statements]
[Exit For]
[statements]
Next [element]
- “Element” is the variable applied to iterate over the constituents in the collection or array.
- “Group” refers to the label of the collection or array.
Note: Collection objects haven’t been discussed in this series, but essentially, a collection object is a group of associated items (objects), which can either be similar or different types.
Best Practices for Composing Code in VB Script
- Each software should begin with a concise description of its functionality.
- Give comments to each variable during its announcement to explain its use.
- Make the code modular to improve readability and maintainability.
- Divide logic into functions for superior code organization and reuse.
- Adhere to a uniform naming convention for variables.
- Include comments to enhance the code’s comprehensibility.
- Indent the lines of code for a clear sequence of execution.
- Declare “Option Explicit” to evade variable spelling mistakes.
- Take necessary measures to circumvent infinite loops.
Conclusion
We went through the basics of VB Scripting in this introduction. Although it’s not a comprehensive guide, the learnings from it are sufficient to author QTP programs for beginners and intermediate users.
We purposely refrained from discussing the subject of functions because it’s complex and crucial. We will explain functions thoroughly in our ensuing articles.
The subsequent article in our QTP training series will concentrate on the Expert view and might address some checkpoints. Don’t hesitate to pose any queries.
=> Navigate Here For The QTP Training Tutorials Series
=> Go ahead and read the whole free VBScripting Tutorial series here