Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
22 views

Week 5 - Control Structures I

- The document discusses learning objectives around event-driven programming and control structures/decision structures in Visual Basic 2015. It covers concepts like the if statement, elseif statement, nested if statements, and comparison operators. Examples are provided to demonstrate how to use these structures to control program flow based on different conditions. Students will learn how to make decisions in their code using if/elseif/else and comparison operators.

Uploaded by

10303374
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Week 5 - Control Structures I

- The document discusses learning objectives around event-driven programming and control structures/decision structures in Visual Basic 2015. It covers concepts like the if statement, elseif statement, nested if statements, and comparison operators. Examples are provided to demonstrate how to use these structures to control program flow based on different conditions. Students will learn how to make decisions in their code using if/elseif/else and comparison operators.

Uploaded by

10303374
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

LEARNING OBJECTIVES

• Students will be taught the following:


• Concept of Event-Driven Programming
• How to use the If Statement
• Allowing Multiple Alternatives with ElseIf
• Comparison Operators
CONTROL STRUCTURES/DECISION STRUCTURES
• The Concept of Event-Driven Programming: Visual Basic 2015 is an event-driven
programming language.
• Event-driven means that the code is executed in response to events triggered by the user
actions:
• Like clicking the mouse, pressing a key on the keyboard, selecting an item from a drop-
down list, typing some words into textbox and more.
• It may also be an event that runs in response to some other programs.
• Some of the common events in Visual Basic 2015 are load, click, double-click, drag and
drop, pressing the keys and more.
• Every form and every control you place on the form has a set of events related to them.
• To view the events, double-click the control (object) on the form to enter the code
window.
• The default event will appear at the top part on the right side of the code window.
• You need to click on the default event to view other events associated with the control.
EVENT DRIVEN PROGRAMMING
• Demonstration of the following:
• Button click
• Mouse- click,
• Mouse- double click
• Mouse-hover
• Mouse-leave, etc.
• Demonstration of Text changed event.
CONTROLLING THE FLOW
• Controlling the flow through your algorithms can be achieved by making
decisions.
• For example: “If the value of X is 1, go and do A; otherwise do B.”
• This ability to make decisions is known as branching.
• Algorithms often include decisions. It’s this decision-making ability that makes
computers do what they do so well.
• When you’re writing code, you make two kinds of decisions.
• The first kind is used to find out what part of an algorithm you’re currently
working on or to cope with problems.
• For example, imagine that you need to write a piece of code to send an email to
10 individuals.
• To do this, after sending each email, you ask, “Have I finished?”
• If so, you quit the algorithm; otherwise, you get the next person in the list
CONTROLLING THE FLOW
• As another example, you might need to open a file, so you ask, “Does the file
exist?”
• You have to deal with both possible answers to that question.
• If file exist open it, else quite the program.
• The second kind of decision is used to perform a different part of the algorithm
depending on one or more facts
• Imagine you’re going through your list of 10 people so that you can send an email
to those who own a computer, but telephone those who don’t.
• As you look at each person, you use the fact that the person does or doesn’t own
a computer to choose what you should do.
THE IF STATEMENT
• The simplest way to make a decision in a Visual Basic 2015 program is to use the If…Then
statement.
• You learn to use an If…Then statement in the following Try It Out exercise.
• Create a Windows Forms Application project called Simple If.
• Add a Button control, setting its Name property to btnIf, and its Text property to If.
• Doubleclick the button and add the following bolded code:
Private Sub btnIf_Click(sender As Object, e As EventArgs) Handles btnIf.Click
'Declare and set a variable
Dim intNumber As Integer = 27
'Here's where you make a decision,
'and tell the user what happened
If intNumber = 27 Then
MessageBox.Show("'intNumber' is, indeed, 27!", "Simple If")
End If
THE IF STATEMENT (SIMPLE IF)
• Save your project and then run it.
• Click the If button and you’ll see the message box shown in Figure 4.1.
THE IF STATEMENT (SIMPLE IF)
• In this example, you see how to code for when the If statement is not true.
• Return to the Forms Designer for the Simple If program.
• Add another Button control to the form and set its Name property to btnAnotherIf and its Text
property to Another If.
• Double-click the button and add the following bolded code:
• Private Sub btnAnotherIf_Click(sender As Object, e As EventArgs) Handles btnAnotherIf.Click
Private Sub btnIf_Click(sender As Object, e As EventArgs) Handles btnIf.Click
'Declare and set a variable
Dim intNumber As Integer = 27
'Here's where you make a decision,
'and tell the user what happened
If intNumber = 1000 Then
MessageBox.Show("'intNumber' is, indeed, 1000!", "Simple If")
End If
THE ELSE STATEMENT
• If you want to run one piece of code if the condition is true and another piece if the condition is
false, then you use the Else statement.
• This expands on the previous Try It Out.
• This Try It Out builds on the previous Try It Out to show how the Else statement works
• Return to the Code Editor in the Simple If project and modify the code in the btnAnotherIf_Click
procedure so that it looks like this:
Private Sub btnAnotherIf_Click(sender As Object, e As EventArgs) Handles btnAnotherIf.Click
'Declare and set a variable
Dim intNumber As Integer = 27
'Here's where you make a decision, and tell the user what happened
If intNumber = 1000 Then
MessageBox.Show("'intNumber' is, indeed, 1000!", "Simple If")
Else
MessageBox.Show("'intNumber' is not 1000!", "Simple If")
End If
THE ELSE STATEMENT
• Run the project and click btnAnotherIf. You’ll see the message box shown in
Figure 4.2.
ALLOWING MULTIPLE ALTERNATIVES WITH ELSEIF
• If you want to test for more than one condition, you need to make use of the ElseIf statement.
• In this Try It Out, you’ll use your Simple If program to see how you can test for the value of
intNumber being 27 and 1000.
• Return to the Code Editor and change the code in the btnAnotherIf_Click procedure so that it looks
like this:
Private Sub btnAnotherIf_Click(sender As Object, e As EventArgs) Handles btnAnotherIf.Click
'Declare and set a variable
Dim intNumber As Integer = 27
'Here's where you make a decision, and tell the user what happened
If intNumber = 1000 Then
MessageBox.Show("'intNumber' is, indeed, 1000!", "Simple If")
ElseIf intNumber = 27 Then
MessageBox.Show("'intNumber' is 27!", "Simple If")
Else MessageBox.Show("'intNumber' is neither 1000 nor 27!", "Simple If")
End If
ALLOWING MULTIPLE ALTERNATIVES WITH ELSEIF
• Run the project and click the Another If button. You’ll see the message box shown
in Figure 4.3.
ALLOWING MULTIPLE ALTERNATIVES WITH ELSEIF
• Students to try the following:
LEVEL FEES
100 1500
200 2500
300 3000
400 3500

