SlideShare a Scribd company logo
Control Flow Functions In VbScript 
Conditional statements allows us to make decisions and to control the flow of execution of a 
script or one of its sections and accordingly repeat actions. 
Usage of VBScript Conditional statements in QTP: 
I) We use conditional statements to insert verification points. 
II) We can user conditional checks for error handling purpose also. 
VBScript mainly provides following 2 types of decision making statements : 
I) If ...Then Statement. 
II) Select Case Statement. 
 Simple If ( Single line without block) statement is Condition is True: 
Dim myDate 
myDate=#10/10/2014# 
if myDate < date then myDate =date+2 ' where current date is 10/13/2014 
msgbox myDate 
'O/P - 10/15/2014 
 Execute block of statements using Simple If 
Dim myDate 
myDate=#10/10/2014# 
if myDate < date then 
myDate =date+2 ' where current date is 10/13/2014 
msgbox myDate 
msgbox "Mindfire solutions" 
End If 
'O/P - 10/15/2014 
Mindfire solutions 
 IF ...Then...Else 
Dim a,b 
a=5 
b=20
If a > b Then 
Msgbox "a is Greater" 
Else 
msgbox "b is Greater" 
End If 
 If...Then...ElseIf..Else Statement 
Dim a 
a = -5 
If a > 0 Then 
Msgbox "a is a POSITIVE Number" 
ElseIf a < 0 Then 
Msgbox "a is a NEGATIVE Number" 
Else 
Msgbox "a is EQUAL than ZERO" 
End If 
 nested if statements 
Dim a 
a = 23 
If a > 0 Then 
Msgbox "The Number is a POSITIVE Number" 
If a = 1 Then 
Msgbox "The Number is Neither Prime NOR Composite" 
Elseif a = 2 Then 
Msgbox "The Number is the Only Even Prime Number" 
Elseif a = 3 Then 
Msgbox "The Number is the Least Odd Prime Number" 
Else 
Msgbox "The Number is NOT 0,1,2 or 3" 
End If 
ElseIf a < 0 Then 
Msgbox "The Number is a NEGATIVE Number" 
Else 
Msgbox "The Number is ZERO" 
End If 
'O/P - The Number is a POSITIVE Number 
The Number is NOT 0,1,2 or 3 
 Select Case statement 
Executes one of several groups of statements, depending on the value of an expression.
Dim num1, num2, operation 
num1=5 
num2=10 
operation= Lcase(InputBox ("Enter an operation")) 
Select Case operation 
Case "add" 
msgbox "res:" &(num1+num2) 
Case "sub" 
msgbox "res:" &(num1-num2) 
Case "div" 
msgbox "res:" &(num2/num1) 
Case Else 
msgbox "res:" &(num1*num2) 
End Select 
Using Control Structures to Make Code Repeat 
For…Next 
The first structure is often referred to as the For…Next loop. The syntax for this structure is 
For counter = start to finish [Step step] 
...code that gets repeated 
Next 
Example: 
' increment loop by 2 
Counter = 1 
Result = 0 
For Counter = 1 to 5 step 2 
Result=Result + Counter 
msgbox Result 
Next
'Example with Exit For 
For Counter=1 to 5 
If Counter=3 Then 
Exit for 
End If 
msgbox Counter 
Next 
Do…Loop 
The first loop structure you can use is the Do While… 
Loop structure. The basic syntax for this structure is 
Do While <Condition> 
….Code with in the loop for repetitive activity 
Loop 
Example: 
Counter = 1 
Do While Counter < 4 
Total =Inputbox("Please enter the total marks in numbers") 
If Total < 30 Then 
MsgBox "Fail" 
ElseIf Total >=30 and Total <=100 then 
Msgbox "Pass" 
Else 
Msgbox "Invalid Marks" 
End If
Counter = Counter + 1 
Loop 
Exit Do While loop: 
i = 0 
Do While i <= 100 
If i > 10 Then 
Exit Do ' Loop Exits if i>10 
End If 
msgbox("The Value of i is : " &i) 
i = i + 2 
Loop 
There is an another piece of DO ..Loop. Below is the syntax: 
Do 
….Code with in the loop for repetitive activity 
Loop Until condition 
Example: 
Counter = 1 
Do 
Total =Inputbox("Please enter the total marks in numbers") 
If Total < 30 Then 
MsgBox "Fail" 
ElseIf Total >=30 and Total <=100 then 
Msgbox "Pass" 
Else 
Msgbox "Invalid Marks" 
End If 
Counter = Counter + 1
Loop until Counter > 4 
Exit Do Until loop: 
i = 0 
Do 
i = i + 2 
If i > 10 Then 
Exit Do ' Loop Exits if i>10 
End If 
msgbox("The Value of i is : " &i) 
Loop Until i > 100 
For Each...Next statement - runs code for each item in a collection or each element of an array 
Example: 
Dim birds(2) 
birds(0)="parrot" 
birds(1)="Crow" 
birds(2)="Sparrow" 
For Each x In birds 
msgbox(x) 
Next 
While...Wend: Executes a series of statements as long as a given condition is True. 
Example: 
Dim Counter 
Counter = 0 ' Initialize variable. 
While Counter < 20 ' Test value of Counter. 
Counter = Counter + 1 ' Increment Counter. 
msgbox Counter 
Wend ' End While loop when Counter > 19.
Conditional statements in vb script

