Presentation is loading. Please wait.

Presentation is loading. Please wait.

If selection construct

Similar presentations


Presentation on theme: "If selection construct"— Presentation transcript:

1 If selection construct
09/12/2018

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

3 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. 09/12/2018

4 Types of Selection in VB
Two selection constructs: If Select Case 09/12/2018

5 The If construct has three variations
2. If ….. Then ….. Else ….. End If 1. If ….. Then ….. End If 3. If ….. Then ….. ElseIf …. Then ….. Else ….. End If 09/12/2018

6 If Age > 16 Then ‘ Age greater than 16? End If
1. If ….. Then ….. End If Dim Age As Integer Age = Console.ReadLine If Age > 16 Then ‘ Age greater than 16? Console.WriteLine(“You are old enough to drive.”) End If 09/12/2018

7 Notes The condition to test is Age > 16.
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. 09/12/2018

8 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. 09/12/2018

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

10 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. 09/12/2018

11 3. If ….. Then ….. ElseIf ….. Else ….. End If
09/12/2018

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

13 Notes There are three routes through this example and two boolean conditions to test. For example: 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. 09/12/2018

14 Concatenation Joins strings together using the & operator.
e.g. Putting a variable in a message: Console.WriteLine(“My name is “ & Name & “. I am “ & Age & “ years old.”) Will show My name is …… I am … years old. 09/12/2018

15 Program 2.1a Deciding exam grades
Specification: 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). 09/12/2018

16 Program 2.1a Deciding exam grades
Dim Mark As Integer Console.WriteLine("Enter a mark.") Mark = Console.ReadLine ‘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? Console.WriteLine(“Merit”) ElseIf Mark >= 40 Then ‘Mark ? Console.WriteLine(“Pass”) Else ‘Mark under 40. Console.WriteLine(“A mark of “ & Mark & “ is a fail.”) End If 09/12/2018

17 Program 2.1a Deciding exam grades
Run the program and test each of the three routes through the If construct by entering the following marks: 70 50 30 09/12/2018

18 Validation Checking what the user enters obeys predefined rules.
e.g. enters numbers less than …. . 09/12/2018

19 Validation: Checking for errors1
There are 2 ways to form IF constructs to check for errors: Simplistic Method: If ErrorCheck is True Then Console.WriteLine(“Suitable Error Message.”) ‘Show a suitable error message. 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.

20 Validation: Checking for errors2
More “professional” or “elegant” method: If ErrorCheck is True Then Console.WriteLine(“Suitable Error Message.”) ‘Show a suitable error message. Else ….. 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.

21 Commenting on If Statements
For presentations 2.1 – 2.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? 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.

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

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

24 Extension Program 2.1b 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. Create a different error message for each situation: Mark entered is less than 0. Mark entered is more than 100. Hint: test for these things first.

25 Extension “Salesman Bonus” Program 2.1c
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. From €70,000 to €99, then their bonus = €7,000. From €50,000 to €69, then their bonus = €4,000. < then 50,000 then they receive no bonus. Extension: What inputs should be disallowed here? Extend the program to disallow these kind of inputs. 09/12/2018

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

27 Extension “/, DIV or MOD” Program 2.1e
Extend the “/, DIV or MOD” Program written in presentation 1 Variables/Identifiers so that it also calculates: 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.

28 Extension Program “Cricket Match” 2.1f
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. INPUT HomeTeamName INPUT HomeRuns INPUT AwayTeamName INPUT AwayRuns SUBTRACT AwaysRuns FROM HomeRuns STORE AS RunDifference CALCULATE the winning team STORE AS WinningTeamName OUTPUT WinningTeamName and RunDifference Typical output is shown. Write this program.

29 Extension “Arithmetic Error” Program 2.1g
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? 29

30 Plenary Why would we want to join strings together and how do we do it? e.g. Putting a variable in a message: Console.WriteLine(“My name is “ & Name & “. I am “ & Age & “ years old.”) 09/12/2018

31 Plenary What does an If structure test?
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. 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. 09/12/2018

32 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? 09/12/2018

33 Validation Checking what the user enters obeys predefined rules.
e.g. enters numbers less than …. . 09/12/2018


Download ppt "If selection construct"

Similar presentations


Ads by Google