You are on page 1of 6

Reading Material

COMPUTER

I. Introduction

Decision making process is an essential part of programming. It can address


practical and give useful output to the user. For example, a Visual Basic program can
ask the computer to a certain task until a certain condition is met., or a program that will
reject non-numeric data.

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?

Saint John Bosco College of Northern Luzon, Inc.Page 1


IV. Discussion/Course Content

LESSON 20: CONDITIONAL, LOGIC AND LOOPS

 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.

This will return a true or false result.

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.

Logical operators are often used to address these issues.

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

1. Types of If Controls Structures:

 If… Then Statement

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

Saint John Bosco College of Northern Luzon, Inc.Page 2


Example:

Dim firstname As String


Firstname= “Maria”
If firstname = “ Maria” Then MessageBox.Show(“Firstname is Maria.”)

 If… Then… Else Statement

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 the condition is false, an alternative action is executed.

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… Then… Else If Statement

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

Saint John Bosco College of Northern Luzon, Inc.Page 3


Example:

Dim Mark As Integer


Dim Grade As string

Manuel = TextBox1.Text
If my Number >=90 Then
Grade=”A”

Else Mark>=70 and Mark<80 then

Grade=”B”

ElseIf MArk.=50 and Mark<60 then

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.

Select Case test expression


Case expression list 1
Block of one or more Visual Basic 2010 statements
Case expression list 2
Block of one or more Visual Basic 2010 statements
Case expression list 3
Block of one or more Visual Basic 2010 statements
Case expression list 4
Block of one or more Visual Basic 2010 statements
.
.
.
Case Else
Block of one or more Visual Basic 2010 statements
End Select

Saint John Bosco College of Northern Luzon, Inc.Page 4


Example:

Dim FriedFood As String


Dim HealthState As String

FriedFood=TextBox1.Text

Select Case Health State


Case ”Eaten”
HealthState=”Calorie Added”
Case=”Not Eaten”
HealthState=”No Added Calorie”
Case Else
HealthState=”Did not check”
End Select

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.

 For… Next Loop

For loop Is the most commonly used type of loop.

Dim As Integer
Dim startNumber As Integer

answer = 0

For startNumber = 1 To 4

answer = answer + startNumber

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:

For i = startNumber To endNumber


Next i

Saint John Bosco College of Northern Luzon, Inc.Page 5


 Do…Next Loop

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.

With a Do Loop you can use “While” and “Until”.

Dim number As Integer


Number = 1
Do While Number < 5
MessageBox.Show( number )
Number = number +1
Loop While number <5

V. References

Introduction to Visual Basic 2010:


https://www.youtube.com/watch?v=KNwG8YwYR9U

Visual Basic 2010 for Beginners:


https://www.youtube.com/watch?v=7lpuICNVP4U

VI. Post-Test

Via Google Classroom

Saint John Bosco College of Northern Luzon, Inc.Page 6

You might also like