This document provides an overview of VBScript concepts including: - Variables, arrays, functions, conditional statements, and looping statements are introduced. - The agenda covers variable declaration, array usage, function parameters, if/else conditional syntax, and for/while loop structures. - Examples are given for common VBScript elements like variables, functions, if/else statements, and for loops to illustrate proper syntax and usage.
Page 2Classification: Restricted Agenda •Overview of VB Script • Variables • Rules for Declaring Variables • Option Explicit • Comments • Arrays • Functions • Calling a Function • Function Parameters and return • Sub Procedures • Types of Conditional Statements • Types of Looping Statements • Loop Control Statements
3.
Page 3Classification: Restricted •VBScript (Visual Basic Script) is a general-purpose, lightweight and active scripting language developed by Microsoft that is modelled on Visual Basic. • As it a scripting language, it is not actually compiled but INTERPRETED! • VBScript, for the most part, is case insensitive. It has a very simple syntax, easy to learn and to implement. • It uses Component Object Model (COM) in order to access the elements of the environment in which it is executing. Overview of VB Script
4.
Page 4Classification: Restricted •UFT is using VBScript as a scripting language • VBScript is used for Client side scripting in Microsoft Internet Explorer. Overview of VB Script
5.
Page 5Classification: Restricted •Open a text editor (Notepad) • Enter the code: msgbox “Hello World” • The msgbox function displays a message box and waits for the user to click a button and then an action is performed based on the button clicked by the user. • Save it as My Script.vbs • Double click on My Script.vbs to execute the code – A popup message with the text “Hello World” appears.
6.
Page 6Classification: Restricted •Variable is a named memory location used to hold a value. • VBScript has only ONE fundamental data type - Variant. • Because of this you can store different types of data in VB easily unlike other languages where you need to declare the variable type like Integer, Decimal etc. • Variables are declared using “dim” keyword • dim [variable_name] • Ex: dim a
7.
Page 7Classification: Restricted Variables •Variable Name must begin with an alphabet. • Variable names cannot exceed 255 characters • Variables Should NOT contain a period(.) • Variable Names should be unique in the declared context. • Eg. Dim Employee • Dim Unit99 • Dim Student_Age • Dim 99Unit • Dim student.age
8.
Page 8Classification: Restricted •The numeric values should be declared without double quotes. a=10 • The String values should be enclosed within double quotes(") a=“My Script” • Date and Time variables should be enclosed within hash symbol(#) a=#27/6/2016# a=#04:30:44 PM# Rules for Declaring Variables
9.
Page 9Classification: Restricted •Since VB is a loosely typed language just like other scripting languages • Loosely typed – Using a variable just by assigning a value alone . Declaration is not mandatory. • Note : It is always a good practice to declare variables & use it. This makes your code more READABLE Rules for Assigning Values to Variables
10.
Page 10Classification: Restricted •Option Explicit – is enforces “Variable Declaration”. • If the first line of your code has “Option Explicit”, then all variables that are to be used in the code should be explicitly declared. • After that you can assign a value to them. Option Explicit dim a a=10 • Invalid: Option Explicit a=10 Option Explicit
11.
Page 11Classification: Restricted •Comments are used to mark a piece of code so that it doesn't get executed. • The apostrophe is the special character VBScript uses as its comment initiator. • VB Script supports only ‘single line comments’ & does not support “multi line comments” • Example: Option Explicit dim myText ‘Variable declaration myText=“Good Evening” ‘Assigning value Msgbox myText ‘printing out the message to user Comments
12.
Page 12Classification: Restricted •Arrays can hold more than one value in a single variable at a time. • Unlike most conventional languages, VBScript Arrays can store any type of variable in an array. Hence, an array can store an integer, string or characters in a single array variable. Arrays
13.
Page 13Classification: Restricted •Method 1 : Using Dim dim arr1() 'Without Size • Method 2 : Mentioning the Size dim arr2(4) 'Declared with size of 4 • Array index will be starting from 0 (ZERO) • Array Index Cannot be Negative. • Method 3 : using 'Array' Parameter dim arr3 arr3 = Array("apple","Orange","Grapes") • The values are assigned to the array by specifying array index value against each one of the values to be assigned. It can be a string. dim arr(3) arr(0) = 1 'Number arr(1) = "VBScript“ 'String Array Declaration
14.
Page 14Classification: Restricted Functions •A function is a group of reusable code which can be called anywhere in your program. • This will enable programmers to divide a big program into a number of small and manageable functions.. • Apart from inbuilt Functions, VBScript allows us to write user- defined functions as well • Basic Syntax: Function Functionname(--parameter-list--) statement 1 statement 2 ……… statement n End Function
15.
Page 15Classification: Restricted Callinga Function • To invoke a function somewhere later in the script, you would simple need to write the name of that function with the Call keyword. • Example: Function Greeting() Msgbox “Hi there!” End Function Call Greeting()
16.
Page 16Classification: Restricted FunctionParameters and return • Parameters can pass to the functions, which will be an input entry to the function • Function also can return some value back to the calling function, this will be output from the function • Example: Dim a a=“Mark” msgbox Greeting(a) ‘Variable a is the input parameter here Function Greeting(name) Greeting = “Hi ”&name End Function
17.
Page 17Classification: Restricted SubProcedures • Sub Procedures are similar to functions but there are few differences. • Sub procedures DONOT Return a value while functions may or may not return a value. • Sub procedures are called without call keyword • Sub procedures are always enclosed within Sub and End Sub statements. • Basic Syntax: Sub Procedurename(--parameter-list--) statement 1 statement 2 statement n End Sub
18.
Page 18Classification: Restricted Callinga Sub Procedure • To invoke a Sub Procedure somewhere later in the script, you would simple need to write the name of that Sub Procedure without Call keyword. • Sub procedure can also have parameters like function • Example: Sub Greeting() Msgbox “Hi there!” End Sub Greeting() ‘calling a sub procedure
19.
Page 19Classification: Restricted Typesof Conditional Statements • if statement - consists of a Boolean expression followed by one or more statements. Syntax: if (condition=true) Then [conditional Statements] End If • if..else statement consists of a Boolean expression followed by one or more statements. If the condition is True, the statements under If statements are executed. If the condition is false, Else part of the script is Executed
20.
Page 20Classification: Restricted Typesof Conditional Statements Syntax: if (condition=true) Then [conditional Statements_1] Else [conditional Statements_2] End If • if...elseif..else statement - An if statement followed by one or more ElseIf Statements, that consists of boolean expressions and then followed by an optional else statement, which executes when all the condition becomes false. Syntax: if (condition=true) Then [conditional Statements_1] Elseif (Cndition = true) then [conditional Statements_2] Else [default Conditional Statement] End If
21.
Page 21Classification: Restricted Typesof Conditional Statements • nested if statements - An if or elseif statement inside another if or elseif statement(s). • switch statement - A switch statement allows a variable to be tested for equality against a list of values. Syntax: Select Case expression Case expressionlist1 [Statements] Case expressionlist2 [Statements] Case Else [Default Statements] End Select
22.
Page 22Classification: Restricted Typesof Looping Statements • for loop - Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable. Syntax: for counter=start to end [step count] [Statements] Next
23.
Page 23Classification: Restricted Typesof Looping Statements • for ..each loop - This is executed if there is at least one element in group and reiterated for each element in a group. Syntax: for each element in group [Statements] Next • while..wend – loop - This tests the condition before executing the loop body. Syntax: while (condition is true) [Statements] wend
24.
Page 24Classification: Restricted Typesof Looping Statements • do..while loops - The do..While statements will be executed as long as condition is True.(i.e.,) The Loop should be repeated till the condition is False. Syntax: Do while (condition is true) [Statements] Loop
25.
Page 25Classification: Restricted Typesof Looping Statements • do..until loops - The do..Until statements will be executed as long as condition is False.(i.e.,) The Loop should be repeated till the condition is True. Do Until (condition is false) [Statements] Loop
26.
Page 26Classification: Restricted LoopControl Statements • Loop control statements change execution from its normal sequence. When execution leaves a scope, all the remaining statements in the loop are NOT executed. • Exit For statement - Terminates the For loop statement and transfers execution to the statement immediately following the loop • Exit Do statement - Terminates the Do While statement and transfers execution to the statement immediately following the loop