SlideShare a Scribd company logo
VBSCRIPT DATATYPES
VBScript (Visual Basic Scripting Edition) is a lightweight scripting language used
primarily for web development and automation tasks. It supports several data
types for handling different types of data
Variant: Default type, can store any kind of data.
Integer: Whole numbers (from -32,768 to 32,767).
Long: Larger integers (from -2,147,483,648 to 2,147,483,647).
Single: Single-precision floating-point numbers.
Double: Double-precision floating-point numbers.
String: Text or character data.
Boolean: Logical values (True or False).
Date: Date and time values.
Nothing: Object variable with no reference.
Empty: Uninitialized variable
Variant
 Description: In VBScript, all variables are of type Variant by default. A
Variant can hold any type of data (e.g., string, integer, date).
Dim myVar
myVar = "Hello, World!" ' String
MsgBox myVar ' Outputs "Hello, World!"
myVar = 100 ' Integer
MsgBox myVar ' Outputs 100
myVar = #12/25/2024# ' Date
MsgBox myVar ' Outputs "12/25/2024"
Integer
Stores whole numbers between -32,768 and 32,767
Dim num
num = 1500
MsgBox num ' Outputs 1500
Long
Used for storing larger integers, ranging from -2,147,483,648 to 2,147,483,647.
Dim largeNum
largeNum = 1234567890
MsgBox largeNum ' Outputs 1234567890
Single
Stores single-precision floating-point numbers (decimal values).
Dim floatNum
floatNum = 3.14159
MsgBox floatNum ' Outputs 3.14159
Double
Used to store double-precision floating-point numbers for higher precision.
Dim doubleNum
doubleNum = 1234567890.987654321
MsgBox doubleNum ' Outputs 1234567890.987654
String
Stores a sequence of characters, including text or numbers as text.
Dim str
str = "Hello, VBScript!"
MsgBox str ' Outputs "Hello, VBScript!"
Boolean
Stores logical values: True or False.
Dim isTrue
isTrue = True
MsgBox isTrue ' Outputs -1 (VBScript treats True as -1)
Dim isFalse
isFalse = False
MsgBox isFalse ' Outputs 0
Date
Used to store date and time values.
Dim currentDate
currentDate = Now ' Get current date and time
MsgBox currentDate ' Outputs current date and time
Dim specificDate
specificDate = #12/25/2024#
MsgBox specificDate ' Outputs "12/25/2024"
Nothing
Represents a variable that has no object reference or value. It is not a data
type per se, but used for object variables.
Dim obj
Set obj = Nothing ' Releases object reference
MsgBox IsObject(obj) ' Outputs False
Empty
Represents an uninitialized variable. It is used to check if a variable has
been assigned a value or not.
Dim emptyVar
MsgBox IsEmpty(emptyVar) ' Outputs True
NULL: Represents the absence of a value (usually used with database or
object-related operations).
Dim myNullVar
myNullVar = Null
MsgBox IsNull(myNullVar) ' Outputs True
VBScript Operators:
Dim a, b, result
a = 10
b = 3
result = a + b
MsgBox result
result = a - b
MsgBox result
result = a * b
MsgBox result
result = a / b
MsgBox result
result = a  b
MsgBox result
result = a Mod b
MsgBox result
Comparison Operators
Dim a, b
a = 10
b = 5
If a = b Then
MsgBox "Equal"
Else
MsgBox "Not Equal"
End If
If a > b Then
MsgBox "a is greater than b"
End If
Logical Operators
Dim a, b
a = True
b = False
If a And b Then
MsgBox "Both are True"
Else
MsgBox "At least one is False"
End If
If Not a Then
MsgBox "a is False"
Else
MsgBox "a is True"
End If
Concatenation
Dim firstName, lastName, fullName
firstName = "John"
lastName = "Doe"
fullName = firstName & " " & lastName
MsgBox fullName
Type Conversion Operators
 These operators are used to convert one data type to another.
