31/01/20161 4.1 selection if selection construct

25
28/06/22 28/06/22 1 4.1 Selection 4.1 Selection If If selection construct selection construct

Upload: claribel-hampton

Post on 17-Jan-2018

232 views

Category:

Documents


0 download

DESCRIPTION

331/01/2016 What is selection? A program testing whether a condition is true or false and - depending on the answer - deciding to execute or not to execute one or more lines of code.

TRANSCRIPT

Page 1: 31/01/20161 4.1 Selection If selection construct

03/05/2303/05/23 11

4.1 Selection4.1 SelectionIfIf selection construct selection construct

Page 2: 31/01/20161 4.1 Selection If selection construct

2203/05/2303/05/23

Learning ObjectivesLearning Objectives

Explain why we may want to join strings Explain why we may want to join strings together and how we do it together and how we do it Describe the If structure and its variations.Describe the If structure and its variations.

Page 3: 31/01/20161 4.1 Selection If selection construct

3303/05/2303/05/23

What is selection?What is selection?

A program testing whether a condition is A program testing whether a condition is true or false and - depending on the true or false and - depending on the answer - deciding to execute or not to answer - deciding to execute or not to execute one or more lines of code.execute one or more lines of code.

Page 4: 31/01/20161 4.1 Selection If selection construct

4403/05/2303/05/23

Types of Selection in VBTypes of Selection in VB

Two selection constructs:Two selection constructs: IfIf Select CaseSelect Case

Page 5: 31/01/20161 4.1 Selection If selection construct

5505/03/2305/03/23

The The IfIf construct has three construct has three variationsvariations

1.1. If ….. Then If ….. Then …..…..End IfEnd If

2.2. If ….. Then If ….. Then ….. …..

ElseElse…..…..

End IfEnd If3.3. If ….. Then If ….. Then

….. ….. ElseIf …. ThenElseIf …. Then

……....ElseElse

…..…..End IfEnd If

Page 6: 31/01/20161 4.1 Selection If selection construct

6605/03/2305/03/23

Dim Age As IntegerDim Age As IntegerAge = txtAge.TextAge = txtAge.TextIf Age > 16 Then If Age > 16 Then ‘ Age greater than 16?‘ Age greater than 16? MsgBox (“You are old enough to drive.”)MsgBox (“You are old enough to drive.”)

End IfEnd If

1.1. If ….. Then If ….. Then …..…..End IfEnd If

Page 7: 31/01/20161 4.1 Selection If selection construct

7705/03/2305/03/23

NotesNotes

The condition to test is Age > 16. The condition to test is Age > 16. If it is true the message is shown, and if false the If it is true the message is shown, and if false the

message is skipped.message is skipped.

Because the condition is either true or false it is Because the condition is either true or false it is called a called a booleanboolean condition condition (Boolean is a data type)(Boolean is a data type)..Any Any IfIf statement must always have a matching statement must always have a matching End IfEnd If to tell VB where the construct ends. to tell VB where the construct ends.There are two routes through this example and There are two routes through this example and one condition to test.one condition to test.

Page 8: 31/01/20161 4.1 Selection If selection construct

8805/03/2305/03/23

Relational / Comparative OperatorsRelational / Comparative Operators

== equal toequal to

<< less thanless than

>> more thanmore than

<=<= smaller than or equal tosmaller than or equal to

>=>= greater than or equal togreater than or equal to

<><> not equal tonot equal to

These relational/comparative operators return value true or false to the program.

Page 9: 31/01/20161 4.1 Selection If selection construct

9905/03/2305/03/23

If Age > 16 Then If Age > 16 Then ‘ Age greater than 16?‘ Age greater than 16? MsgBox (“You are old enough to drive.”)MsgBox (“You are old enough to drive.”)

Else Else ‘ Age 16 or less.‘ Age 16 or less. MsgBox (“Sorry, you are too young to drive. MsgBox (“Sorry, you are too young to drive.

You must be 17 years old.”)You must be 17 years old.”)

End IfEnd If

2.2. If ….. Then If ….. Then ….. ….. ElseElse…..…..End IfEnd If

Page 10: 31/01/20161 4.1 Selection If selection construct

101005/03/2305/03/23

NotesNotes

The The ElseElse part of the construct is executed part of the construct is executed if the boolean condition is false.if the boolean condition is false.There are two routes through this example There are two routes through this example and one condition to test.and one condition to test.

