Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

I0060

05 Laboratory Exercise 1
Conditions and Loops
Objectives:

At the end of the exercise, the students should be able to:

 use conditional statements to control program flow, and


 implement loop structures.

Materials:

 Computer with Visual Basic 2008 Express Edition installed


 Flash drive

Basic Principles:

Conditions or conditional statements, and loops are used to control the program flow. With this
activity, the students will be knowledgeable on using these structures to develop program when a
condition occurs.

Procedures:

Activity # 1: Conditions – creating If statements

1. Open a New Project and name it as ConAndLoops.

2. Add one Textbox, one Label and one Button Control to the form.

3. Change the Name Property of the controls as follows:


• Textbox1 – txtNum
• Label1 – lblResult
• Button1 – btnShow

4. Change the Text Property of the controls as follows:


• lblResult – Result
• btnShow – Show

5. Copy the code below for button Show under the BtnShow_Click():

Dim x As Integer
x = txtNum.Text
If x Mod 2 = 0 Then
lblResult.Text = "The number is Even"
End If

6. Run the program.

7. Enter values which are multiple of 2. Click the Show button and observe what happens.

8. Enter values which are not multiple of 2. Click the Show button and observe if changes occur.

9. Stop the program.

10. Insert the added code below, If Else conditional statement, for button Show:
Dim x As Integer
x = txtNum.Text

05 Laboratory Exercise 1 *Property of STI


Page 1 of 4
I0060

If x Mod 2 = 0 Then
lblResult.Text = "The number is Even"
Else
lblResult.Text = “The number is Odd”
End If

11. Run the program.

12. Enter values which are multiple of 2. Click the Show button and observe what happens. Then,
enter values which are not multiple of 2. Click the Show button and observe changes.

13. Stop the program.

Figure 5.1 Form1 – Even or Odd

Activity #2: Looping

1. On the same project, change the Text Property of lblResult to Prime or Composite.

2. Replace the code in BtnShow under BtnShow_Click with the code below:

Dim x, y As Integer
Dim z As Boolean
x = txtNum.Text
z = True

For y = 2 To (x - 1)
If x Mod y = 0 Then
z = False
Exit For
End If
Next

If z Then
lblResult.Text = "It is a prime number"
Else
lblResult.Text = "It is a composite number"
End If

05 Laboratory Exercise 1 *Property of STI


Page 2 of 4
I0060

3. Run the program.

4. Enter values that can be divided only by 1 or itself like 5, 7, 11. Then click btnShow and
observe what happens.

5. Enter values that can be divided by numbers other than 1 and itself like 6, 9, 10. Then, click
the btnShow and observe what happens.

Figure 5.2 Form1 – Prime or Composite

Challenged Exercise:

1. Using Select Case, create a program that


will determine if a person is a/an:

a) Infant – 0 to 2 years

b) Child – 3 to 9

c) Adolescent – 10 to 19
Figure 5.3 Form2
e) Adult – 30 to 39

f) Middle Age – 40 to 60

g) Elder – 60 onward

Figure 5.4 Form2 – Message box

2. Add another form on the same project.

05 Laboratory Exercise 1 *Property of STI


Page 3 of 4
I0060

3. On the new Windows Form, with the use of loop, create a program that will display a Message
box starting with supplied value based from the entered start value and end to the entered end
value.

For example, the input start integer is 6 and the end integer is 12, so the Message box will
appear 7 times, first with value of 6, next is 7, then 8, 9, 10, 11, and 12.

Figure 5.5 Form3

Figure 5.6 Message boxes

4. Run the program.

5. Save the laboratory exercise.

05 Laboratory Exercise 1 *Property of STI


Page 4 of 4

You might also like