If selection construct

Slides:



Advertisements
Similar presentations
30/04/ Selection Nested If structures & Complex Multiple Conditions.
Advertisements

ITEC113 Algorithms and Programming Techniques
Conditional Statements Introduction to Computing Science and Programming I.
Computer Science 1620 Programming & Problem Solving.
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Lecture Set 5 Control Structures Part A - Decisions Structures.
1 2.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works.
110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Chapter 5 - VB 2005 by Schneider1 Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks.
Input Textboxes Input Boxes Different than textboxes Good for small amount of input (form full of textboxes is not nice) X = Inputbox(“prompt message”,
26/10/ Selection Nested If structures & Complex Multiple Conditions.
30/10/ Iteration Loops Do While (condition is true) … Loop.
04/11/ Arrays 1D Arrays Defining, Declaring & Processing.
22/11/ Selection If selection construct.
1 Flow Control Ifs, loops. 2 Data Type At the lowest level, all data in a computer is written in 1’s and 0’s (binary) How the data gets interpreted, what.
Chapter 3 Control Structures. The If…Then Statement The If…Then statement is a Decision statement = that executes a set of statements when a condition.
31/01/ Selection If selection construct.
Controlling Program Flow with Decision Structures.
1 4.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works. Directly testing if text boxes.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
© 2006 Lawrenceville Press Slide 1 Chapter 5 The If…Then Statement (One-Way)  Conditional control structure, also called a decision structure  Executes.
Starter What does the following code do?
A variable is a name for a value stored in memory.
COMPUTATIONAL CONSTRUCTS
Chapter 4 The If…Then Statement
Data Types Variables are used in programs to store items of data e.g a name, a high score, an exam mark. The data stored in a variable is entered from.
Line Continuation, Output Formatting, and Decision Structures
VB.Net Programming Console Application
Programming Mehdi Bukhari.
IF statements.
The Selection Structure
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
Control Statement Examples
Line Continuation, Output Formatting, and Decision Structures
Conditional Statements
Conditions and Ifs BIS1523 – Lecture 8.
Algorithm Discovery and Design
Do While (condition is true) … Loop
Coding Concepts (Basics)
4.1 Strings ASCII & Processing Strings with the Functions
If selection construct
Do … Loop Until (condition is true)
Logical Operations In Matlab.
Text / Serial / Sequential Files
Algorithm and Ambiguity
3.1 Iteration Loops For … To … Next 18/01/2019.
Microsoft Visual Basic 2005: Reloaded Second Edition
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Flowcharts and Pseudo Code
Programming In Lesson 4.
Chapter 3: Selection Structures: Making Decisions
Review of Previous Lesson
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Python Selection.
Review of Previous Lesson
Review of Previous Lesson
10.3 Procedures Function Procedures 07/06/2019.
Text / Serial / Sequential Files
3.2 Working with Data Scope of variables 29/07/2019.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Switch Case Structures
Control Structures.
Presentation transcript:

If selection construct 09/12/2018

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 40 - 59? Console.WriteLine(“Pass”) Else ‘Mark under 40. Console.WriteLine(“A mark of “ & Mark & “ is a fail.”) End If 09/12/2018

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

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

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.

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.

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.

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

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

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.

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,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: What inputs should be disallowed here? Extend the program to disallow these kind of inputs. 09/12/2018

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.

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.

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.

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

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

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

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

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