Professional Documents
Culture Documents
S TR U C TU R ES
ControlFlow
In a program, statements may be
SequentialConstruct
The sequential construct means the
Stament 2
Stament 3
Selection Construct
The selection construct means the
Selection Construct
One course-of-action
Condition
?
true
Statement 1
false
Statement 1
Statement 2
Another
courseof-action
Statement 2
Iterative Constructs
The iterative or repetitive
Iterative Construct
false
The exit condition
Condition
?
True
Statement 1
Statement 2
The loop
body
Selection Constructs
VBScript provides two types of
selection construct :
1) If statement
2) Select Case statement
The If Statement : If statement of VB
Script comes in various forms & are
given below:
1) If..Then Statement
2) If..Then..Else Statement
3) If..Then..ElseIf Statement
4) Nested Ifs
If (Num>0) Then
Print It is a positive number
End if
Example 2 :
If txtAge.Text>=18 Then
Print You are eligible to vote
End if
If txtAge.Text>=18 Then
Print You are eligible to vote
Else
Print Sorry, You are not eligible to vote
End If
Example 2 :
If..Then..ElseIfStatem ent
If..Then..ElseIf statement is used to test a
N ested Ifs
A nested If is an if that has another If in its
N ested Ifs
2. If (expression 1) Then
body-of-if
Else
:
If (expression 2) Then
Statement-1
[Else
Statement-2]
End If
N ested Ifs
3) If (expression 1) Then
:
If (expression 2) Then
Statement-1
[Else
Statement-2]
End If
Else
If (expression 3) Then
Statement-3
[Else
Statement-4]
:
End If
End If
D iff
erent form s ofSelect-Case
1. Select Case : Simplest Form [Exact
match]
Select Case Expression
Case Value
one or more visual basic Scripts
tatements
Case Value
one or more visual basic Script tatements
Case Else :
one or more visual basicScript statements
Syntax ofForm 2
Select Case : Second Form [Relational
Test]
Select Case Expression
Case is relation :
one or more visual basic script
statements
Case is relation :
one or more visual basic script
statements
[Case Else :
one or more visual basic script
Looping Structures
VB offers broadly following three types
of looping structures :
1. For..Next
2. Do Loop
a) Do While..Loop
b) Do..Loop While
c) Do Until..Loop
d) Do..Loop Until
3. While..Wend
Exam ples
Example 1 : Generate natural nos from
1 to 100
For I = 1 To 100
Print I
Next I
Example 2 : Generate first 20 even
nos.
For E = 2 to 40 Step 2
Print E
Next E
number N.
:
Fact=1
For I= 1 to N
Fact = Fact * I
Next I
Print Factorial of ; N; =; Fact
:
D o..Loop Structures
Do While..Loop : Do While loop is an
Dim A as Byte
A=10
Do While A>10
A=A-1
Loop
Example 2 : Executes loop
Dim P as Byte
P=20
Do While P>5
P=P-2
Loop
D o..Loop W hile
Do Loop While is an exit controlled loop
Exam ples
Example 1 :
Do
num = InputBox (Enter a number)
sum = sum + num
Loop While num < > 0
Here the statements inside the loop will
be executed once no matter what the
comparison test evaluates to.
D o..U ntilLoop
Do Until loop is an entry controlled loop
Dim A as Byte
A=10
Do Until A<10
A=A-1
Loop
Example 2 : Executes loop
Dim P as Byte
P=20
Do Until P<5
P=P-2
Loop
D o..Loop U ntil
Do Loop Until is an exit controlled loop
Exam ples
Example 1 :
Do
num = InputBox (Enter a number)
sum = sum + num
Loop Until num = 0
Here the statements inside the loop will
be executed once no matter what the
comparison test evaluates to.
W hile..W end
While..Wend loop is functionally
While <Condition>
one or more vbscript statements
Wend
Exam ples
Example 1 : Generate the sum of first
10 natural nos
I=1
While I<=10
Sum = Sum + I
I=I+1
Wend
Print Sum of 10 natural nos = ; Sum
N ested Loops
A loop within another loop is called as
Nested Loop.
Example :
For I = 1 to 5
For J = 1 To 3
Print J
Next J
Print
Next I
Inner Loop
Outer Loop
Exam ples
Example : Program to generate the output
given below :
1
2
3
4
5
2
33
444
5555
Sol :
For I = 1 To 5
For J = 1 To I
Print I;
Next J
Print
Next I