2 1 Selection If selection construct 08012022 1

  • Slides: 48
Download presentation
2. 1 Selection If selection construct 08/01/2022 1

2. 1 Selection If selection construct 08/01/2022 1

Learning Objectives Describe the If structure and its variations. Use and explain concatenation. n

Learning Objectives Describe the If structure and its variations. Use and explain concatenation. n Explain why we may want to join strings together and how we do it. Describe validation and explain how to do it. 08/01/2022 2

What is selection? A program testing whether a condition is true or false and

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. 08/01/2022 3

Types of Selection in VB Two selection constructs: n n If Select Case 08/01/2022

Types of Selection in VB Two selection constructs: n n If Select Case 08/01/2022 4

The If construct has three variations 1. If …. . Then …. . End

The If construct has three variations 1. If …. . Then …. . End If 08/01/2022 3. If …. . Then …. . Else. If …. Then …. . Else …. . End If 2. If …. . Then …. . Else …. . End If 5

1. If …. . Then …. . End If Dim Age As Integer Age

1. If …. . Then …. . End If Dim Age As Integer Age = Console. Read. Line If Age > 16 Then ‘ Age greater than 16? n Console. Write. Line(“You are old enough to drive. ”) End If 08/01/2022 6

Notes The condition to test is Age > 16. n If it is true

Notes The condition to test is Age > 16. n 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. 08/01/2022 7

Relational / Comparative Operators 08/01/2022 = equal to < less than > more than

Relational / Comparative Operators 08/01/2022 = 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. 8

2. If …. . Then …. . Else …. . End If If Age

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

Notes The Else part of the construct is executed if the boolean condition is

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. 08/01/2022 10

3. If …. . Then …. . Else. If …. . Else …. .

3. If …. . Then …. . Else. If …. . Else …. . End If 08/01/2022 11

If Age > 16 Then ‘ Age greater than 16? n Console. Write. Line(“You

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

Notes There are three routes through this example and two boolean conditions to test.

Notes There are three routes through this example and two boolean conditions to test. For example: n 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 Else. If statements. 08/01/2022 13

Concatenation Joins strings together using the & operator. n e. g. Putting a variable

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

Program 2. 1 a Deciding exam grades Specification: n n Ask the user to

Program 2. 1 a Deciding exam grades Specification: n n 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). 08/01/2022 15