Page 11: 31/01/20161 4.1 Selection If selection construct

111105/03/2305/03/23

3.3. If ….. Then If ….. Then ….. ….. ElseIfElseIf…..…..ElseElse…..…..End IfEnd If

Page 12: 31/01/20161 4.1 Selection If selection construct

121205/03/2305/03/23

If Age > 16 Then If Age > 16 Then ‘ Age greater than 16?‘ Age greater than 16? MsgBox (“You are old enough to drive.”)MsgBox (“You are old enough to drive.”)

ElseIf Age = 16 Then ElseIf Age = 16 Then ‘ Age 16 exactly?‘ Age 16 exactly? MsgBox (“Sorry, you are too young to drive. MsgBox (“Sorry, you are too young to drive.

You only have to wait less than a year though.”)You only have to wait less than a year though.”)

Else Else ‘ Age 15 or less.‘ Age 15 or less. MsgBox (“Sorry, you are too young to drive. MsgBox (“Sorry, you are too young to drive.

You must be 17 years old.”)You must be 17 years old.”)

End IfEnd If

Page 13: 31/01/20161 4.1 Selection If selection construct

131305/03/2305/03/23

NotesNotes

There are three routes through this example There are three routes through this example and two boolean conditions to test.and two boolean conditions to test.For example:For example: If Age is 16:If Age is 16:

The first condition The first condition Age Age > 16 is false.> 16 is false.The second one, The second one, Age Age = 16, is tested, and since it is = 16, is tested, and since it is true the next two lines of code are executed.true the next two lines of code are executed.The Else part would be skipped.The Else part would be skipped.

More routes are possible if you use more More routes are possible if you use more ElseIf statements.ElseIf statements.

Page 14: 31/01/20161 4.1 Selection If selection construct

141405/03/2305/03/23

ConcatenationConcatenation

Joins strings together using the & Joins strings together using the & operator.operator. e.g. Putting a variable in a message:e.g. Putting a variable in a message:

MsgBox (“My name is “ MsgBox (“My name is “ && Name Name && “. I am “ “. I am “ && Age Age && “ years old.”) “ years old.”)Will show Will show My name is …… I am … years old.My name is …… I am … years old.

Page 15: 31/01/20161 4.1 Selection If selection construct

151505/03/2305/03/23

Program 4.1 Deciding exam gradesProgram 4.1 Deciding exam grades

SpecificationSpecification:: Ask the user to enter an exam mark from 0 to Ask the user to enter an exam mark from 0 to

100.100. Display the grade it represents – Merit (60 or Display the grade it represents – Merit (60 or

more), Pass (40 – 59), Fail (under 40).more), Pass (40 – 59), Fail (under 40).

Page 16: 31/01/20161 4.1 Selection If selection construct

161605/03/2305/03/23

Program 4.1 Deciding exam gradesProgram 4.1 Deciding exam grades

Create a new project named ‘Grades’.Create a new project named ‘Grades’.Change the form’s Change the form’s TextText property to property to ‘Deciding exam grades’.‘Deciding exam grades’.

txtMarktxtMark

butMarkbutMark

Page 17: 31/01/20161 4.1 Selection If selection construct

171705/03/2305/03/23

Program 4.1 Deciding exam gradesProgram 4.1 Deciding exam grades

Double click butEnter and enter the following code in its Double click butEnter and enter the following code in its code template:code template:

Dim Mark As IntegerDim Mark As Integer Mark = txtMark.TextMark = txtMark.Text ‘‘The following If statement is after the declaring and storing lines The following If statement is after the declaring and storing lines

because the Mark has to be stored before we can test it; it is because the Mark has to be stored before we can test it; it is before the Message boxes because we have to decide which before the Message boxes because we have to decide which one to display.one to display.

If Mark >=60 Then If Mark >=60 Then ‘Mark 60 or more?‘Mark 60 or more?MsgBox (“Merit”)MsgBox (“Merit”)

ElseIf Mark >= 40 Then ElseIf Mark >= 40 Then ‘Mark 40 - 59?‘Mark 40 - 59?MsgBox (“Pass”)MsgBox (“Pass”)

Else Else ‘Mark under 40.‘Mark under 40.MsgBox (“A mark of “ & Mark & “ is a fail.”)MsgBox (“A mark of “ & Mark & “ is a fail.”)

End IfEnd If

Page 18: 31/01/20161 4.1 Selection If selection construct