Operator Description Example
CInt Converts to Integer CInt(expression)
CDbl Converts to Double CDbl(expression)
CStr Converts to String CStr(expression)
CDate Converts to Date CDate(expression)
CLng Converts to Long CLng(expression)
Dim num, strNum
num = 3.14
strNum = CStr(num) ' Converts to string
MsgBox strNum
Dim dateStr, dateVal
dateStr = "12/25/2024"
dateVal = CDate(dateStr) ' Converts to date
MsgBox dateVal
Assignment Operators
 These operators are used to assign values to variables.
Operator Description Example
= Assignment a = b
+= Add and assign a += b (a = a + b)
-= Subtract and assign a -= b (a = a - b)
*= Multiply and assign a *= b (a = a * b)
/= Divide and assign a /= b (a = a / b)
&= Concatenate and assign a &= b (a = a & b)
Dim a, b
a = 5
b = 3
a += b ' a = a + b, so a becomes 8
MsgBox a
Bitwise Operators
 These operators are used for bit-level operations.
Operator Description Example
And Bitwise AND a And b
Or Bitwise OR a Or b
Operator Description Example
Xor Bitwise XOR a Xor b
Not Bitwise NOT Not a
Shl Bitwise Shift Left a Shl b
Shr Bitwise Shift Right a Shr b
Dim a, b, result
a = 5 ' 0101 in binary
b = 3 ' 0011 in binary
result = a And b
MsgBox result
result = a Or b
MsgBox result
VBScript Control Statements
control statements are used to control the flow of execution in a program. These
statements allow for conditional branching, looping, and error handling.
If...Then...Else: Used for conditional branching.
For...Next: Used for iterating a specific number of times.
For Each...Next: Used to iterate through a collection or array.
Do...Loop: Used for creating loops with conditions checked before or after
execution.
Exit statements: Used to exit loops or procedures prematurely.
Select Case: Used for multiple condition checks.
Error Handling: On Error Resume Next and On Error GoTo 0 for managing runtime
errors.
1. Conditional Statements
If...Then Statement
The If...Then statement evaluates a condition and executes code if the condition is
true.
If condition Then
' Code to execute if condition is True
End If
Example:
Dim num
num = 10
If num > 5 Then
MsgBox "The number is greater than 5"
End If
If...Then...Else Statement
The If...Then...Else statement adds an Else part to the If statement, allowing
alternative code to run when the condition is false.
SYNTAX:
If condition Then
' Code to execute if condition is True
Else
' Code to execute if condition is False
End If
Example:
Dim num
num = 3
If num > 5 Then
MsgBox "The number is greater than 5"
Else
MsgBox "The number is 5 or less"
End If
c. If...Then...ElseIf...Else Statement
This allows multiple conditions to be evaluated.
SYNTAX:
If condition1 Then
' Code to execute if condition1 is True
ElseIf condition2 Then
' Code to execute if condition2 is True
Else
' Code to execute if all conditions are False
End If
Example:
Dim num
num = 7
If num > 10 Then
MsgBox "The number is greater than 10"
ElseIf num > 5 Then
MsgBox "The number is greater than 5 but less than or equal to 10"
Else
MsgBox "The number is 5 or less"
End If
2. Looping Statements
a. For...Next Loop
The For...Next loop is used for iterating a specific number of times.
SYNTAX:
For counter = start To end
' Code to execute on each iteration
Next
Example:
Dim i
For i = 1 To 5
MsgBox "Iteration number: " & i
Next
b. For Each...Next Loop
This loop is used to iterate over a collection or array.
Syntax:
For Each item In collection
' Code to execute for each item
Next
Example:
Dim colors
colors = Array("Red", "Green", "Blue")
For Each color In colors
MsgBox "Color: " & color
Next
c. Do...Loop Statement
The Do...Loop provides a flexible way to create loops. You can loop while a
condition is true or until a condition is true.
SYNTAX:
Do While...Loop (loop until the condition is false):
Do While condition
' Code to execute while condition is True
Loop
Example:
Dim num
num = 1
Do While num <= 5
MsgBox "Current number: " & num
num = num + 1
Loop
Do Until...Loop (loop until the condition is true):
SYNTAX:
Do Until condition
' Code to execute until condition is True
Loop
Example:
Dim num
num = 1
Do Until num > 5
MsgBox "Current number: " & num
num = num + 1
Loop
Do...Loop While (check condition after the loop executes):
SYNTAX:
Do
' Code to execute
Loop While condition
Example:
Dim num
num = 1
Do
MsgBox "Current number: " & num
num = num + 1
Loop While num <= 5
Do...Loop Until (check condition after the loop executes):
SYNATX:
Do
' Code to execute
Loop Until condition
Example:
Dim num
num = 1
Do
MsgBox "Current number: " & num
num = num + 1
Loop Until num > 5
3. Exit Statements
a. Exit For
This statement is used to exit a For...Next loop prematurely.
Syntax:
For counter = 1 To 10
If counter = 5 Then
Exit For
End If
' More code here
Next
Example:
Dim i
For i = 1 To 10
If i = 5 Then
Exit For ' Exit the loop when i is 5
End If
MsgBox "Iteration number: " & i
Next
b. Exit Do
This statement is used to exit a Do...Loop prematurely.
Synatx:
Do While condition
If someCondition Then
Exit Do
End If
' More code here
Loop
Example:
Dim num
num = 1
Do
If num = 3 Then
Exit Do ' Exit the loop when num is 3
End If
MsgBox "Current number: " & num
num = num + 1
Loop
c. Exit Sub
This exits a Sub procedure.
Syntax:
Sub MySub()
If someCondition Then
Exit Sub
End If
' More code here
End Sub
Example:
Sub MySub()
Dim num
num = 10
If num > 5 Then
MsgBox "Exiting Sub"
Exit Sub ' Exit the Sub
End If
MsgBox "This line will not execute"
End Sub
' Call the Sub
MySub()
d. Exit Function
This exits a Function procedure.
Syntax:
Function MyFunction()
If someCondition Then
Exit Function
End If
' More code here
End Function
Example:
Function MyFunction()
Dim result
result = 10
If result > 5 Then
MyFunction = result ' Assign the result and exit the function
Exit Function
End If
MyFunction = result ' This line won't be reached if result > 5
End Function
MsgBox MyFunction()
4. Error Handling
a. On Error Resume Next
This statement tells VBScript to continue execution with the next statement if an
error occurs.
Syntax:
On Error Resume Next
' Code that might cause an error
If Err.Number <> 0 Then
' Handle the error
End If
Example:
On Error Resume Next
Dim x
x = 1 / 0 ' This will cause an error (division by zero)
If Err.Number <> 0 Then
MsgBox "An error occurred: " & Err.Description
End If
5. Select Case Statement
The Select Case statement allows for multiple conditions to be checked. It is an
alternative to using multiple If...Then statements.
Syntax:
Select Case expression
Case value1
' Code to execute if expression equals value1
Case value2
' Code to execute if expression equals value2
Case Else
' Code to execute if no case matches
End Select
Example:
Dim num
num = 3
Select Case num
Case 1
MsgBox "The number is 1"
Case 2
MsgBox "The number is 2"
Case 3
MsgBox "The number is 3"
Case Else
MsgBox "The number is not 1, 2, or 3"
End Select