Program 2. 1 a Deciding exam grades Dim Mark As Integer Console. Write. Line("Enter

Program 2. 1 a Deciding exam grades Dim Mark As Integer Console. Write. Line("Enter a mark. ") Mark = Console. Read. Line ‘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? n Console. Write. Line(“Merit”) Else. If Mark >= 40 Then ‘Mark 40 - 59? n Console. Write. Line(“Pass”) Else ‘Mark under 40. n Console. Write. Line(“A mark of “ & Mark & “ is a fail. ”) End If 08/01/2022 16

Program 2. 1 a Deciding exam grades Run the program and test each of

Program 2. 1 a Deciding exam grades Run the program and test each of the three routes through the If construct by entering the following marks: n n n 70 50 30 08/01/2022 17

Validation Checking what the user enters obeys predefined rules. n 08/01/2022 e. g. enters

Validation Checking what the user enters obeys predefined rules. n 08/01/2022 e. g. enters numbers less than …. . 18

Validation: Checking for errors 1 There are 2 ways to form IF constructs to

Validation: Checking for errors 1 There are 2 ways to form IF constructs to check for errors: n Simplistic Method: If Error. Check is True Then n Console. Write. Line(“ Console. Write. Line Suitable Error Message. ”) n ‘Show a suitable error message. n Exit Sub ‘Stop the procedure. End If Without Exit Sub your program will correctly report the error but will continue and crash anyway. …. . Code that you want executed if everything is OK. ……

Validation: Checking for errors 2 n More “professional” or “elegant” method: If Error. Check

Validation: Checking for errors 2 n More “professional” or “elegant” method: If Error. Check is True Then n Console. Write. Line(“ Console. Write. Line Suitable Error Message. ”) n ‘Show a suitable error message. Else n n n …. . Code that you want executed if everything is OK. …… End If If you have multiple error checks then just use a series of Else If’s for them, before the final Else. 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.

Commenting on If Statements For presentations 2. 1 – 2. 4 I will only

Commenting on If Statements For presentations 2. 1 – 2. 4 I will only ask for comments to If statements. Your comments MUST explain: n n 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? What happens if the test is true? What happens if the test is false? 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.

Writing code which is easy to understand: Sensible variable names. Keywords in capitals. n

Writing code which is easy to understand: Sensible variable names. Keywords in capitals. n So that the reader does not have to keep cross referencing with a table of variable names. Comments or Annotation. n To explain the logic of the code. Indentation (this is done automatically by VB for you): n To show the lines of the code that go together. For example: n n Private Sub but. Calculate. Mean … Dim …. Mean = Total / Number. Of. Marks lbl. Mean. Result. Text = Mean. …. . End Sub 22

Given Pseudocode will use the following structure: n IF <condition> THEN n n <statement(s)>

Given Pseudocode will use the following structure: n IF <condition> THEN n n <statement(s)> ENDIF or, including an ‘else’ clause: n IF <condition> THEN n <statement(s)> ELSE n n <statement(s)> ENDIF

Extension Program 2. 1 b Deciding Exam Grades Extend the previous guided “ 2.

Extension Program 2. 1 b Deciding Exam Grades Extend the previous guided “ 2. 1 Deciding exam grades” program so that it does not allow and shows suitable error messages if the mark entered is less than 0 or larger than 100. n Create a different error message for each situation: Mark entered is less than 0. Mark entered is more than 100. n Hint: test for these things first.

Extension “Salesman Bonus” Program 2. 1 c Write a program for a salesman to

Extension “Salesman Bonus” Program 2. 1 c Write a program for a salesman to input the total value of their sales this year and give their bonus: n n >= € 100, 000 then their bonus = € 10, 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. < then 50, 000 then they receive no bonus. Extension: n 08/01/2022 What inputs should be disallowed here? Extend the program to disallow these kind of inputs. 25

Bonus Program – Very poor Style False if Total. Value <0 Elseif Total. Value

Bonus Program – Very poor Style False if Total. Value <0 Elseif Total. Value >= 100000 True False Console. Write. Line("Error") Else. If Total. Value >= 70000 True Console. Write. Line(“Bonus: 10000”) True 1 If / Else. If ‘block’ What’s There is no needthe for the final Else. If problem? False Else. If Total. Value >= 50000 Console. Write. Line(“Bonus: 7000”) True False Else. If Total. Value <= 50000 Console. Write. Line(“Bonus: 4000”) End Sub True Console. Write. Line(“Bonus: 0”)

Bonus Program – Very poor Style If Total. Value < 0 Then Console. Write.

Bonus Program – Very poor Style If Total. Value < 0 Then Console. Write. Line(“Error”) Else. If Total. Value >= 100000 Then Console. Write. Line(“Bonus: 10000”) Else. If Total. Value >= 70000 Then Console. Write. Line(“Bonus: 7000”) Else. If Total. Value >= 50000 Then Console. Write. Line(“Bonus: 10000”) There is no Else. If Total. Value < 50000 Then need for the What’s the problem? final Else. If Console. Write. Line(“Bonus: 0”) End If

Bonus Program – Better but still poor Style False If Total. Value <0 True

Bonus Program – Better but still poor Style False If Total. Value <0 True Console. Write. Line("Error") Elseif Total. Value >= 100000 1 If / Else. If ‘block’ False Elseif Total. Value >= 70000 True Console. Write. Line(“Bonus: 10000”) False Elseif Total. Value >= 50000 True Console. Write. Line(“Bonus: 7000”) The problem is What’s the repeating problem? Console. Write. Line() End Sub False / Else True Console. Write. Line(“Bonus: 4000”) Console. Write. Line(“Bonus: 0”)

If Total. Value < 0 Then Console. Write. Line(“Error”) Else. If Total. Value >=

If Total. Value < 0 Then Console. Write. Line(“Error”) Else. If Total. Value >= 100000 Then Console. Write. Line(“Bonus: 10000”) Else. If Total. Value >= 70000 Then Console. Write. Line(“Bonus: 7000”) Else. If Total. Value >= 50000 Then Console. Write. Line(“Bonus: 10000”) Else Console. Write. Line(“Bonus: 0”) End If Bonus Program – Better but still poor Style What’s The problem is therepeating problem? Console. Write. Line();

If Total. Value <0 Bonus Program – Improved Style but not working correctly False

If Total. Value <0 Bonus Program – Improved Style but not working correctly False Elseif Total. Value >= 100000 True Console. Write. Line("Error") True Bonus = 10000; The problem here is that a What’s bonus is the problem? printed even if there is an error. Console. Write. Line(“Bonus: “ & Bonus) End Sub False Elseif Total. Value >= 70000 True Bonus = 7000; 1 If / Else. If ‘block’ False Elseif Total. Value >= 50000 False / else True Bonus = 4000; Bonus = 0;

If Total. Value < 0 Then Console. Write. Line(“Error”) Else. If Total. Value >=

If Total. Value < 0 Then Console. Write. Line(“Error”) Else. If Total. Value >= 100000 Then Bonus = 10000; Else. If Total. Value >= 70000 Then Bonus = 7000; Else. If Total. Value >= 50000 Then Bonus = 4000; Else Bonus = 0 End If Console. Write. Line(“Bonus: ” & Bonus) Bonus Program – Improved Style but not working correctly The problem here is that a bonus is printed even What’s the problem? if there is an error.

If total. Value <0 True Console. Write. Line("Error") Bonus Program – Good Style &

If total. Value <0 True Console. Write. Line("Error") Bonus Program – Good Style & Working Correctly False / Else If total. Value >= 100000 nested If / Else. If ‘block’ False Else. If total. Value >= 70000 True Bonus = 10000 True Bonus = 7000 False Else. If total. Value >= 50000 False / Else True Bonus = 4000 Console. Write. Line(“Bonus: “ & Bonus) End Sub Bonus = 0

If Total. Value < 0 Then Console. Write. Line(“Error”) Else If Total. Value >=

If Total. Value < 0 Then Console. Write. Line(“Error”) Else If Total. Value >= 100000 Then Bonus = 10000; Else. If Total. Value >= 70000 Then Bonus = 7000; Else. If Total. Value >= 50000 Then Bonus = 4000; Else Bonus = 0 End If Console. Write. Line(“Bonus: ” & Bonus) End If Bonus Program – Good Style & working correctly

Extension Income, Personal Allowance and Tax Rate Program 2. 1 d Please extend program

Extension Income, Personal Allowance and Tax Rate Program 2. 1 d Please extend program 1 g from presentation “ 1 Variables/Identifiers” so that the program gives the right answer if a user enters a Salary less than the Personal Allowance.

Extension “/, DIV or MOD” Program 2. 1 e Extend the “/, DIV or

Extension “/, DIV or MOD” Program 2. 1 e Extend the “/, DIV or MOD” Program written in presentation 1 Variables/Identifiers so that it also calculates: 4. The number of actual boxes needed to pack all the melons (even if one is not full). e. g. 2. 5 boxes really means 3 boxes.

Extension Program “Cricket Match” 2. 1 f A program is to be written to

Extension Program “Cricket Match” 2. 1 f A program is to be written to enter and display the result of a cricket match. The winning team is the one scoring the most runs. The structured English description of the problem is shown here. It assumes the scores are not equal. n n n n INPUT Home. Team. Name INPUT Home. Runs INPUT Away. Team. Name INPUT Away. Runs SUBTRACT Aways. Runs FROM Home. Runs STORE AS Run. Difference CALCULATE the winning team STORE AS Winning. Team. Name OUTPUT Winning. Team. Name and Run. Difference Typical output is shown. Write this program.

Extension “Arithmetic Error” Program 2. 1 g Write a program that will output the

Extension “Arithmetic Error” Program 2. 1 g Write a program that will output the value of the expression: n Area /(Space. Width * Space. Length – Empty. Spaces) What happens if the following values are used? n Space. Width ← 7 n Space. Length ← 4 n Empty. Spaces ← 28 This is called an “arithmetic error”. Add code to stop this situation causing the program to crash. In your comments explain: n n 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? 37

Extension “Even Or Odd” Program 2. 1 h Ask the user to enter a

Extension “Even Or Odd” Program 2. 1 h Ask the user to enter a number and then tell the user if the number is even or odd. 38

Extension “Positive Or Negative” Program 2. 1 i Ask the user to enter a

Extension “Positive Or Negative” Program 2. 1 i Ask the user to enter a number and then tell the user if the number is positive or negative 39

Extension “Maximum” Program 2. 1 j Ask the user to enter at least two

Extension “Maximum” Program 2. 1 j Ask the user to enter at least two numbers, tell the user which number is highest. 40

Extension “Minimum” Program 2. 1 j Ask the user to enter at least two

Extension “Minimum” Program 2. 1 j Ask the user to enter at least two numbers, tell the user which number is lowest. 41

Extension “Divisble” Program 2. 1 k Ask the user the to enter 2 numbers,

Extension “Divisble” Program 2. 1 k Ask the user the to enter 2 numbers, tell the user whether the first number is divisible by the second number without a remainder. n Hint: Use the Mod function. e. g. n 12 Mod 6 = 0 ‘ So no remainder. n 12 Mod 5 = 2 ‘ So there is a remainder. 42

Extension “Week. Day” Program 2. 1 k Ask the user to input a week

Extension “Week. Day” Program 2. 1 k Ask the user to input a week day and then tell the user which week day number this is. n e. g. Monday ‘ Is 1. Tuesday ‘ Is 2. …. Sunday ‘ Is 7. 43

Extension “Month” Program 2. 1 l Ask the user to input a month and

Extension “Month” Program 2. 1 l Ask the user to input a month and then tell the user which month number this is and how many days are in this month. n e. g. January ‘ Is 1 and has 31 days. February ‘ Is 2 and has 28 days but in a leap year it has 29 days. …. See: n https: //www. timeanddate. com/calendar/months/ 44

Plenary Why would we want to join strings together and how do we do

Plenary Why would we want to join strings together and how do we do it? n e. g. Putting a variable in a message: Console. Write. Line(“My name is “ & Name & “. I am “ & Age & “ years old. ”) 08/01/2022 45

Plenary What does an If structure test? n An If structure tests a boolean

Plenary What does an If structure test? n An If structure tests a boolean condition. What happens if this test returns True? n 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)? n Otherwise control passes to optional Else or Else. If statements but ultimately the construct ends with an End If statement. 08/01/2022 46

Plenary What is validation, how is it done and how do we stop errors

Plenary What is validation, how is it done and how do we stop errors like the program attempting to store letters in a number variable? 08/01/2022 47

Validation Checking what the user enters obeys predefined rules. n 08/01/2022 e. g. enters

Validation Checking what the user enters obeys predefined rules. n 08/01/2022 e. g. enters numbers less than …. . 48