‏لقطة شاشة ٢٠٢٤-٠٤-١٨ في ٧.١٥.٠٦ ص

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

University of Basrah

Collage of Engineering
Department of Petroleum
Computer Programming

Lecture Four
Control Structures

First Year

By

Assist Lect. Haider S. Mohammed


Computer Programming / VB2010 Assist Lect. Haider S. Mohammed

4. Control Structures

In this chapter, you will learn how to write Visual Basic code that can make
decision when it process input from the users, and control the program flow in
the process. Decision making process is an important part of programming
because it will help solve practical problems intelligently so that it can provide
useful output or feedback to the user. For example, we can write a Visual Basic
program that can ask the computer to perform certain task until a certain condition
is met, or a program that will reject non-numeric data. In order to control the
program flow and to make decisions, we need to use the conditional operators
and the logical operators together with the If control structure. To effectively
control the Visual Basic program flow, we shall use the If control structure
together with the conditional operators and logical operators. There are basically
three types of If control structures, namely:

• If ...Then
• If – Then –Else
• Select Case

4.1 If ... Then Statement:


This is the simplest control structure which ask the computer to perform a certain
action specified by the Visual Basic expression if the condition is true. However,
when the condition is false, no action will be performed. The general format for
the (If- Then) statement is

4.1-1 If Condition Then Statement


Where, Condition is usually a comparison, but it can be any expression that
evaluates to a numeric value, this value as true or false. If condition is True,
Visual Basic executes all the statements following the Then keyword.

1
Computer Programming / VB2010 Assist Lect. Haider S. Mohammed

Example 4-1: Write a program to enter the value of two variables (X and Y).
Find and print the maximum value for two variables. Design form window and
select all the control objects are used.

Solution
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim x, y, max As Integer
x = Val(TextBox1.Text)
y = Val(TextBox2.Text)
max = x
If y > x Then max = y
TextBox3.Text = CStr(max)
End Sub
End Class

4.1-2 If condition Then Goto n Where n : number of statement ( must be Positive


Integer value) for example: Goto 5 , Goto 16 , Goto 2010

2
Computer Programming / VB2010 Assist Lect. Haider S. Mohammed

Example 4.2: Used (If-Then Goto) condition to write a program for the
previous Example 4.1

Solution:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim x, y, max As Integer
x = Val(TextBox1.Text)
y = Val(TextBox2.Text)
If y > x Then max = y : GoTo 10
max = x
10: TextBox3.Text = CStr(max)
End Sub
End Class

Note: The statement Exit Sub used to stop the program without return to the
project window.

3
Computer Programming / VB2010 Assist Lect. Haider S. Mohammed

4.2 If - Block Statement:


4.2.1 (If – Then – EndIf) statement: The If...Then – EndIf Statement performs
an indicated action only when the condition is True; otherwise the action is
skipped.
If condition Then
VB Expression
End If
Example 4.3: Used (If – Then – EndIf) condition to write a program for the
previous Example 4.1
Solution
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim x, y, max As Integer
x = Val(TextBox1.Text)
y = Val(TextBox2.Text)
max = x
If y > x Then
max = y
End If
TextBox3.Text = CStr(max)
End Sub
End Class

4
Computer Programming / VB2010 Assist Lect. Haider S. Mohammed

4.2.2 (If – Then – Else) statement: The If – Then - Else statement allows the
programmer to specify that a different action is to be performed when a certain
action specified by the VB expression if the condition is True than when the
condition is false, an alternative action will be executed. The general format for
the If - Then - Else statement is
If condition Then
VB expression
Else
VB expression
End If
Example 4.4: Used (If – Then – Else) condition to write a program for the
previous Example 4.1
Solution
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim x, y, max As Integer
x = Val(TextBox1.Text)
y = Val(TextBox2.Text)
If y > x Then
max = y
Else
max = x
End If
TextBox3.Text = CStr(max)
End Sub
End Class

5
Computer Programming / VB2010 Assist Lect. Haider S. Mohammed