More Related Content

Similar to VBS control structures for if do whilw.docx (20)

PPT
VB Script Overview
Praveen Gorantla
 
DOCX
Vb script tutorial
Abhishek Kesharwani
 
PPTX
Vbscript
Abhishek Kesharwani
 
PDF
Vb script tutorial
Abhishek Kesharwani
 
PPS
Visual Basic Review - ICA
emtrajano
 
PPT
Visual basic 6.0
Aarti P
 
PPTX
Excel 2016 VBA PPT Slide Deck - For Basic to Adavance VBA Learning
PrantikMaity6
 
PPTX
VISUAL BASIC 6 - CONTROLS AND DECLARATIONS
Suraj Kumar
 
PPT
AVB201.2 Microsoft Access VBA Module 2
Dan D'Urso
 
PDF
Visual Basics for Application
Raghu nath
 
PPT
QTP Presentation2
vucevic
 
PPSX
VBScript in Software Testing
Fayis-QA
 
PDF
Vba functions
Wongyu Choe
 
PDF
Conditional Statements & Loops
simmis5
 
PPT
QTP VB Script Trainings
Ali Imran
 
PPTX
Excel VBA.pptx
GiyaShefin
 
PPT
Ch (2)(presentation) its about visual programming
nimrastorage123
 
PPT
Vb script
mcatahir947
 
