VBS : Tutorial 2 (Conditional Statements)
Conditional
Statements
1) If…Then…Else Statement
2) Select Case Statement
If...Then...Else
example :Basic
IF
Dim myDate
myDate = #2/13/98#
If myDate < Now Then myDate = Now
example : If
with multiple statements inside
Dim x
x= 20
If x>10 Then
msgbox "x value is:
"&x
msgbox "Bye Bye"
End If
example: If Else
Dim x
x= Inputbox
(" Enter a value")
If x>100 Then
Msgbox "X is a Big
Number"
Msgbox "X value is:
"&X
Else
Msgbox "X is a Small Number"
Msgbox "X value is: "&X
End If
example : If ...ElseIf ...Else
fee="Visa"
if fee="Cash" then
msgbox
"pay cash!"
elseif fee="Visa" then
msgbox
"pay with visa."
elseif fee="American
Express" then
msgbox
"pay with American Express."
else
msgbox
"Unknown method of Payment."
end If
Select Case
example :Basic
Dim x
x=10
Select case x
case
10
msgbox
"yes"
end select
example: Full Eg
Option explicit
Dim x,y, Operation, Result
x= Inputbox (" Enter x
value")
y= Inputbox ("Enter y
value")
Operation= Inputbox ("Enter an Operation")
Select Case Operation
Case
"add"
Result=
x+(y)
Msgbox
"Addition of x,y values is "&Result
Case
"sub"
Result=
x-y
Msgbox
"Substraction of x,y values is "&Result
Case
"mul"
Result= x*y
Msgbox
"Multiplication of x,y values is "&Result
Case
"div"
Result=
x/y
Msgbox
"Division of x,y values is "&Result
Case
"mod"
Result=
x mod y
Msgbox
"Mod of x,y values is "&Result
Case
"expo"
Result=
x^y
Msgbox"Exponentation
of x,y values is "&Result
Case
Else
msgbox
"Wrong Operation"
End Select
1 .Grade students based on total
marks.
2. Eligible to vote.
String
1. Len
2. Instr
3. mid
4. strReverse
5. Replace
6. strcomp
7. split
8. join
9. space
10. left
11. right
12. Lcase
13. Ucase
Len
msgbox len("asdsa")
Instr
msgbox
Instr(1,"asd","s")
mid
msgbox mid("asdzxc",1,3)
strReverse
msgbox
strReverse("reverse")
Replace
msgbox
Replace("reverse","e","1")
strcomp
-1
(if string1 < string2)
0
(if string1 = string2)
1
(if string1 > string2)
Null
(if string1 or string2 is Null)
msgbox
strcomp("reverse1","reverse")
Split
a=split("1 2 3"," ")
msgbox ubound(a)
join
a=split("1 2 3","
")
msgbox ubound(a)
str=join(a," ")
msgbox isarray(str)
-------------------------------------------------------------------------------------
If condition types
if condition=true then '------------1
stat1
stat2
end if
if condition then '------------2
stat1
stat2
else
stat1
stat1
end if
if condition1 then '------------3
stat1
stat2
elseif condition2 then
stat1
stat1
end if
if condition1 then '------------4
stat1
stat2
elseif condition2 then
stat1
stat1
elseif....
elseif....
else
stat1
stat2
end if
------------------------------------Select Case
Dim x
x=30
Select Case x 'no break
Case
10 'no
colon
msgbox
"x="& 10
Case
20
msgbox
"x="& 20
Case
else 'default if
nothing matches
msgbox
"not exist"
End Select
-------------------------------------------------------
'len-----------------
'instr(starting_from , "Input
String ", "To Search")----------
'mid("Input String ",
starting_from , how_many_char )---------
'strreverse----------
'msgbox
( len("piyush tiwari") - len(replace("piyush
tiwari","i","")) )
'strcomp----
'split----
'join-----
'space
'left
'right
'lcase--
'ucase--
'replace------