Presentation is loading. Please wait.

Presentation is loading. Please wait.

22/11/20151 2.1 Selection If selection construct.

Similar presentations


Presentation on theme: "22/11/20151 2.1 Selection If selection construct."— Presentation transcript:

1 22/11/20151 2.1 Selection If selection construct

2 222/11/2015 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. Explain why we may want to join strings together and how we do it. Describe validation and explain how to do it.

3 322/11/2015 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 422/11/2015 Types of Selection in VB Two selection constructs: If If Select Case Select Case

5 522/11/2015 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 622/11/2015 Dim Age As Integer Age = Console.ReadLine If Age > 16 Then ‘ Age greater than 16? Console.WriteLine(“You are old enough to drive.”) Console.WriteLine(“You are old enough to drive.”) End If 1. If ….. Then ….. End If

7 722/11/2015 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 822/11/2015 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 922/11/2015 If Age > 16 Then ‘ Age greater than 16? Console.WriteLine(“You are old enough to drive.”) 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.”) Console.WriteLine(“Sorry, you are too young to drive. You must be 17 years old.”) End If 2. If ….. Then ….. Else ….. End If

10 1022/11/2015 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 1122/11/2015 3. If ….. Then ….. ElseIf ….. Else ….. End If

12 1222/11/2015 If Age > 16 Then ‘ Age greater than 16? Console.WriteLine(“You are old enough to drive.”) 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.”) 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.”) Console.WriteLine(“Sorry, you are too young to drive. You must be 17 years old.”) End If

13 1322/11/2015 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 1422/11/2015 Concatenation Joins strings together using the & operator. e.g. Putting a variable in a message: e.g. Putting a variable in a message: Console.WriteLine(“My name is “ & Name & “. I am “ & Age & “ years old.”) Console.WriteLine(“My name is “ & Name & “. I am “ & Age & “ years old.”) Will show My name is …… I am … years old.

15 1522/11/2015 Program 2.1a 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 1622/11/2015 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”) Console.WriteLine(“Merit”) ElseIf Mark >= 40 Then ‘Mark 40 - 59? Console.WriteLine(“Pass”) Console.WriteLine(“Pass”) Else ‘Mark under 40. Console.WriteLine(“A mark of “ & Mark & “ is a fail.”) Console.WriteLine(“A mark of “ & Mark & “ is a fail.”) End If

17 1722/11/2015 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 70 50 50 30 30

18 1822/11/2015Validation Checking what the user enters obeys predefined rules. e.g. enters numbers less than ….. e.g. enters numbers less than …..

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 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 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.2.1 – 2.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? 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. 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. 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. To show the lines of the code that go together. For example: Private Sub butCalculateMean … Private Sub butCalculateMean … Dim …. Mean = Total / NumberOfMarks lblMeanResult.Text = Mean. ….. End Sub End Sub 22

23 Given Pseudocode will use the following structure: IF THEN ENDIF or, including an ‘else’ clause: IF THEN ELSE ENDIF

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 2522/11/2015 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. >= €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.Extension: What inputs should be disallowed here? Extend the program to do disallow these kind of inputs. What inputs should be disallowed here? Extend the program to do disallow these kind of inputs.

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.1 Variables/Identifiers

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:1 Variables/Identifiers 4. 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.

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 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?

30 3022/11/2015 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: Console.WriteLine(“My name is “ & Name & “. I am “ & Age & “ years old.”) Console.WriteLine(“My name is “ & Name & “. I am “ & Age & “ years old.”)

31 3122/11/2015 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.

32 3222/11/2015 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?

33 3322/11/2015Validation Checking what the user enters obeys predefined rules. e.g. enters numbers less than ….. e.g. enters numbers less than …..


Download ppt "22/11/20151 2.1 Selection If selection construct."

Similar presentations


Ads by Google