Professional Documents
Culture Documents
COMPUTER
I. Introduction
To control the program flow and to arrive at decisions, there is a ne ed to use the
conditional and logical operators together with the if control structure.
II. Objectives
At the end of the lesson, you should be able to;
1. define conditional and logical operators;
2. differentiate the types of if controls
3. define loops In Visual Basic 2010;
4. differentiate for loop and do loop;
5. describe the types of do loop; and
6. demonstrate how to use logical statements, conditional statements and loops in
programming.
III. Pre-Test
Answer the following questions;
1. What the difference between conditional and logical operators?
2. What are the types of if controls?
3. When do we use the select case statement?
Logical operators are powerful tools that resemble mathematical operators. This operators
allows VB2010 program to compare data values and then decide what actions to take ,
whether to execute or terminate the program.
Operator Meaning
= Equal to
< Less than
> Greater than
<= Less than and equal
>= More than and equal
<> Not equal to
The conditional operators allow refinement on what is being test for: Instead of saying “If
X is equal to Y”, you can specify whether it’s greater than, or equal to. However, there are
cases that using numerical comparison is not enough.
Operator Meaning
and Both sides must be true
or One side or the other must be true
xor One side or the other must be true but
not both
not Negates truth
This is the simplest control structure which asks the computer to perform a certain
action specified by the VB2010 expression if the condition is true. However, when the
condition is false, no action is taken.
If condition then
VB2010 expression
End if
If… Then does not provide options for the users. To provide a choise, use If… Then…
Else statement.
This control structure asks the computer to do a certain task if the condition is true.
If Condition Then
Visual Basic 2010 Expression
Else
Visual Basic 2010 expression
End if
Example:
firstname=”Manuel”
If firstname = “Manuel” Then
MessageBox.Show(“Firstname is Maria”)
Else
MessageBox.Show(“Firstname is not Maria”)
End If
If there are more than two alternative choices, the If… Then… Else If statement can be
executed.
If Condition Then
Visual Basic 2010 Expression
ElseIf condition Then
Visual Basic 2010 expression
ElseIf condition Then
Visual Basic 2010 expression
.
.
Else
Visual Basic 2010 expression
End if
Manuel = TextBox1.Text
If my Number >=90 Then
Grade=”A”
Grade=”B”
Grade=”C”
Else
Grade=”D”
End If
2. Select Case
The Select Case is another way to test what is inside of a variable.Select case is preferred
when there are multiple conditions.
FriedFood=TextBox1.Text
MessageBox.Show(HealthState)
Understanding Loops
Visual Basic 2010 allows procedures to be repeated as many times as long as the
processor and memory could support. This called looping.
This is required when there’s a need to process something repetitively until a certain
condition is met.
Dim As Integer
Dim startNumber As Integer
answer = 0
For startNumber = 1 To 4
Next startNumber
messageBox.Show(answer)
A for loop needs a start position and an end position, and all on the same line.
A for loop needs a way to get the number in the loop.
A for loop without any code to execute looks like this:
In For… Loop, there is a specific number as to how many times the looping will
happen. If you don’t know how many times the looping will happen, a Do loop is best to use.
V. References
VI. Post-Test