- What
is meant by variable and write syntax
a small program?
Definition
A variable is convenient placeholder that refers to a
computer memory location. It refers the type of data.
Syntax
To Declare the variable
Dim Variablename1, Variablename2…..VariablenameN
Example
Dim A, B, C, D
A = 5 /
Integer Value
B = “Prakash” /
String Value
C = 12.256 /
Float Value
D = now /
Date Value
MsgBox(A)
MsgBox(B)
MsgBox(C)
MsgBox(D)
Output
5
Prakash
12.256
18/11/2008 09:04:05 PM
- What
is meant by Constant and write syntax and a small program?
Definition
Constant is
similar to variable and it is used to store the value.
Syntax
Variable name = Value
Example
Dim a, b
A = “PRAKASH”
B = 10
MsgBox(a)
MsgBox(b)
Output
PRAKASH
10
- Input Statement
The Input Statement in the VB Script is InputBox
Syntax
InputBox(“Text in the Input Box”)
Example
Dim A, B, C
A = int(InputBox(“Enter the Value A”))
B = int(InputBox(“Enter the Value B”))
C = A + B
MsgBox(C)
Input
5
10
Output
15
- Output
Statement
The Output Statements in the VB Script are as follows
- MsgBox
The difference between the MsgBox and Print statement as
follows
- In Print statement there is no need to click OK to continue the execution.
- In Msgbox, user needs to click OK in MsgBox to proceed further.
Syntax
Print
Print (“Text Message”, Variable name)
MsgBox
MsgBox (“Text Message”, Variable name)
Example
Dim A, B, C
A = InputBox(“Enter the City Name”)
B = InputBox(“Enter Pincode”)
MsgBox (A & “-“ & B)
Print (A & “-“ & B)
Input
Chennai
600 017
Output
Chennai-600 017
- IF
Statement
Syntax
IF (expression) Then Statement1
Endif
If the expression returns the value True, then Statement1
will executed else it will go the next statement.
Example
Dim A, B, C
A = int(InputBox(“Enter the value for A”))
B = int(InputBox(“Enter the value for B”))
If (A>B) Then
MsgBox(“A is Greater”)
End IF
Input
10
5
Output
A is Greater
6.
IF…ELSE Statement
Syntax
IF (expression) Then Statement1
Else Statement 2
End IF
If the expression returns the value True, then Statement1
will executed else it will execute
Statement2.
Example
Dim A, B, C
A = int(InputBox(“Enter the value for A”))
B = int(InputBox(“Enter the value for B”))
If (A>B) Then
MsgBox(“A is
Greater”)
Else
MsgBox(“B is
Greater”)
End IF
Input
10
50
Output
B is Greater
7. IF…ELSEIF Statement
Syntax
IF (Expression1) Then Statement1
ElseIF (Expression2) Then
Statement2
ELSE Statement3
EndIF
EndIF
If the Expression1 returns the value True, then Statement1
will executed else it evaluate the Expression2 and if returns True, Then it
will execute Statement2 else it will execute Statement3.
Example
Dim A, B, C
A = int(InputBox(“Enter the value for A”))
B = int(InputBox(“Enter the value for B”))
If (A>B) Then
MsgBox(“A is
Greater”)
Else IF (A<B) Then
MsgBox(“B is
Greater”)
ELSE
MsgBox(“A is
Equal to B”)
End IF
End IF
Input
50
50
Output
A is Equal to B
8. IF…ELSEIF Ladder Statements
Syntax
IF (expression) Then Statement1
ElseIF (Expression2) Then
Statement2
ELSEIF(Expression3)
Then Statement3
ELSEIF (Expression4) Then Statement4
.
.
Else Statement N
EndIF
EndIF
EndIF
EndIF
If the expression returns the value True, then Statement1
will executed else it will evaluate the
next ElseIF Expression until any Expression returns true. If any Expression
returns true then its respective statement will execute else Statement2.
Example
Dim A, B, C
A = int(InputBox(“Enter any Number from 1 to 5”))
If (A=5) Then
MsgBox(“A is Five”)
ElseIF
(A=4) Then
MsgBox(“A is
Four”)
ElseIF
(A=3) Then
MsgBox(“A is
Three”)
ElseIF
(A=2) Then
MsgBox(“A is Two”)
ElseIF
(A=1) Then
MsgBox(“A is One”)
ELSE
MsgBox(“Entered Invalid Number”)
End IF
Input
7
Output
Entered Invalid Number
9. Nested If
Syntax
If
(Expression1) Then
If
(Expression2) Then
If
(Expression3) Then Statement1
Endif
EndIF
EndIf
If all the conditions are true then only statement1 will
execute.
Example
Dim A, B, C
A = int(InputBox(“Enter the value
for A”)
B = int(InputBox(“Enter the value
for B”)
If (A>B) Then
If
(A<B) Then
MsgBox(“A is
Equal to B”)
Else
MsgBox(“A is Not Equal to B”)
End
IF
End IF
Input
20
10
Output
A is Not Equal to B
10. For Loop Usage
Syntax
For Counter = Begin To End [STEP increment value]
Statement(s)
[Exit For]
Next
- Counter is numeric value
- Begin refers the initial value of counter
- End refers the final value of counter
- [STEP] – It is Optional and it refers the incremental value
- [Exit For] – It is Optional and it will break the loop.
- Next - End of the FOR Loop, when the program encounter the ending Next statement, the value specified as a step is added to counter and Next iteration of the loop takes place.
Example
Dim i
For I = 1 to 5 STEP 1
Select Case I
Case 1
MsgBox(“One”)
Case 2
MsgBox(“Two”)
Case 3
MsgBox(“Three”)
Case 4
MsgBox(“Four”)
Case 5
MsgBox(“Five”)
End Select
Next
Output
One
Two
Three
Four
Five
11. While Loop
Syntax
While (Expression)
Statement(s)
[Exit
While]
Wend
If the Condition is true then statement(s) will executed
repeatedly until the Condition is false.
Example
Dim I
I = 1
While (I <= 5)
MsgBox(I)
I = I + 1
WEND
Output
1
2
3
4
5
12. Select Case Statement
Syntax
Select Case Expression
Case
Explist
.
.
.
[Case Else
[Statement(s)]
End Select
It Evaluate
the Expression and check with Expression list which matches the result of
expression. If any Expression list matches the result of Expression then that
Expression list statement will execute else Case Else Statement(s) will
execute.
Example
A = int(InputBox(“Enter the Value Between 1 to 5”))
Select Case I
Case 1
MsgBox(“One”)
Case 2
MsgBox(“Two”)
Case 3
MsgBox(“Three”)
Case 4
MsgBox(“Four”)
Case 5
MsgBox(“Five”)
End Select
Input
5
Output
Five
13. Combination of For &
While Statement
Example
Dim I, J
For I = 1 to 4
While ( J
<= I )
MsgBox(J)
J
= J + 1
Wend
Next
Output
0
1
2
3
4
14. String
Operations
1.
String Comparison
Syntax
Strcomp(string1, string2)
Example
Dim A, B
A = InputBox(“Enter First Name”)
B = InputBox(“Enter Second Name”)
Result = strcomp(A , B)
MsgBox(Result)
Input
Prakash
Raja
Output
-1
2.
String Concatenation
Dim A, B
A = InputBox(“Enter First Name”)
B = InputBox(“Enter Second Name”)
C = A + B
MsgBox(C)
Input
Rajesh
Kumar
Output
RajeshKumar
15. To Receive
a String
Dim A, B
A = InputBox(“Enter First Name”)
B = InputBox(“Enter Second Name”)
MsgBox(A)
MsgBox(B)
Input
Rajesh
Kumar
Output
Rajesh
Kumar
16. To Break a
String
Syntax
Split
(String, delimeter)
Example
Dim A, B
A =
“CRICKET”
B = split(A
, “I”)
MsgBox(B(0))
MsgBox(B(1))
Output
CR
CKET
- To
Find a letter position in a String
Mid Function Syntax
mid(string,
start, Number of Charecters)
Program
Dim A,B,C,D,I
A = InputBox(“Enter the String”)
B = InputBox(“Enter the letter to
Find the Position”)
C = len(A)
For I = 1 to C
D = mid(A, I, 1)
If (D = B) Then MsgBox(“The
letter” & B & ”is in position” & I & “ of string” & A)
Next
Input
Prakash
K
Output
The letter Kis in position 4 of
string Prakash
- To
Replace One part of Word in required Word
Replace Function Syntax
Repalce(
String, find, replace)
Program
Dim A,B
A = “Rajesh Kumar”
B = Replace (A,”Rajesh”,”Sathish”)
MsgBox(B)
Output
Sathish Kumar
- To
Remove Charecters in leftside of a Word
Right Function Syntax
Right(string, number of Charecters)
Program
Dim A, B, C, D, E
A = InputBox(“Enter the Word”)
B = int(InputBox(“Number of Charecters to be removed”)
C = len(A)
D = C - B
E = right(A, D)
MsgBox(E)
Input
Prakash
2
Output
Akash
- Reverse
a String
StrReverse Syntax
StrReverse(string)
Program
Dim A, B
A = InputBox(“Enter the String”)
B = StrReverse(A)
MsgBox(B)
Input
PRAKASH
Output
HSAKARP
- One Dimensional Array
It is list of row with one Coloumn, It is used to store
multiple data in single variable.
Program
Dim A(2)
For I = 0 to 2
A(I) =
InputBox(“Enter Array Values”)
Next
For eachitem in A
Print item
Next
Input
Anbu
Arasu
Ariyvu
Output
Anbu
Arasu
Ariyvu
- Two
Dimensional Array
It is list of row with multiple Coloumns
Program
Dim A[2,2]
For I = 0 to 2
For J = 0
to 2
A[I,J]
= Int(InputBox(“Enter the Value of A(”,I,”,”,J,”)”)
Next
Next
For eachitem in A
Print item
Next
- Addition
of Two Dimensional Array
Program
Dim A(2,2), B(2,2), C(2,2)
For I = 0 to 2
For J = 0
to 2
A(I,J) =
Int(InputBox("Enter the Value of of Array A"))
Next
Next
For I = 0 to 2
For J = 0
to 2
B(I,J) =
Int(InputBox("Enter the Value of of Array B"))
Next
Next
For I = 0 to 2
For J = 0
to 2
C(i,j) =
A(i,j) + B(i,J)
Next
Next
For I = 0 to 2
For J = 0
to 2
MsgBox
C(i,j)
Next
Next
Input
0
1
2
3
4
5
6
7
8
9
9
8
7
6
5
4
3
2
1
0
Output
10
10
10
10
10
10
10
10
10
- Subtraction of Two
Dimensional Array
Program
Dim A(2,2), B(2,2), C(2,2)
For I = 0 to 2
For J = 0
to 2
A(I,J) =
Int(InputBox("Enter the Value of of Array A"))
Next
Next
For I = 0 to 2
For J = 0
to 2
B(I,J) =
Int(InputBox("Enter the Value of of Array B"))
Next
Next
For I = 0 to 2
For J = 0
to 2
C(i,j) =
A(i,j) - B(i,J)
Next
Next
For I = 0 to 2
For J = 0 to 2
MsgBox
C(i,j)
Next
Next
Input
9
8
7
6
5
4
3
2
1
0
0
1
2
3
4
5
6
7
8
9
Output
9
7
5
3
1
-1
-3
-5
-7
-9
- Multiplication
of Two Arrays
Dim A(1,1), B(1,1), C(1,1)
For I = 0 to 1
For J = 0 to
1
A[I,J]
= Int(InputBox(“Enter the Value of A(”,I,”,”,J,”)”)
Next
Next
For I = 0 to 1
For J = 0
to 1
B[I,J]
= Int(InputBox(“Enter the Value of B(”,I,”,”,J,”)”)
Next
Next
For I = 0 to 1
For J = 0 to 1
Sum
= 0
For
K = 0 to 0
Sum
= Sum + A(I,K) + B(K,J)
C(I,J)
= Sum
Next
Next
Next
No comments:
Post a Comment