Presentation is loading. Please wait.

Presentation is loading. Please wait.

31/01/20161 4.1 Selection If selection construct.

Similar presentations


Presentation on theme: "31/01/20161 4.1 Selection If selection construct."— Presentation transcript:

1 31/01/20161 4.1 Selection If selection construct

2 231/01/2016 Learning Objectives Explain why we may want to join strings together and how we do it Describe the If structure and its variations.

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

4 431/01/2016 Types of Selection in VB Two selection constructs: If If Select Case Select Case

5 531/01/2016 The If construct has three variations 1. If ….. Then ….. End If 2. If ….. Then ….. Else ….. End If 3. If ….. Then ….. ElseIf …. Then ….. Else ….. End If

6 631/01/2016 Dim Age As Integer Age = txtAge.Text If Age > 16 Then ‘ Age greater than 16? MsgBox (“You are old enough to drive.”) MsgBox (“You are old enough to drive.”) End If 1. If ….. Then ….. End If

7 731/01/2016 Notes The condition to test is Age > 16. If it is true the message is shown, and if false the message is skipped. If it is true the message is shown, and if false the message is skipped. Because the condition is either true or false it is called a boolean condition (Boolean is a data type). Any If statement must always have a matching End If to tell VB where the construct ends. There are two routes through this example and one condition to test.

8 831/01/2016 Relational / Comparative Operators = equal to < less than > more than <= smaller than or equal to >= greater than or equal to <> not equal to These relational/comparative operators return value true or false to the program.

9 931/01/2016 If Age > 16 Then ‘ Age greater than 16? MsgBox (“You are old enough to drive.”) MsgBox (“You are old enough to drive.”) Else ‘ Age 16 or less. MsgBox (“Sorry, you are too young to drive. You must be 17 years old.”) MsgBox (“Sorry, you are too young to drive. You must be 17 years old.”) End If 2. If ….. Then ….. Else ….. End If

10 1031/01/2016 Notes The Else part of the construct is executed if the boolean condition is false. There are two routes through this example and one condition to test.

11 1131/01/2016 3. If ….. Then ….. ElseIf ….. Else ….. End If

12 1231/01/2016 If Age > 16 Then ‘ Age greater than 16? MsgBox (“You are old enough to drive.”) MsgBox (“You are old enough to drive.”) ElseIf Age = 16 Then ‘ Age 16 exactly? MsgBox (“Sorry, you are too young to drive. You only have to wait less than a year though.”) MsgBox (“Sorry, you are too young to drive. You only have to wait less than a year though.”) Else ‘ Age 15 or less. MsgBox (“Sorry, you are too young to drive. You must be 17 years old.”) MsgBox (“Sorry, you are too young to drive. You must be 17 years old.”) End If

13 1331/01/2016 Notes There are three routes through this example and two boolean conditions to test. For example: If Age is 16: If Age is 16: The first condition Age > 16 is false. The second one, Age = 16, is tested, and since it is true the next two lines of code are executed. The Else part would be skipped. More routes are possible if you use more ElseIf statements.

14 1431/01/2016 Concatenation Joins strings together using the & operator. e.g. Putting a variable in a message: e.g. Putting a variable in a message: MsgBox (“My name is “ & Name & “. I am “ & Age & “ years old.”) MsgBox (“My name is “ & Name & “. I am “ & Age & “ years old.”) Will show My name is …… I am … years old.

15 1531/01/2016 Program 4.1 Deciding exam grades Specification: Ask the user to enter an exam mark from 0 to 100. Ask the user to enter an exam mark from 0 to 100. Display the grade it represents – Merit (60 or more), Pass (40 – 59), Fail (under 40). Display the grade it represents – Merit (60 or more), Pass (40 – 59), Fail (under 40).

16 1631/01/2016 Program 4.1 Deciding exam grades Create a new project named ‘Grades’. Change the form’s Text property to ‘Deciding exam grades’. txtMark butMark

17 1731/01/2016 Program 4.1 Deciding exam grades Double click butEnter and enter the following code in its code template: Dim Mark As Integer Dim Mark As Integer Mark = txtMark.Text Mark = txtMark.Text ‘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 before the Message boxes because we have to decide which one to display. ‘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 before the Message boxes because we have to decide which one to display. If Mark >=60 Then ‘Mark 60 or more? If Mark >=60 Then ‘Mark 60 or more? MsgBox (“Merit”) ElseIf Mark >= 40 Then ‘Mark 40 - 59? ElseIf Mark >= 40 Then ‘Mark 40 - 59? MsgBox (“Pass”) Else ‘Mark under 40. Else ‘Mark under 40. MsgBox (“A mark of “ & Mark & “ is a fail.”) End If End If

18 1831/01/2016 Program 4.1 Deciding exam grades Run the program and test each of the three routes through the If construct by entering the following marks: 70 70 50 50 30 30

19 Commenting on If Statements From presentations 4.1 – 4.4 I will only ask for comments to If statements.4.14.4 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.

20 2031/01/2016 Extension “Salesman Bonus” Program 1 Write a program for a salesman to input the total value of their sales this year and 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 = €7,000. From €70,000 to €99,999.99 then their bonus = €7,000. From €50,000 to €69,999.99 then their bonus = €4,000. From €50,000 to €69,999.99 then their bonus = €4,000. < then 50,000 then they receive no bonus. < then 50,000 then they receive no bonus.

21 Checking for errors1 There 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 Exit Sub your program will correctly report the error but will continue and crash anyway.

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

23 23 Extension “Arithmetic Error” Program 2 Write a program that will output the value of the 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?

24 2431/01/2016 Plenary Why would we want to join strings together 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 “ & Name & “. I am “ & Age & “ years old.”) MsgBox (“My name is “ & Name & “. I am “ & Age & “ years old.”)

25 2531/01/2016 Plenary What does an If structure test? An If structure tests a boolean condition. An If structure tests a boolean condition. What happens if this test returns True? If this test returns True then certain lines of code are executed. If this test returns True then certain lines of code are executed. What happens if this test returns False (remember to mention the variations of the If structure) ? Otherwise control passes to optional Else or ElseIf statements but ultimately the construct ends with an End If statement. Otherwise control passes to optional Else or ElseIf statements but ultimately the construct ends with an End If statement.


Download ppt "31/01/20161 4.1 Selection If selection construct."

Similar presentations


Ads by Google