4.2.3 Nested (If – Then – Else) statement: If there are more than two alternative
choices, using just If – Then - Else statement will not be enough. In order to
provide more choices, we can use If...Then... Else statement inside If...Then...Else
structures. The general format for the Nested If...Then…Else statement is

Example 4.5: Write a program to enter the value of variable (Mark). Find the
grade using If – Block statement and display the value of grade in a text box.
When the value of variable (Mark) exceed 100, write a Message Box (Wrong
entry, please Re-enter the Mark). Design form window and select all the control
objects are used.

6
Computer Programming / VB2010 Assist Lect. Haider S. Mohammed

Solution:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim Mark As Single, Grade As String
Mark = Val(TextBox1.Text)
If Mark > 100 Then
MessageBox.Show("Wrong entry, Re-enter the Mark")
TextBox1.Text = "" : TextBox2.Text = "" : Exit Sub
ElseIf Mark >= 90 Then
Grade = "Excellent"
ElseIf Mark >= 80 Then
Grade = "Very Good"
ElseIf Mark >= 70 Then
Grade = "Good"
ElseIf Mark >= 60 Then
Grade = "Medium"
ElseIf Mark >= 50 Then
Grade = "Pass"
Else : Grade = "Fail"
End If
TextBox2.Text = (Grade)
End Sub

4.3 Select- Case statement: Select - Case structure is an alternative to If –


Then - ElseIf for selectively executing a single block of statements from among
multiple block of statements. The Select Case control structure is slightly
different from the If - ElseIf control structure. The difference is that the Select
Case control structure basically only makes decision on one expression or

7
Computer Programming / VB2010 Assist Lect. Haider S. Mohammed

dimension while the If – ElseIf statement control structure may evaluate only one
expression, each If – ElseIf statement may also compute entirely different
dimensions. Select- Case is more convenient to use than the If- Else - End If. The
format of the Select Case control structure is as follows:

Select Case test expression


Case expression list 1
VB statements
Case expression list 2
VB Statements
Case expression list 3
VB statements
Case expression list 4

Case Else
VB Statements
End Select

Example 4.6: Example 4.5 can be rewritten as follows:

8
Computer Programming / VB2010 Assist Lect. Haider S. Mohammed

Solution 1
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim Mark As Integer, Grade As String
Mark = Val(TextBox1.Text)
Select Case Mark
Case 0 To 49
Grade = "Fail"
Case 50 To 59
Grade = "pass"
Case 60 To 69
Grade = "medium"
Case 70 To 79
Grade = "good"
Case 80 To 89
Grade = "very good"
Case 90 To 100
Grade = "excellent"
Case Else
MessageBox.Show("wrong entry, please re-enter the mark")
TextBox1.Text = ""
TextBox2.Text = ""
Exit Sub
End Select
TextBox2.Text = Grade
End Sub
End Class

9
Computer Programming / VB2010 Assist Lect. Haider S. Mohammed

Solution 2
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim Mark As Integer, Grade As String
Mark = Val(TextBox1.Text)
Select Case Mark
Case Is > 100, Is < 0
MessageBox.Show("wrong entry, please re-enter the mark")
TextBox1.Text = "" : TextBox2.Text = "" : Exit Sub
Case Is >= 90
Grade = "excellent"
Case Is >= 80
Grade = "very good"
Case Is >= 70
Grade = "good"
Case Is >= 60
Grade = "medium"
Case Is >= 50
Grade = "pass"
Case Is <= 49
Grade = "fail"
End Select
TextBox2.Text = (Grade)
End Sub
End Class

10
Computer Programming / VB2010 Assist Lect. Haider S. Mohammed

H.W
1. Write a program to enter a user name and display the message (hello) three
times. The first one for (Mena), the second one for (Nastia) and the third for any
user as a guest.

2. Design a form with four TextBoxes and three Buttons. Design the program so
that the values of num1, num2, and Symbol are entered into separate three text
boxes. Write a code to perform (add, subtract, multiply and divide) when pressing
on Button (Calculate). Display the result in separate text box. The Button (Clear)
used to clear values in TextBoxes. Click Button (Exit) to end the program and
return to the project window.

11

You might also like