More Related Content

PPTX
Android Layout.pptx
PPTX
Windowforms controls c#
PDF
Test Driven Development
PPTX
Cookies and sessions
PPTX
jQuery PPT
PPT
Origins and evolution of HTML and XHTML
PPT
JavaScript: Events Handling
PDF
JavaScript - Chapter 11 - Events
Android Layout.pptx
Windowforms controls c#
Test Driven Development
Cookies and sessions
jQuery PPT
Origins and evolution of HTML and XHTML
JavaScript: Events Handling
JavaScript - Chapter 11 - Events

What's hot (20)

PPTX
Understanding LINQ in C#
PPTX
Html Frames
PDF
jQuery for beginners
PDF
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
PDF
Manipulating strings
PPTX
Lab #2: Introduction to Javascript
PPT
JQuery introduction
PPTX
Functions in C.pptx
PPTX
jQuery
PPTX
ASP.NET State management
PDF
Java Thread Synchronization
PPTX
Dialog box in vb6
PPTX
Tuple in python
KEY
Server Side Programming
PPT
PDF
Let’s Learn Python An introduction to Python
PPTX
HTML Fundamentals
PPT
JavaScript Control Statements I
PPTX
Form Validation in JavaScript
PPTX
Angularjs PPT
Understanding LINQ in C#
Html Frames
jQuery for beginners
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
Manipulating strings
Lab #2: Introduction to Javascript
JQuery introduction
Functions in C.pptx
jQuery
ASP.NET State management
Java Thread Synchronization
Dialog box in vb6
Tuple in python
Server Side Programming
Let’s Learn Python An introduction to Python
HTML Fundamentals
JavaScript Control Statements I
Form Validation in JavaScript
Angularjs PPT
Ad

Viewers also liked (9)

PPT
Vb script
PDF
Loops and conditional statements
PPT
VBscript
PPTX
Conditional statements
PPT
Intorudction into VBScript
DOCX
Audit Imp Q_ Bcom III Year_General & Computers
PDF
Hand book for 6th semester B.Com Bangalore University
PDF
Vb script tutorial for qtp[1]
PPSX
INTRODUCTION TO C PROGRAMMING
Vb script
Loops and conditional statements
VBscript
Conditional statements
Intorudction into VBScript
Audit Imp Q_ Bcom III Year_General & Computers
Hand book for 6th semester B.Com Bangalore University
Vb script tutorial for qtp[1]
INTRODUCTION TO C PROGRAMMING
Ad

Similar to Conditional statements in vb script (20)

DOCX
VBS control structures for if do whilw.docx
PDF
Vbscript
DOCX
Vb script tutorial
PDF
Vb script tutorial
PDF
PPT
PPT
ملخص البرمجة المرئية - الوحدة الرابعة
PDF
4th_Ed_Ch03.pdf
PPTX
Programming Fundamentals in Python - Selection Structure
PDF
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
PPT
Java căn bản - Chapter6
PDF
Chapter 3 Computer Programmingodp-1_250331_041044.pdf
PDF
Solved Practice Problems For the C++ Exams
PDF
Pseudocode By ZAK
PDF
C101-PracticeProblems.pdf
PDF
Practiceproblems(1)
PPTX
POLITEKNIK MALAYSIA
PDF
Python for Machine Learning
PDF
Chap 5 c++
PDF
OIT 116 LOOPS AND CONDITION STATEMENTS.pdf
VBS control structures for if do whilw.docx
Vbscript
Vb script tutorial
Vb script tutorial
ملخص البرمجة المرئية - الوحدة الرابعة
4th_Ed_Ch03.pdf
Programming Fundamentals in Python - Selection Structure
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
Java căn bản - Chapter6
Chapter 3 Computer Programmingodp-1_250331_041044.pdf
Solved Practice Problems For the C++ Exams
Pseudocode By ZAK
C101-PracticeProblems.pdf
Practiceproblems(1)
POLITEKNIK MALAYSIA
Python for Machine Learning
Chap 5 c++
OIT 116 LOOPS AND CONDITION STATEMENTS.pdf

Recently uploaded (20)

PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Introduction and Scope of Bichemistry.pptx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Cell Structure & Organelles in detailed.
PDF
Basic Mud Logging Guide for educational purpose
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Onica Farming 24rsclub profitable farm business
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
PDF
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
PDF
Open folder Downloads.pdf yes yes ges yes
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PDF
English Language Teaching from Post-.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Introduction and Scope of Bichemistry.pptx
O5-L3 Freight Transport Ops (International) V1.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Cell Structure & Organelles in detailed.
Basic Mud Logging Guide for educational purpose
The Final Stretch: How to Release a Game and Not Die in the Process.
O7-L3 Supply Chain Operations - ICLT Program
Microbial diseases, their pathogenesis and prophylaxis
TR - Agricultural Crops Production NC III.pdf
Insiders guide to clinical Medicine.pdf
Onica Farming 24rsclub profitable farm business
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
Open folder Downloads.pdf yes yes ges yes
01-Introduction-to-Information-Management.pdf
Open Quiz Monsoon Mind Game Final Set.pptx
English Language Teaching from Post-.pdf