VB Script Overview
Praveen Gorantla
 
Vb script tutorial
Abhishek Kesharwani
 
Vb script tutorial
Abhishek Kesharwani
 
Visual Basic Review - ICA
emtrajano
 
Visual basic 6.0
Aarti P
 
Excel 2016 VBA PPT Slide Deck - For Basic to Adavance VBA Learning
PrantikMaity6
 
VISUAL BASIC 6 - CONTROLS AND DECLARATIONS
Suraj Kumar
 
AVB201.2 Microsoft Access VBA Module 2
Dan D'Urso
 
Visual Basics for Application
Raghu nath
 
QTP Presentation2
vucevic
 
VBScript in Software Testing
Fayis-QA
 
Vba functions
Wongyu Choe
 
Conditional Statements & Loops
simmis5
 
QTP VB Script Trainings
Ali Imran
 
Excel VBA.pptx
GiyaShefin
 
Ch (2)(presentation) its about visual programming
nimrastorage123
 
Vb script
mcatahir947
 

More from Ramakrishna Reddy Bijjam (20)

PPTX
Pyhton with Mysql to perform CRUD operations.pptx
Ramakrishna Reddy Bijjam
 
PPTX
Regular expressions,function and glob module.pptx
Ramakrishna Reddy Bijjam
 
PPTX
Natural Language processing using nltk.pptx
Ramakrishna Reddy Bijjam
 
PPTX
Parsing HTML read and write operations and OS Module.pptx
Ramakrishna Reddy Bijjam
 
PPTX
JSON, XML and Data Science introduction.pptx
Ramakrishna Reddy Bijjam
 
PPTX
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
PPTX
BINARY files CSV files JSON files with example.pptx
Ramakrishna Reddy Bijjam
 
DOCX
Builtinfunctions in vbscript and its types.docx
Ramakrishna Reddy Bijjam
 
DOCX
VBScript Functions procedures and arrays.docx
Ramakrishna Reddy Bijjam
 
PPTX
Numbers and global functions conversions .pptx
Ramakrishna Reddy Bijjam
 
DOCX
Structured Graphics in dhtml and active controls.docx
Ramakrishna Reddy Bijjam
 
DOCX
Filters and its types as wave shadow.docx
Ramakrishna Reddy Bijjam
 
PPTX
JavaScript Arrays and its types .pptx
Ramakrishna Reddy Bijjam
 
PPTX
JS Control Statements and Functions.pptx
Ramakrishna Reddy Bijjam
 
PPTX
Code conversions binary to Gray vice versa.pptx
Ramakrishna Reddy Bijjam
 
PDF
FIXED and FLOATING-POINT-REPRESENTATION.pdf
Ramakrishna Reddy Bijjam
 
PPTX
Handling Missing Data for Data Analysis.pptx
Ramakrishna Reddy Bijjam
 
PPTX
Data Frame Data structure in Python pandas.pptx
Ramakrishna Reddy Bijjam
 
PPTX
Series data structure in Python Pandas.pptx
Ramakrishna Reddy Bijjam
 
PPTX
Mongodatabase with Python for Students.pptx
Ramakrishna Reddy Bijjam
 
Pyhton with Mysql to perform CRUD operations.pptx
Ramakrishna Reddy Bijjam
 
Regular expressions,function and glob module.pptx
Ramakrishna Reddy Bijjam
 
Natural Language processing using nltk.pptx
Ramakrishna Reddy Bijjam
 
Parsing HTML read and write operations and OS Module.pptx
Ramakrishna Reddy Bijjam
 
JSON, XML and Data Science introduction.pptx
Ramakrishna Reddy Bijjam
 
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
BINARY files CSV files JSON files with example.pptx
Ramakrishna Reddy Bijjam
 
