0% found this document useful (0 votes)
138 views

Chapter-2: Control Structures in Visual Basic 6.0

This document discusses various control structures in Visual Basic 6.0 including if/then, if/then/else, select case, and for/next loop structures. It provides the syntax for each structure and examples of how to use them. The if/then structure performs actions when a condition is true, while if/then/else specifies different actions for true or false conditions. Select case provides an alternative to nested if/then/else statements for selecting blocks of code. The for/next loop iterates over a range of numbers or other values.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
138 views

Chapter-2: Control Structures in Visual Basic 6.0

This document discusses various control structures in Visual Basic 6.0 including if/then, if/then/else, select case, and for/next loop structures. It provides the syntax for each structure and examples of how to use them. The if/then structure performs actions when a condition is true, while if/then/else specifies different actions for true or false conditions. Select case provides an alternative to nested if/then/else statements for selecting blocks of code. The for/next loop iterates over a range of numbers or other values.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

Chapter-2

Control Structures in Visual Basic 6.0

Terms To Touch.

Control Statements are used to control the flow of


program's execution. Visual Basic supports control
structures such as
if... Then,
if...Then ...Else,
Select...Case,
and Loop structures such as
Do While...Loop,
While...Loop,
For...Next etc method.

If...Then selection structure

The If...Then selection structure performs an


indicated action only when the condition is
True; otherwise the action is skipped.
Syntax of the If...Then selection
If <condition> Then
statement
End If
e.g.: If marks>75 Then
txtGrade.Text = Grade A"
End If

If...Then...Else selection

structure
The If...Then...Else selection structure allows the

programmer to specify that a different action is to be


performed when the condition is True than when the
condition is False.
Syntax of the If...Then...Else selection
If <condition > Then
statement1
Else
statement2
End If
e.g.: If marks>50 Then
txtGrade.Text = "Pass"
Else
txtGrade.Text = "Fail"
End If

Nested If...Then...Else selection


structure

Nested If...Then...Else selection structures


test for multiple cases by placing
If...Then...Else selection structures inside
If...Then...Else structures.
Syntax of the Nested If...Then...Else selection
structure

Nested If...Then...Else selection


structure

Method 1
If < condition 1 > Then
statement1
ElseIf < condition 2 > Then
statement2
ElseIf < condition 3 > Then
statement3
Else
Statement4
End If

Nested If...Then...Else selection


structure
Method 2

If < condition 1 > Then


statement1
Else
If < condition 2 > Then
statement2
Else
If < condition 3 > Then
statement3
Else
Statement4
EndIf

Nested If...Then...Else selection


structure
e.g.: Assume you have to find the grade using

nested if and display in a text box


If marks> 75 Then
txtGrade.Text = Grade A"
ElseIf marks > 65 Then
txtGrade.Text = " Grade B"
ElseIf marks > 55 Then
txtGrade.text = " Grade C"
ElseIf marks > 45 Then
txtGrade.Text = " Grade D"
Else
txtGrade.Text = " Grade F"
End If

Select...Case selection
structure
Select...Case structure is an alternative to

If...Then...ElseIf for selectively executing a single block


of statements from among multiple block of statements.
Select...case is more convenient to use than the
If...Else...End If. The following program block illustrate
the working of Select...Case.
Syntax of the Select...Case selection structure
Select Case Index
Case 0
Statements
Case 1
Statements
End Select

Select...Case selection
structure
e.g.: Assume you have to find the grade using select...case and

display in the text box


Dim marks as Integer

marks = txtAverage.Text
Select Case marks
Case 100 To 75
txtGrade.Text =Grade A"
Case 74 To 65
txtGrade.Text =Grade B"
Case 64 To 55
txtGrade.Text =Grade C"
Case 54 To 45
txtGrade.Text =Grade D"
Case 44 To 0
txtGrade.Text =Grade F"
Case Else
MsgBox "Invalid marks"
End Select

The For...Next Loop

The For...Next Loop is another way to make


loops in Visual Basic. For...Next repetition
structure handles all the details of countercontrolled repetition.
Syntax of the For.Next looping structure
For x = startNumber to endNumber (Step increment)
print x
Next x

The For...Next Loop

n order to count the numbers from 1 yo 50 in


steps of 2, the following loop can be used
For x = 1 To 50 Step 2
Print x
Next
For counter=1 to 10
Text1.Text=counter
Next

The For...Next Loop

For counter=1 to 1000 step 10


counter=counter+1
Next
For counter=1 to 1000 step 10
counter=counter+1
Next

The For...Next Loop

Dim number As Integer


For number = 1 To 10
If number = 4 Then
Print "This is number 4"
Else
Print number
End If
Next

Thank You

You might also like