Conditional statements in vb script

  • 1. Control Flow Functions In VbScript Conditional statements allows us to make decisions and to control the flow of execution of a script or one of its sections and accordingly repeat actions. Usage of VBScript Conditional statements in QTP: I) We use conditional statements to insert verification points. II) We can user conditional checks for error handling purpose also. VBScript mainly provides following 2 types of decision making statements : I) If ...Then Statement. II) Select Case Statement.  Simple If ( Single line without block) statement is Condition is True: Dim myDate myDate=#10/10/2014# if myDate < date then myDate =date+2 ' where current date is 10/13/2014 msgbox myDate 'O/P - 10/15/2014  Execute block of statements using Simple If Dim myDate myDate=#10/10/2014# if myDate < date then myDate =date+2 ' where current date is 10/13/2014 msgbox myDate msgbox "Mindfire solutions" End If 'O/P - 10/15/2014 Mindfire solutions  IF ...Then...Else Dim a,b a=5 b=20
  • 2. If a > b Then Msgbox "a is Greater" Else msgbox "b is Greater" End If  If...Then...ElseIf..Else Statement Dim a a = -5 If a > 0 Then Msgbox "a is a POSITIVE Number" ElseIf a < 0 Then Msgbox "a is a NEGATIVE Number" Else Msgbox "a is EQUAL than ZERO" End If  nested if statements Dim a a = 23 If a > 0 Then Msgbox "The Number is a POSITIVE Number" If a = 1 Then Msgbox "The Number is Neither Prime NOR Composite" Elseif a = 2 Then Msgbox "The Number is the Only Even Prime Number" Elseif a = 3 Then Msgbox "The Number is the Least Odd Prime Number" Else Msgbox "The Number is NOT 0,1,2 or 3" End If ElseIf a < 0 Then Msgbox "The Number is a NEGATIVE Number" Else Msgbox "The Number is ZERO" End If 'O/P - The Number is a POSITIVE Number The Number is NOT 0,1,2 or 3  Select Case statement Executes one of several groups of statements, depending on the value of an expression.
  • 3. Dim num1, num2, operation num1=5 num2=10 operation= Lcase(InputBox ("Enter an operation")) Select Case operation Case "add" msgbox "res:" &(num1+num2) Case "sub" msgbox "res:" &(num1-num2) Case "div" msgbox "res:" &(num2/num1) Case Else msgbox "res:" &(num1*num2) End Select Using Control Structures to Make Code Repeat For…Next The first structure is often referred to as the For…Next loop. The syntax for this structure is For counter = start to finish [Step step] ...code that gets repeated Next Example: ' increment loop by 2 Counter = 1 Result = 0 For Counter = 1 to 5 step 2 Result=Result + Counter msgbox Result Next
  • 4. 'Example with Exit For For Counter=1 to 5 If Counter=3 Then Exit for End If msgbox Counter Next Do…Loop The first loop structure you can use is the Do While… Loop structure. The basic syntax for this structure is Do While <Condition> ….Code with in the loop for repetitive activity Loop Example: Counter = 1 Do While Counter < 4 Total =Inputbox("Please enter the total marks in numbers") If Total < 30 Then MsgBox "Fail" ElseIf Total >=30 and Total <=100 then Msgbox "Pass" Else Msgbox "Invalid Marks" End If
  • 5. Counter = Counter + 1 Loop Exit Do While loop: i = 0 Do While i <= 100 If i > 10 Then Exit Do ' Loop Exits if i>10 End If msgbox("The Value of i is : " &i) i = i + 2 Loop There is an another piece of DO ..Loop. Below is the syntax: Do ….Code with in the loop for repetitive activity Loop Until condition Example: Counter = 1 Do Total =Inputbox("Please enter the total marks in numbers") If Total < 30 Then MsgBox "Fail" ElseIf Total >=30 and Total <=100 then Msgbox "Pass" Else Msgbox "Invalid Marks" End If Counter = Counter + 1
  • 6. Loop until Counter > 4 Exit Do Until loop: i = 0 Do i = i + 2 If i > 10 Then Exit Do ' Loop Exits if i>10 End If msgbox("The Value of i is : " &i) Loop Until i > 100 For Each...Next statement - runs code for each item in a collection or each element of an array Example: Dim birds(2) birds(0)="parrot" birds(1)="Crow" birds(2)="Sparrow" For Each x In birds msgbox(x) Next While...Wend: Executes a series of statements as long as a given condition is True. Example: Dim Counter Counter = 0 ' Initialize variable. While Counter < 20 ' Test value of Counter. Counter = Counter + 1 ' Increment Counter. msgbox Counter Wend ' End While loop when Counter > 19.