CHAPTER 4: Selection Control Structure. Objectives  Use the relational comparison operators  Learn about AND logic  Learn about OR logic  Make selections.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Chapter 4 - Control Statements
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Objectives AND logic OR logic Evaluating compound conditions with multiple logical operators Precedence when combining AND and OR operators Efficiency.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
2-1 Making Decisions Sample assignment statements PayAmount = Hours * Rate PayAmount = 40*Rate + (Hours – 40)*Rate*1.5.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.
True or False Unit 3 Lesson 7 Building Blocks of Decision Making With Additions & Modifications by Mr. Dave Clausen True or False.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Programming Logic and Design Fourth Edition, Introductory
Programming Logic and Design Sixth Edition
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Chapter 3 Making Decisions
Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.
Visual C# 2005 Decision Structures. Visual C# Objectives Understand decision making Learn how to make decisions using the if statement Learn how.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
CPS120: Introduction to Computer Science Decision Making in Programs.
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.
Selection Structure If... Then.. Else Case. Selection Structure Use to make a decision or comparison and then, based on the result of that decision or.
Selection Control Structures Simple Program Design Third Edition A Step-by-Step Approach 4.
Programming Logic and Design, Second Edition, Comprehensive
© 2005 Lawrenceville Press Slide 1 Chapter 5 Relational Operators Relational OperatorMeaning =greater than.
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Chapter 5: Making Decisions
Chapter 5 Selection Statements Mr. Dave Clausen La Cañada High School.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
Computer Programming TCP1224 Chapter 5 The Selection Structure.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
CPS120: Introduction to Computer Science Decision Making in Programs.
5.02B Decision Making Structure (part 2). Compound Boolean Expressions.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 5 Making Decisions.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Conditional Control Structures Chapter 5 Review. The If Statement The if statement is a conditional control structure, also called a decision structure,
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Random Functions Selection Structure Comparison Operators Logical Operator
Programming Logic and Design Fifth Edition, Comprehensive Chapter 4 Making Decisions.
Java Programming Fifth Edition
Computer Science 210 Computer Organization
Sequence, Selection, Iteration The IF Statement
Chapter 5 Decisions. Chapter 5 Decisions ssential uestion: How are Boolean expressions or operators used in everyday life?
Topics The if Statement The if-else Statement Comparing Strings
Topics The if Statement The if-else Statement Comparing Strings
2-1 Making Decisions Sample assignment statements
Computers & Programming Languages
Relational Operators Operator Meaning < Less than > Greater than
Computer Science 210 Computer Organization
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Lecture 5 Binary Operation Boolean Logic. Binary Operations Addition Subtraction Multiplication Division.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Chapter 3: Selection Structures: Making Decisions
The Selection Structure
Using C++ Arithmetic Operators and Control Structures
Chapter 3: Selection Structures: Making Decisions
Controlling Program Flow
Control Structures.
Presentation transcript:

CHAPTER 4: Selection Control Structure

Objectives  Use the relational comparison operators  Learn about AND logic  Learn about OR logic  Make selections within ranges  Learn about precedence when combining AND and OR selections  Learn more about the case structure  Use a decision table

Comparisons  Boolean expression  Represents only one of two states  Evaluates to true or false  Every decision in a computer program involves evaluating a Boolean expression  Computer circuitry consists of two-state on- off switches  Represented by 1 or 0  Every computer decision yields a true-false result

Relational Operators  Any logical situation can be expressed with only three types of comparisons: =, >, and < – Operators >= and <= are not necessary, but make code more readable  “Not equal” operator  Most confusing of comparisons  Most likely to be different in different languages

Example

Understanding AND Logic  Compound condition  Asks multiple questions before an outcome is determined AND decision  Requires that both of two tests evaluate to True  Requires a nested decision (nested if)  Using nested if statements – Second selection structure is contained entirely within one side of first structure – else clause paired with last if

Truth table for the AND operator Understanding AND Logic

Combining Decisions in an AND Selection (continued) Figure 4-10 Using an AND operator and the logic behind it

Avoiding Common Errors in an AND Selection  Second decision must be made entirely within the first decision  Range of values – every value between low and high limits  In most programming languages logical AND is a binary operator  Requires complete Boolean expression on both sides

Understanding OR Logic  When you want to take action when one or the other of two conditions is true  Example:  Salespeople get bonus when they have achieved one of two goals: Sell at least five items Sell at least $2,000 in merchandise – itemsSold >= ITEMS_MIN ? If true, assign $300 bonus

Writing OR Decisions for Efficiency  May ask either question first  Both produce the same output, but vary widely in number of questions asked  If first question is true, no need to ask second  In an OR decision first ask the question that is more likely to be true  Eliminates as many repetitions as possible of second decision

Combining Decisions in an OR Selection  Conditional OR operator allows you to ask two or more questions in a single comparison  Only one Boolean expression in an OR selection must be true to produce a result of true  Question placed first will be asked first, so consider efficiency  Computer can ask only one question at a time

Combining Decisions in an OR Selection (continued) Truth table for the OR operator

Combining Decisions in an OR Selection (continued)

Avoiding Common Errors in an OR Selection  Second question must be self-contained structure with one entry and exit point  Request for A and B in English often translates to a request for A or B logically  Example: “Give a bonus to anyone who has sold at least three items and to anyone who has sold $2000” “Give a bonus to anyone who has sold at least three items or $2000”

Understanding Precedence When Combining AND and OR Selections  Combine multiple AND and OR operators in an expression  When multiple conditions must all be true, use multiple AND s if score1 >= 75 AND score2 >= 75 AND score 3 >= 75 then classGrade = “Pass” else classGrade = “Fail” endif

Understanding Precedence When Combining AND and OR Selections  When only one of multiple conditions must be true, use multiple ORs if score1 >= 75 OR score2 >= 75 OR score3 >= 75 then classGrade = “Pass” else classGrade = “Fail” endif

Understanding Precedence When Combining AND and OR Selections  When AND and OR operators are combined in the same statement, AND operators are evaluated first if age = 65 AND rating = “G”  Use parentheses to correct logic and force evaluations to occur in the order desired if (age = 65) AND rating = “G”

Understanding Precedence When Combining AND and OR Selections (continued)  Mixing AND and OR operators makes logic more complicated  Can avoid mixing AND and OR decisions by nesting if statements

Understanding Precedence When Combining AND and OR Selections Nested decisions that determine movie patron discount