Sie sind auf Seite 1von 49

Conditional

Statements

IT Training Module 4 VISUAL BASIC 6 1


A. IF..THEN
Structure

IT Training Module 4 VISUAL BASIC 6 2


If…Then Structure

The If… Then structure presents a


condition and if the condition is
True, the statement or series of
statements (contains event) in
between the If and End If will be
executed.

IT Training Module 4 VISUAL BASIC 6 3


Syntax:

If condition Then
Statement/s
End If

IT Training Module 4 VISUAL BASIC 6 4


B. IF..THEN…Else
Structure

IT Training Module 4 VISUAL BASIC 6 5


If…Then…Else Structure

Like the If… Then structure, a


condition will be presented and if
the condition is True, the statement
or series of statements (contains
event) after the If statement will be
executed, and if False, it will
execute the statement after Else.
IT Training Module 4 VISUAL BASIC 6 6
Good Programming Practice
• Indent the statements after then and else

IT Training Module 4 VISUAL BASIC 6 7


Syntax:

If condition Then
Statement/s
Else
Statement/s
End If

IT Training Module 4 VISUAL BASIC 6 8


Example 1:

If score = 100 Then


Lbl1.caption = “Perfect”
Else
Lbl1.caption = “With mistakes”
End If

IT Training Module 4 VISUAL BASIC 6 9


Example 2:

If PName = “Anna” Then


Lbl1.caption = “ANNA”
MsgBox ("Hello, Anna!!!")
Else
Lbl1.caption = “UNIDENTIFIED”
MsgBox ("Unauthorized Person!!!")
End If
IT Training Module 4 VISUAL BASIC 6 10
Example 3:
Output of Example 3:

Output when Anna is entered Output when Rosie is entered


C. Nested IF
Structure

IT Training Module 4 VISUAL BASIC 6 13


Nested IF Structure

The Nested If structure uses several


conditions.

IT Training Module 4 VISUAL BASIC 6 14


ElseIF Structure

If the condition is True, the statement/s


after the If condition will be executed.
If the condition is False, each condition in
the ElseIf is evaluated until it gives a True
result then the statement/s following it will
be immediately executed. If no condition
was met, the statement/s after Else will be
executed.
IT Training Module 4 VISUAL BASIC 6 15
Syntax:
If condition1 Then
Statement/s 1
ElseIf condition2 Then
Statement/s 2
ElseIf condition3 Then
Statement/s 3
Else
Statement/s 4
End If
Example 1:
If Num <=100 Then
Num= Num + 1
ElseIf Num <= 200 Then
Num = Num + 2
ElseIf Num<= 300 Then
Num = Num + 3
Else
Num = Num + 0
End If
Example 2:
Output Example 2:

Input Box

Message Box
(Output when
MARK is entered)
D. SELECT
Case

IT Training Module 4 VISUAL BASIC 6 20


SELECT Case

The Select Case presents several


conditions. It is used instead of the
Nested If because it is clearer and
easier to use. It evaluates an
expression once. The result is
compared with several values, if they
match, the statement in the statement
block is executed.
IT Training Module 4 VISUAL BASIC 6 21
Select Case expression
Syntax:
Case Value1
Statement block 1
Case Value 2
Statement block 2
Case Value 3
Statement block 3
Case Value 4
Statement block 4
Case Else
Statement block
End Select
Select Case Subject
Case 1
LblSubject.Caption = “Mathematics”
Case 2
Example 1: LblSubject.Caption = “Science”
Case 3
LblSubject.Caption = “English”
Case 4
LblSubject.Caption = “Filipino”
Case 5
LblSubject.Caption = “Religion”
Case 6
LblSubject.Caption = “Physical Education”
Case 7
LblSubject.Caption = “Computer”
Case 8
LblSubject.Caption = “Music”
End Select
Example 2:
Output of Example 2:

Output when Enter button is clicked after user input


Time Allotment:
HANDS-ON ACTIVITY 20 minutes

1. Create a program that will ask for a username


and password.
Username: Manuel25
Password: October19
2. If the username and password are both correct,
a message box will appear saying “YOU MAY
NOW ENTER”, otherwise the message will be
“TRY AGAIN!!!”
3.Design your user interface.
File: Password Project: Validation
IT Training Module 4 VISUAL BASIC 6 26
Loop
Statements

IT Training Module 4 VISUAL BASIC 6 27


A. Do…While
Structure

IT Training Module 4 VISUAL BASIC 6 28


Do…While Structure

The Do… While is used to execute


a block of statements in an
unspecified number of times while
a condition is True. If condition is
false on the first pass, the
statements are not executed.

IT Training Module 4 VISUAL BASIC 6 29


Syntax:

Do While condition
Statements
Loop
Example 1:

Dim Number as Integer


Number = 10

Do while Number < 20


Number=Number+2
Print Number
Loop
Output of Example 1 or 2:
B. Do…Until
Structure

IT Training Module 4 VISUAL BASIC 6 33


Do…Until Structure

The Do… Until is used to execute a


block of statements in an
unspecified number of times until a
condition becomes True. If
condition is True on the first pass,
the statements are not executed.

IT Training Module 4 VISUAL BASIC 6 34


Syntax:

Do Until condition
Statements
Loop
Example 1:

Dim Number as Integer


Number = 30

Do Until Number <= 20


Number=Number - 2
Print Number
Loop
Output of Example 1 or 2:
C. For…Next
Structure

IT Training Module 4 VISUAL BASIC 6 38


For…Next Structure

The For…Next Loop is used when


you know the how many times the
statements of the loop will be
executed. It uses a variable that
serves as a counter that increases
or decreases in value every time
there is a loop.
IT Training Module 4 VISUAL BASIC 6 39
In the For…Next Loop execution

1. The counter is set to the value of start.


2. Counter is checked to see if it is greater than
the end. If yes, it passes to the statements
after the Next, if not, the statements are
executed.
3. The counter is incremented by the amount
specified.
***If not specified, the counter is
incremented by 1.

4. Counter is incremented
IT Training Module 4
and goes back to
VISUAL BASIC 6 40
step 2.
Syntax:
For counter = start To end
Statements
Next

OR
For counter = start To end [Step increment]
Statements
Next
Example 1:

Dim Num As Integer


For Num = 10 To 15
Print Num
Next Num
Output of Example 1:
Example 2:

Dim Num As Integer


For Num = 10 To 100 Step 10
Print Num
Next Num
Output of Example 2:
Example 3:

Dim Num As Integer


For Num = 3 To 30 Step 3
Print Num
Next Num
Output of Example 3:
Time Allotment:
HANDS-ON ACTIVITY 20 minutes

Create a program that will display the


numbers increased by 5 and will only
stop when it reached 100. Start the
number with 1

Project: Loop 100 File: Loop

IT Training Module 4 VISUAL BASIC 6 48


End of Part IV

IT Training Module 4 VISUAL BASIC 6 49

Das könnte Ihnen auch gefallen