Selection Logic Building decision-making into programs.

Slides:



Advertisements
Similar presentations
Chapter 4: Control Structures I (Selection)
Advertisements

MONTEGO BAY HIGH SCHOOL INFORMATION TECHNOLOGY THE EXCEL IF FUNCTION.
Programming with Microsoft Visual Basic th Edition
1.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
Decision Structures Chapter 4. Chapter 4 Objectives To understand: o What values can be stored in a Boolean variable o What sequence structures are and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming First Edition.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
Programming with Microsoft Visual Basic th Edition
Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt)
Lesson 5 - Decision Structure By: Dan Lunney
Microsoft Visual Basic: Reloaded Chapter Five More on the Selection Structure.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 9 Decisions, Decisions, Decisions.
Objectives AND logic OR logic Evaluating compound conditions with multiple logical operators Precedence when combining AND and OR operators Efficiency.
An Introduction to Programming with C++ Fifth Edition Chapter 5 The Selection Structure.
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter Four The Selection Structure
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Chapter 4: The Selection Structure
Programming Logic and Design Fourth Edition, Introductory
Programming Logic and Design Sixth Edition
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Five More on the Selection Structure.
Computer Science Selection Structures.
Chapter 3 Making Decisions
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Seven More on the Repetition Structure.
Chapter 4: The Selection Structure
Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Visual C# 2005 Decision Structures. Visual C# Objectives Understand decision making Learn how to make decisions using the if statement Learn how.
Lecture Set 5 Control Structures Part A - Decisions Structures.
Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
Programming Logic and Design, Second Edition, Comprehensive
CHAPTER 3 SELECTION STRUCTURE.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
Conditional Expression One of the most useful tools for processing information in an event procedure is a conditional expression. A conditional expression.
Chapter 5: More on the Selection Structure
Programming with Microsoft Visual Basic th Edition
Python Selection. All the programs you have been developing so far have been sequential, this means that each instruction is executed in a set order.
`. Lecture Overview Structure Programming Basic Control of Structure Programming Selection Logical Operations Iteration Flowchart.
Chapter Four The Selection Structure Programming with Microsoft Visual Basic th Edition.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 5 Making Decisions.
Chapter 10 So Many Paths … So Little Time (Multiple-Alternative Selection Structures) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Conditional statements and boolean expressions Arithmetic, relational and logical operators.
USING CONDITIONAL CODE AMIR KHANZADA. Conditional Statement  Conditional statements are the set of commands used to perform different actions based on.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 4 Making Decisions.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
The Selection Structure
Chapter 4: The Selection Structure
An Introduction to Programming with C++ Fifth Edition
Microsoft Visual Basic 2005 BASICS
Objectives After studying this chapter, you should be able to:
Chapter 4: Decision Structures and Boolean Logic
Chapter 8: More on the Repetition Structure
Visual Basic – Decision Statements
Microsoft Visual Basic 2005: Reloaded Second Edition
More on If statements (Calculate, Calculate1, Calculate2)
Chapter 4: Decision Structures and Boolean Logic
Presentation transcript:

Selection Logic Building decision-making into programs

Sample Problem (v1) Create an application to allow a store clerk to enter: the price of a dozen roses the number of dozens desired Calculate and display the total cost of the order Microsoft Visual Basic 2012: Reloaded, Fifth Edition 2

3 Sample Problem (v1) Figure 4-2: Rosebud Roses application (sequence structure only)

Sample Problem (v1) Microsoft Visual Basic 2012: Reloaded, Fifth Edition 4

Sample Problem (v1) Download and look at code Microsoft Visual Basic 2012: Reloaded, Fifth Edition 5

Sample Problem modified (v2) Create an application to allow a store clerk to enter: the price of a dozen roses the number of dozens desired Customers who order more than 2 dozen roses get a 10% discount Calculate and display the total cost of the order Microsoft Visual Basic 2012: Reloaded, Fifth Edition 6

7 Modified Problem (v2) Figure 4-3: Modified Rosebud Roses application (single-alternative selection structure) Figure 4-4: Single-alternative selection structure shown in a flowchart

Microsoft Visual Basic 2012: Reloaded, Fifth Edition 8 Coding Single-Alternative and Dual- Alternative Selection Structures The If…Then…Else statement is used for coding single- alternative and dual-alternative selection structures The set of statements contained in each path is referred to as a statement block Figure 4-6: How to use the If…Then…Else statement (continues)

Modified Problem (v2) Modify our code as seen in previous slide Microsoft Visual Basic 2012: Reloaded, Fifth Edition 9

Sample Problem modified (v3) Create an application to allow a store clerk to enter: the price of a dozen roses the number of dozens desired All customers get a discount Customers who order more than 2 dozen roses get a 10% discount All other orders get a 5% discount Calculate and display the total cost of the order Microsoft Visual Basic 2012: Reloaded, Fifth Edition 10

Microsoft Visual Basic 2012: Reloaded, Fifth Edition 11 Sample Problem Modified (v3) Figure 4-5: Modified Rosebud Roses application (dual-alternative selection structure)

Microsoft Visual Basic 2012: Reloaded, Fifth Edition 12 Coding Single-Alternative and Dual- Alternative Selection Structures (cont’d.) Figure 4-6: How to use the If…Then…Else statement (continues)

Modified Problem (v3) Modify our code as seen in previous slide Microsoft Visual Basic 2012: Reloaded, Fifth Edition 13

Microsoft Visual Basic 2012: Reloaded, Fifth Edition 14 Comparison (Relational) Operators Each comparison operator (also referred to as relational operators) can be used to compare two values, and the comparison always results in a Boolean value: either True or False. When comparing values, keep in mind that equal to (=) is the opposite of not equal to (<>), greater than (>) is the opposite of less than or equal to ( =) Figure 4-7 How to use comparison operators in a condition (continues) ( in math ≥ ) ( in math ≤ ) ( in math ≠ )

Modified Problem (v4) Instead of 2 discount rate levels, have 3 discount rate levels: 1 or 2 dozen: 5% 3 to 6 dozen: 10% More than 6 dozen: 20% Microsoft Visual Basic 2012: Reloaded, Fifth Edition 15

Modified Problem (v4) flowchart a Microsoft Visual Basic 2012: Reloaded, Fifth Edition 16 Order <= 2 Order <= 6 discountRate =.10 discountRate =.20 discountRate =.05 T T F F

Modified Problem (v4) flowchart b Microsoft Visual Basic 2012: Reloaded, Fifth Edition 17 Order > 6 Order > 2 discountRate =.10 discountRate =.05 discountRate =.20 T T F F

Modified Problem (v4) code b Microsoft Visual Basic 2012: Reloaded, Fifth Edition 18 If ordered > 6 Then discountRate = 0.2 Else If ordered > 2 Then discountRate = 0.1 Else discountRate = 0.05 End If If ordered > 6 Then discountRate = 0.2 Elseif ordered > 2 Then discountRate = 0.1 Else discountRate = 0.05 End If