181805/03/2305/03/23

Program 4.1 Deciding exam gradesProgram 4.1 Deciding exam grades

Run the program and test each of the Run the program and test each of the three routes through the three routes through the IfIf construct by construct by entering the following marks:entering the following marks: 7070 5050 3030

Page 19: 31/01/20161 4.1 Selection If selection construct

Commenting on If StatementsFrom presentations 4.1 – 4.4 I will only ask for comments to If statements.Your comments MUST explain:

What are you testing? Why are you testing for this? When (after and before what) are you testing for this and why

does it have to be there?When in the procedure code or, if it is on its own, in which procedure (button, checkbox, textbox, etc…)?

What happens if the test is true?

Note that you may answer all these questions in one long comment either before or after the If statement you are commenting on; or you can answer each question with a separate comment. It is up to you.

Page 20: 31/01/20161 4.1 Selection If selection construct

202005/03/2305/03/23

Extension “Salesman Bonus” Program 1Extension “Salesman Bonus” Program 1

Write a program for a salesman to input Write a program for a salesman to input the total value of their sales this year and the total value of their sales this year and give their bonus:give their bonus: >= >= €100,000 then their bonus = €10,000.€100,000 then their bonus = €10,000. From €70,000 to €99,999.99 then their bonus From €70,000 to €99,999.99 then their bonus

= €7,000.= €7,000. From €50,000 to €69,999.99 then their bonus From €50,000 to €69,999.99 then their bonus

= €4,000.= €4,000. < then 50,000 then they receive no bonus.< then 50,000 then they receive no bonus.

Page 21: 31/01/20161 4.1 Selection If selection construct

Checking for errors1There are 2 ways to form IF constructs to check for errors: Simplistic Method:

If ErrorCheck is True Then MsgBox (“Suitable Error Message.”) lblLabelResult.Text = “” ‘Clear the “result” label. Exit Sub ‘Stop the procedure.

End If…..Code that you want executed if everything is OK.……

Without Without Exit SubExit Sub your your program will correctly program will correctly report the error but will report the error but will continue and crash continue and crash anyway. anyway.

Page 22: 31/01/20161 4.1 Selection If selection construct

Checking for errors2 More “professional” or “elegant” method:

If ErrorCheck is True Then MsgBox (“Suitable Error Message.”) lblLabelResult.Text = “” ‘Clear the “result” label.

Else ….. Code that you want executed if everything is OK. ……

End If

It doesn’t really matter which way you actually choose but you should attempt the more “elegant” method or at least be able to understand it, as this is the way it will probably be given to you in exams.

Page 23: 31/01/20161 4.1 Selection If selection construct

2323

Extension “Arithmetic Error” Program 2Extension “Arithmetic Error” Program 2

Write a program that will output the value of the Write a program that will output the value of the expression:expression:

Area /(SpaceWidth * SpaceLength – EmptySpaces)What happens if the following values are used?

SpaceWidth ← 7 SpaceLength ← 4 EmptySpaces ← 28

This is called an “arithmetic error”.Add code to stop this situation causing the program to crash.In your comments explain:

When (after or before what) did you check for the arithmetic error? Why did you check for it there? What you are checking for? What happens if your check is positive/true?

Page 24: 31/01/20161 4.1 Selection If selection construct

242405/03/2305/03/23

PlenaryPlenary

Why would we want to join strings together Why would we want to join strings together and how do we do it?and how do we do it? e.g. Putting a variable in a message:e.g. Putting a variable in a message:

MsgBox (“My name is “ MsgBox (“My name is “ && Name Name && “. I am “ “. I am “ && Age Age && “ years old.”) “ years old.”)

Page 25: 31/01/20161 4.1 Selection If selection construct

252505/03/2305/03/23

PlenaryPlenary

What does an What does an IfIf structure test? structure test? An If structure tests a boolean condition.An If structure tests a boolean condition.

What happens if this test returns What happens if this test returns TrueTrue?? If this test returns True then certain lines of code are If this test returns True then certain lines of code are

executed.executed.

What happens if this test returns What happens if this test returns FalseFalse (remember to mention the variations of the(remember to mention the variations of the IfIf structure)structure)?? Otherwise control passes to optional Otherwise control passes to optional ElseElse or or ElseIfElseIf

statements but ultimately the construct ends with an statements but ultimately the construct ends with an End If statement.End If statement.