• Write a program in Visual Basic that does the following:


• Allow a user to enter his level in a textbox or select from a dropdown
• Display the appropriate fees.
ALLOWING MULTIPLE ALTERNATIVES WITH ELSEIF
• NOTE You can add as many ElseIf statements as you need to test for conditions.
• However, bear in mind that each ElseIf statement is executed as Visual Basic
2015 attempts to discover whether the condition is true.
• This slows your program if you have a lot of conditions to be tested.
• If this is the case, you should try to put the statements in the order they are most
likely to be executed, with the most common one at the top.
• This is faster as the code exits after finding the first true test.
• Alternatively, you should use a Select Case block,
NESTED IF STATEMENTS
• It’s possible to nest an If statement inside another:
• Consider the following:
LEVEL 100 200 300 400
IT 1500 2000 2500 3000
CS 1400 1900 2400 2900
LAW 1600 2100 2600 3100

• Develop a program that allows a user to:


• Enter the his or her level and program
• Display the appropriate fees for that students
• There’s no real limit to how far you can nest your If statements.
• However, the more levels of nesting you have, the harder it is to follow what’s
happening in your code.
SINGLE-LINE IF STATEMENT
• The single-line form of the If statement is typically used for short, simple tests,
and it saves space in the Code Editor.
• However, it doesn’t provide the structure and flexibility of the multiline form and
is usually harder to read:
• If intX = 3 Then MessageBox.Show("intX = 3") Else MessageBox.Show("intX is not 3")
• You don’t need an End If at the end of a single-line If…Then statement.
• Multiple statements can also be executed within a single-line If…Then statement.
• All statements must be on the same line and must be separated by colons, as in
the following example:
• If intX = 3 Then MessageBox.Show("intX = 3"): intX = intX + 1: Total += intX
COMPARISON OPERATORS
• We have already considered how to use the If together with the equal to (=)
operator.
• Now lets consider how to use the If with other comparison operators.
• We can use If with comparison operators to ask the following questions, all of
which have yes/no answers:
• Is intNumber greater than 49?
• Is intNumber less than 49?
• Is intNumber greater than or equal to 49?
• Is intNumber less than or equal to 49?
• Is strName not equal to Ben?
• When working with string values, most of the time you’ll use the Equal To or Not
Equal To operator.
• When working with numeric values (both integer and floatingpoint), you can use all
COMPARISON OPERATORS
• Using Not Equal To: We have learnt how to use Equal To with strings in the
previous slides.
• Lets now test the Not Equal To operator with strings.
• The Not Equal To (<>) operator will be false when Equal To (=) is true, and it will
be true when Equal To is false
• Create a Windows Forms Application project called If Demo.
• Add a TextBox control and a Button control.
• Set the Name property for TextBox1 to txtName and the Text property to
Stephanie.
• Set the Name property for Button1 to btnCheck and the Text property to Check.
USING NOT EQUAL TO
• Double-click the Button control to create its Click event handler. Add the bolded code:
Private Sub btnCheck_Click(sender As Object, e As EventArgs) Handles btnCheck.Click
'Declare a variable and get the name from the text box
Dim strName As String
strName = txtName.Text
'Is the name Wendy?
If strName <> "Wendy" Then
MessageBox.Show("The name is *not* Wendy.", "If Demo")
End If
End Sub
• Save your project and then run it.
• When the form is displayed, click the Check button and you will see a message box
indicating that the name is not Wendy
USING THE NUMERIC OPERATORS
• In this section, you take a look at the four other comparison operators you can
use.
• These are all fairly basic, so you’ll go through this quite fast.
• Using Less Than: In this try out we are going to learn how to use the Less Than
operator (<) to make decisions.
• Return to the Forms Designer for the If Demo project.
• Add another TextBox control and set its Name property to txtValue.
• Add another Button control and set its Name property to btnCheckNumbers and
its Text property to Check Numbers.
• Double-click the Check Numbers button and add the following bolded code to its
Click event handler:
USING LESS THAN
Private Sub btnCheckNumbers_Click(sender As Object, e As EventArgs) Handles btnCheckNumbers.Click
'Declare variable
Dim intNumber As Integer
Try
'Get the number from the text box
intNumber = CType(txtValue.Text, Integer)
Catch
Exit Sub
End Try
'Is intNumber less than 27?
If intNumber < 27 Then
MessageBox.Show("Is 'intNumber' less than 27? Yes!", "If Demo")
Else
MessageBox.Show("Is 'intNumber' less than 27? No!", "If Demo")
End If
USING LESS THAN
• Run the project.
• Enter 14 into the text box and click the Check Numbers button.
• You’ll be told whether the number entered is less than or greater than 27, as
shown in Figure 4.4.
USING THE LESS THAN OR EQUAL TO OPERATOR
• The Less Than Or Equal To operator will be true when the tested value is less than the comparison
value and also when the two values are equal.
• You will see this next.
• Return to the Code Editor and change the If statement in the btnCheckNumbers_Click event handler,
as shown here:
Try
'Get the number from the text box
intNumber = CType(txtValue.Text, Integer)
Catch
Exit Sub
End Try
'Is intNumber less than 27?
If intNumber <= 27 Then
MessageBox.Show("Is 'intNumber' less than 27? Yes!", "If Demo")
Else MessageBox.Show("Is 'intNumber' less than 27? No!", "If Demo")
USING THE LESS THAN OR EQUAL TO OPERATOR
• Now run the project and enter 27 into the text box. Click the Check Numbers
button, and you should see the results shown in Figure 4.5.
USING GREATER THAN AND GREATER THAN OR EQUAL TO
• In this example, you see how to use the Greater Than and Greater Than Or Equal
To operators.
• Return to the Code Editor and add two additional If statements in the
btnCheckNumbers_Click event handler, as shown here:
Try
'Get the number from the text box
intNumber = CType(txtValue.Text, Integer)
Catch
Exit Sub
End Try
USING GREATER THAN AND GREATER THAN OR EQUAL TO
• Return to the Code Editor and add two additional If statements in the btnCheckNumbers_Click event handler, as shown here:
'Is intNumber greater than 27?
If intNumber > 27 Then
MessageBox.Show("Is 'intNumber’ greater than 27? Yes!", "If Demo")
Else
MessageBox.Show("Is 'intNumber' greater than 27? No!", "If Demo")
End If
'Is intNumber greater than or equal to 27?
If intNumber >= 27 Then
MessageBox.Show("Is 'intNumber’ greater than or equal to 27? Yes!", "If Demo")
Else
MessageBox.Show("Is 'intNumber' greater than or equal to 27? No!", "If Demo")
End If
End Sub
USING GREATER THAN AND GREATER THAN OR EQUAL TO
• Run the program. This time, enter a value of 99 and click the Check Numbers
button.
• You’ll see two message boxes, one after the other. The first message box indicates
that intNumber is is greater than 27, whereas the second message box indicates
that intNumber is greater than 27.
THE ‘AND’ AND ‘OR’ OPERATORS
• What happens when you need your If statement to test more than one
condition?
• For example, suppose that you want to ensure that intNumber is less than 27 and
greater than 10.
• Or, how about checking that strName is "Wendy" or "Stephanie"?
• You can combine operators used with an If statement with the And and Or
operators, as you do in the next Try It Out.
• Using the Or Operator: Create a new Windows Forms Application called And Or
Demo.
• In the Forms Designer for Form1, add two TextBox controls and a Button control.
• Set the Name properties of the text boxes to txtName1 and txtName2 and the
Name property of the button to btnOrCheck.
USING THE OR OPERATOR
• Set the Text property for txtName1 to Wendy and the Text property for txtName2
to Stephanie.
• Finally, set the Text property for btnOrCheck to Or Check.
• Your completed form should look similar to the one shown in Figure 4.6.
USING THE OR OPERATOR
• Double-click the Or Check button and add the following code to its Click event handler:
Private Sub btnOrCheck_Click(sender As Object, e As EventArgs) Handles btnOrCheck.Click
'Declare variables
Dim strName1 As String, strName2 As String
'Get the names
strName1 = txtName1.Text
strName2 = txtName2.Text
'Is one of the names Wendy?
If strName1 = "Wendy" Or strName2 = "Wendy" Then
MessageBox.Show("One of the names is Wendy.", "And Or Demo")
Else
MessageBox.Show("Neither of the names is Wendy.", "And Or Demo")
End If
USING THE OR OPERATOR
• Run the project and click the button. You should see the results shown in Figure
4.7.
• Click OK to dismiss the message box dialog and flip the names around so that the
top one (txtName1) is Stephanie and the bottom one (txtName2) is Wendy.
• Click the button again. You’ll see a message box indicating that one of the names
is Wendy.
• Click OK to dismiss the message box again and this time change the names so that
neither of them is Wendy.
• Click the button, and you should see a message box indicating that neither of the
names is Wendy.
USING THE AND OPERATOR
• The And operator is conceptually similar to Or, except that both parts of the condition need to be satisfied, as
you will see in the next Try It Out.
• Return to the Forms Designer in the And Or Demo project. Add another Button control to the form. Set its Name
property to btnAndCheck and its Text property to And Check. Double-click the button and add the following
bolded code to its Click event handler:
Private Sub btnAndCheck_Click(sender As Object, e As EventArgs) Handles btnAndCheck.Click
'Declare variables
Dim strName1 As String, strName2 As String
'Get the names
strName1 = txtName1.Text
strName2 = txtName2.Text
'Are both names Wendy?
If strName1 = "Wendy" And strName2 = “Rudy" Then
MessageBox.Show("Both names are Wendy.", "And Or Demo")
Else
MessageBox.Show("One of the names is not Wendy.", "And Or Demo")
End If
USING THE AND OPERATOR
• Run the program and click the And Check button.
• A message box tells you that one of the names is not Wendy.
• Change both names so that they are both Wendy and click the button.
• You’ll see the results shown in Figure 4.8.
MORE ON ‘AND’ AND ‘OR’
• You’ve seen And and Or used with strings.
• They can also be used with numeric values, like this:
If intX = 2 And intY = 3 Then
MessageBox.Show("Hello, both of the conditions have been satisfied!")
End If
• or
If intX = 2 Or intY = 3 Then
MessageBox.Show("Hello, one of the conditions has been satisfied!")
End If
• It’s possible to use parentheses to group operators and look for a value within a range.
• For example, say you want to determine whether the value of intX is between 12 and 20
exclusive or between 22 and 25 exclusive.
• You can use the following If…Then statement:
SUMMARY
TOPIC CONCEPT
Event-Driven Programming This means that the code is executed in response to events are triggered by user
actions. Example of such actions are clicking a mouse, pressing a key on the keyboard,
typing some text into a textbox, etc. Visual Basic 2015 supports event driven
programming. This will be demonstrated at the computer lab.
The If statement The simplest way to make a decision in Visual Basic 2015 is to use the If….Then
The Else Statement Statement. This runs the code if the ‘condition’ is true.
Allowing Multiple Alternatives with If you want to run a piece of code if the ‘condition’ is true and another if the condition is
ElseIf false, then you will also need the Else part. Slides 5 to 15 contains source that
demonstrates how to use the Simple If, Nested If and If …ElseIf.

Comparison Operators The use of If can be combined with comparison operators such as >, <, etc. Slides 18 to
AND, OR Operators 26 demonstrates this concept.
The AND, OR operators can be used to test more than one condition. slides 29 and 31
shows how to do this

You might also like