22/11/20151 2.1 Selection If selection construct.

Slides:



Advertisements
Similar presentations
5.04 Apply Decision Making Structures
Advertisements

30/04/ Selection Nested If structures & Complex Multiple Conditions.
Programming TBE 540 Farah Fisher. Objectives After viewing this presentation, the learner will be able to… Given a task, create pseudocode Given pseudocode,
 Control structures  Algorithm & flowchart  If statements  While statements.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
ITEC113 Algorithms and Programming Techniques
Conditional Statements Introduction to Computing Science and Programming I.
Computer Science 1620 Programming & Problem Solving.
Control Structures: Getting Started Sequence and Selection also arithmetic operators, data types, logical operators.
PROGRAMMING, ALGORITHMS AND FLOWCHARTS
Chapter 4 The If…Then Statement
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
Chapter 2 - Algorithms and Design
Mastering Char to ASCII AND DOING MORE RELATED STRING MANIPULATION Why VB.Net ?  The Language resembles Pseudocode - good for teaching and learning fundamentals.
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.
Scratch Programming Lesson 4 Question asking and answering.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
04/11/ Arrays 1D Arrays Defining, Declaring & Processing.
BACS 287 Programming Logic 2. BACS 287 Sequence Construct The sequence construct is the default execution mode for the CPU. The instructions are executed.
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.
1 CS105 Discussion 5 – Variables and If Announcements MP 1 due on Monday Midterm 1 on Tuesday If you need a conflict, request it NOW!!
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.
Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the keyword loop and ends with end loop. This will create.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
Iteration. Iteration: Review  If you wanted to display all the numbers from 1 to 1000, you wouldn’t want to do this, would you? Start display 1 display.
31/01/ Selection If selection construct.
Variables Continued In the last session we saw how variables are objects that allow us to store values in the RAM of the computer In this session we shall.
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
110 F-1 Decisions and Conditions Chapter 4: We can design a form We can calculate To make our programs more powerful, we need to be able to make choices.
05/02/ Records. 205/02/2016 Learning Objectives State: The difference between records and arrays. The difference between records and arrays. How.
Controlling Program Flow with Decision Structures.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
02/03/ Strings Left, Right and Trim. 202/03/2016 Learning Objectives Explain what the Left, Right and Trim functions do.
AVCE ICT – Unit 7 - Programming Session 12 - Debugging.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
1 4.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works. Directly testing if text boxes.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
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.
Computer Science Up Down Controls, Decisions and Random Numbers.
Slide 1 Chapter 4 The If…Then Statement  Conditional control structure, also called a decision structure  Executes a set of statements when a condition.
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 © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
COMPUTATIONAL CONSTRUCTS
VB.Net Programming Console Application
IF statements.
Control Statement Examples
Conditions and Ifs BIS1523 – Lecture 8.
If selection construct
Do While (condition is true) … Loop
Coding Concepts (Basics)
If selection construct
Do … Loop Until (condition is true)
Logical Operations In Matlab.
3.1 Iteration Loops For … To … Next 18/01/2019.
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
Programming In Lesson 4.
Chapter 3: Selection Structures: Making Decisions
Review of Previous Lesson
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

22/11/ Selection If selection construct

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.

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.

422/11/2015 Types of Selection in VB Two selection constructs: If If Select Case Select Case

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

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

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.

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.

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

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.

1122/11/ If ….. Then ….. ElseIf ….. Else ….. End If

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

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.

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.

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

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

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:

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

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.

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.

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.

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

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

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.

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, then their bonus = €7,000. From €70,000 to €99, then their bonus = €7,000. From €50,000 to €69, then their bonus = €4,000. From €50,000 to €69, 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.

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

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.

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?

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.”)

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.

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?

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