Builtinfunctions in vbscript and its types.docx
Ramakrishna Reddy Bijjam
 
VBScript Functions procedures and arrays.docx
Ramakrishna Reddy Bijjam
 
Numbers and global functions conversions .pptx
Ramakrishna Reddy Bijjam
 
Structured Graphics in dhtml and active controls.docx
Ramakrishna Reddy Bijjam
 
Filters and its types as wave shadow.docx
Ramakrishna Reddy Bijjam
 
JavaScript Arrays and its types .pptx
Ramakrishna Reddy Bijjam
 
JS Control Statements and Functions.pptx
Ramakrishna Reddy Bijjam
 
Code conversions binary to Gray vice versa.pptx
Ramakrishna Reddy Bijjam
 
FIXED and FLOATING-POINT-REPRESENTATION.pdf
Ramakrishna Reddy Bijjam
 
Handling Missing Data for Data Analysis.pptx
Ramakrishna Reddy Bijjam
 
Data Frame Data structure in Python pandas.pptx
Ramakrishna Reddy Bijjam
 
Series data structure in Python Pandas.pptx
Ramakrishna Reddy Bijjam
 
Mongodatabase with Python for Students.pptx
Ramakrishna Reddy Bijjam
 
Ad

Recently uploaded (20)

PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PDF
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PPTX
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
community health nursing question paper 2.pdf
Prince kumar
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
Ad

VBS control structures for if do whilw.docx

  • 1. VBSCRIPT DATATYPES VBScript (Visual Basic Scripting Edition) is a lightweight scripting language used primarily for web development and automation tasks. It supports several data types for handling different types of data Variant: Default type, can store any kind of data. Integer: Whole numbers (from -32,768 to 32,767). Long: Larger integers (from -2,147,483,648 to 2,147,483,647). Single: Single-precision floating-point numbers. Double: Double-precision floating-point numbers. String: Text or character data. Boolean: Logical values (True or False). Date: Date and time values. Nothing: Object variable with no reference. Empty: Uninitialized variable Variant  Description: In VBScript, all variables are of type Variant by default. A Variant can hold any type of data (e.g., string, integer, date). Dim myVar myVar = "Hello, World!" ' String MsgBox myVar ' Outputs "Hello, World!" myVar = 100 ' Integer MsgBox myVar ' Outputs 100
  • 2. myVar = #12/25/2024# ' Date MsgBox myVar ' Outputs "12/25/2024" Integer Stores whole numbers between -32,768 and 32,767 Dim num num = 1500 MsgBox num ' Outputs 1500 Long Used for storing larger integers, ranging from -2,147,483,648 to 2,147,483,647. Dim largeNum largeNum = 1234567890 MsgBox largeNum ' Outputs 1234567890 Single Stores single-precision floating-point numbers (decimal values). Dim floatNum floatNum = 3.14159
  • 3. MsgBox floatNum ' Outputs 3.14159 Double Used to store double-precision floating-point numbers for higher precision. Dim doubleNum doubleNum = 1234567890.987654321 MsgBox doubleNum ' Outputs 1234567890.987654 String Stores a sequence of characters, including text or numbers as text. Dim str str = "Hello, VBScript!" MsgBox str ' Outputs "Hello, VBScript!" Boolean Stores logical values: True or False. Dim isTrue isTrue = True MsgBox isTrue ' Outputs -1 (VBScript treats True as -1) Dim isFalse isFalse = False MsgBox isFalse ' Outputs 0
  • 4. Date Used to store date and time values. Dim currentDate currentDate = Now ' Get current date and time MsgBox currentDate ' Outputs current date and time Dim specificDate specificDate = #12/25/2024# MsgBox specificDate ' Outputs "12/25/2024" Nothing Represents a variable that has no object reference or value. It is not a data type per se, but used for object variables. Dim obj Set obj = Nothing ' Releases object reference MsgBox IsObject(obj) ' Outputs False Empty Represents an uninitialized variable. It is used to check if a variable has been assigned a value or not. Dim emptyVar MsgBox IsEmpty(emptyVar) ' Outputs True NULL: Represents the absence of a value (usually used with database or object-related operations).
  • 5. Dim myNullVar myNullVar = Null MsgBox IsNull(myNullVar) ' Outputs True VBScript Operators: Dim a, b, result a = 10 b = 3 result = a + b MsgBox result result = a - b MsgBox result result = a * b MsgBox result result = a / b MsgBox result
  • 6. result = a b MsgBox result result = a Mod b MsgBox result Comparison Operators Dim a, b a = 10 b = 5 If a = b Then MsgBox "Equal" Else MsgBox "Not Equal" End If If a > b Then MsgBox "a is greater than b" End If Logical Operators
  • 7. Dim a, b a = True b = False If a And b Then MsgBox "Both are True" Else MsgBox "At least one is False" End If If Not a Then MsgBox "a is False" Else MsgBox "a is True" End If Concatenation Dim firstName, lastName, fullName firstName = "John" lastName = "Doe" fullName = firstName & " " & lastName
  • 8. MsgBox fullName Type Conversion Operators  These operators are used to convert one data type to another. Operator Description Example CInt Converts to Integer CInt(expression) CDbl Converts to Double CDbl(expression) CStr Converts to String CStr(expression) CDate Converts to Date CDate(expression) CLng Converts to Long CLng(expression) Dim num, strNum num = 3.14 strNum = CStr(num) ' Converts to string MsgBox strNum Dim dateStr, dateVal dateStr = "12/25/2024" dateVal = CDate(dateStr) ' Converts to date MsgBox dateVal Assignment Operators
  • 9.  These operators are used to assign values to variables. Operator Description Example = Assignment a = b += Add and assign a += b (a = a + b) -= Subtract and assign a -= b (a = a - b) *= Multiply and assign a *= b (a = a * b) /= Divide and assign a /= b (a = a / b) &= Concatenate and assign a &= b (a = a & b) Dim a, b a = 5 b = 3 a += b ' a = a + b, so a becomes 8 MsgBox a Bitwise Operators  These operators are used for bit-level operations. Operator Description Example And Bitwise AND a And b Or Bitwise OR a Or b
  • 10. Operator Description Example Xor Bitwise XOR a Xor b Not Bitwise NOT Not a Shl Bitwise Shift Left a Shl b Shr Bitwise Shift Right a Shr b Dim a, b, result a = 5 ' 0101 in binary b = 3 ' 0011 in binary result = a And b MsgBox result result = a Or b MsgBox result VBScript Control Statements
  • 11. control statements are used to control the flow of execution in a program. These statements allow for conditional branching, looping, and error handling. If...Then...Else: Used for conditional branching. For...Next: Used for iterating a specific number of times. For Each...Next: Used to iterate through a collection or array. Do...Loop: Used for creating loops with conditions checked before or after execution. Exit statements: Used to exit loops or procedures prematurely. Select Case: Used for multiple condition checks. Error Handling: On Error Resume Next and On Error GoTo 0 for managing runtime errors. 1. Conditional Statements If...Then Statement The If...Then statement evaluates a condition and executes code if the condition is true. If condition Then ' Code to execute if condition is True End If Example: Dim num num = 10
  • 12. If num > 5 Then MsgBox "The number is greater than 5" End If If...Then...Else Statement The If...Then...Else statement adds an Else part to the If statement, allowing alternative code to run when the condition is false. SYNTAX: If condition Then ' Code to execute if condition is True Else ' Code to execute if condition is False End If Example: Dim num num = 3 If num > 5 Then MsgBox "The number is greater than 5" Else MsgBox "The number is 5 or less" End If c. If...Then...ElseIf...Else Statement
  • 13. This allows multiple conditions to be evaluated. SYNTAX: If condition1 Then ' Code to execute if condition1 is True ElseIf condition2 Then ' Code to execute if condition2 is True Else ' Code to execute if all conditions are False End If Example: Dim num num = 7 If num > 10 Then MsgBox "The number is greater than 10" ElseIf num > 5 Then MsgBox "The number is greater than 5 but less than or equal to 10" Else MsgBox "The number is 5 or less" End If 2. Looping Statements
  • 14. a. For...Next Loop The For...Next loop is used for iterating a specific number of times. SYNTAX: For counter = start To end ' Code to execute on each iteration Next Example: Dim i For i = 1 To 5 MsgBox "Iteration number: " & i Next b. For Each...Next Loop This loop is used to iterate over a collection or array. Syntax: For Each item In collection ' Code to execute for each item Next Example:
  • 15. Dim colors colors = Array("Red", "Green", "Blue") For Each color In colors MsgBox "Color: " & color Next c. Do...Loop Statement The Do...Loop provides a flexible way to create loops. You can loop while a condition is true or until a condition is true. SYNTAX: Do While...Loop (loop until the condition is false): Do While condition ' Code to execute while condition is True Loop Example: Dim num num = 1 Do While num <= 5 MsgBox "Current number: " & num num = num + 1
  • 16. Loop Do Until...Loop (loop until the condition is true): SYNTAX: Do Until condition ' Code to execute until condition is True Loop Example: Dim num num = 1 Do Until num > 5 MsgBox "Current number: " & num num = num + 1 Loop Do...Loop While (check condition after the loop executes): SYNTAX: Do ' Code to execute Loop While condition
  • 17. Example: Dim num num = 1 Do MsgBox "Current number: " & num num = num + 1 Loop While num <= 5 Do...Loop Until (check condition after the loop executes): SYNATX: Do ' Code to execute Loop Until condition Example: Dim num num = 1 Do MsgBox "Current number: " & num num = num + 1 Loop Until num > 5
  • 18. 3. Exit Statements a. Exit For This statement is used to exit a For...Next loop prematurely. Syntax: For counter = 1 To 10 If counter = 5 Then Exit For End If ' More code here Next Example: Dim i For i = 1 To 10 If i = 5 Then Exit For ' Exit the loop when i is 5 End If MsgBox "Iteration number: " & i Next b. Exit Do
  • 19. This statement is used to exit a Do...Loop prematurely. Synatx: Do While condition If someCondition Then Exit Do End If ' More code here Loop Example: Dim num num = 1 Do If num = 3 Then Exit Do ' Exit the loop when num is 3 End If MsgBox "Current number: " & num num = num + 1 Loop c. Exit Sub This exits a Sub procedure.
  • 20. Syntax: Sub MySub() If someCondition Then Exit Sub End If ' More code here End Sub Example: Sub MySub() Dim num num = 10 If num > 5 Then MsgBox "Exiting Sub" Exit Sub ' Exit the Sub End If MsgBox "This line will not execute" End Sub ' Call the Sub MySub() d. Exit Function
  • 21. This exits a Function procedure. Syntax: Function MyFunction() If someCondition Then Exit Function End If ' More code here End Function Example: Function MyFunction() Dim result result = 10 If result > 5 Then MyFunction = result ' Assign the result and exit the function Exit Function End If MyFunction = result ' This line won't be reached if result > 5 End Function MsgBox MyFunction()
  • 22. 4. Error Handling a. On Error Resume Next This statement tells VBScript to continue execution with the next statement if an error occurs. Syntax: On Error Resume Next ' Code that might cause an error If Err.Number <> 0 Then ' Handle the error End If Example: On Error Resume Next Dim x x = 1 / 0 ' This will cause an error (division by zero) If Err.Number <> 0 Then MsgBox "An error occurred: " & Err.Description End If 5. Select Case Statement The Select Case statement allows for multiple conditions to be checked. It is an alternative to using multiple If...Then statements.
  • 23. Syntax: Select Case expression Case value1 ' Code to execute if expression equals value1 Case value2 ' Code to execute if expression equals value2 Case Else ' Code to execute if no case matches End Select Example: Dim num num = 3 Select Case num Case 1 MsgBox "The number is 1" Case 2 MsgBox "The number is 2" Case 3 MsgBox "The number is 3" Case Else
  • 24. MsgBox "The number is not 1, 2, or 3" End Select