Chapter 3: Selection Structures: Making Decisions

Slides:



Advertisements
Similar presentations
An Introduction to Programming with C++ Fifth Edition Chapter 5 The Selection Structure.
Advertisements

Chapter 6 Horstmann Programs that make decisions: the IF command.
2-1 Making Decisions Sample assignment statements PayAmount = Hours * Rate PayAmount = 40*Rate + (Hours – 40)*Rate*1.5.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.
Boolean Logic & Truth Tables In today’s lesson we will look at: a reminder about truth values and NOT, AND, OR and EOR truth tables operator precedence.
Adapted from Discrete Math
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Programming Logic and Design Sixth Edition
Computer Science Selection Structures.
Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks 5.3 Select Case Blocks.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
Chapter 4 Selection Structures: Making Decisions.
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
Logic Disjunction A disjunction is a compound statement formed by combining two simple sentences using the word “OR”. A disjunction is true when at.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
CHAPTER 4: Selection Control Structure. Objectives  Use the relational comparison operators  Learn about AND logic  Learn about OR logic  Make selections.
CPS120: Introduction to Computer Science Operations Lecture 9.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
BACS 287 Programming Logic 2. BACS 287 Sequence Construct The sequence construct is the default execution mode for the CPU. The instructions are executed.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Introduction to branching.
Thinking Mathematically
CSCI N201: Programming Concepts Copyright ©2005  Department of Computer & Information Science Working with Selection Structures.
Assignment statement: Assigns a value to a variable Variable must appear on the left side, value on the right side of the assignment operator Right side.
Program Development C# Programming January 30, 2007 Professor J. Sciame.
Chapter 2 Inequalities. Lesson 2-1 Graphing and Writing Inequalities INEQUALITY – a statement that two quantities are not equal. SOLUTION OF AN INEQUALITY.
Computer Programming TCP1224 Chapter 5 The Selection Structure.
By: Mr. Baha Hanene Chapter 6. LEARNING OUTCOMES This chapter will cover the learning outcome 02 i.e. 2.Use basic data-types and input / output in C programs.
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Chapter 4 Select … Case Multiple-Selection Statement & Logical Operators 1 © by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Random Functions Selection Structure Comparison Operators Logical Operator
Programming Logic and Design Fifth Edition, Comprehensive Chapter 4 Making Decisions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
THE DECISIONS CONTROL STRUCTURE! CHAPTER 2. Transfer of control statement: o The statement of computer program are executed one after the other in the.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
More on the Selection Structure
AND.
Sequence, Selection, Iteration The IF Statement
Assignment statement:
The Selection Structure
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4: Decision Structures and Boolean Logic
An Introduction to Programming with C++ Fifth Edition
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Topics The if Statement The if-else Statement Comparing Strings
Objectives After studying this chapter, you should be able to:
Chapter 4: Decision Structures and Boolean Logic
VB Decisions, Conditions & If
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 5: Control Structure
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Core Objects, Variables, Input, and Output
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Lecture 5 Binary Operation Boolean Logic. Binary Operations Addition Subtraction Multiplication Division.
Truth tables Mrs. Palmer.
Chapter 3: Selection Structures: Making Decisions
Chapter 5: The Selection Structure
Using C++ Arithmetic Operators and Control Structures
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4: Decision Structures and Boolean Logic
ICS103: Programming in C 4: Selection Structures
Decision Making Using the IF and EVALUATE Statements
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Presentation transcript:

Chapter 3: Selection Structures: Making Decisions

Outline Dual Alternative Logical Operators Example: Profit or Loss Pseudocode Flowchart Logical Operators Compond Condition AND, OR & NOT Truth Table

Flowchart for Single and Dual Alternative Selection Structures Figure 3.1 Flowcharts for single- and dual-alternative selection structures

Profit Or loss Example Design a program that calculating the profit of selling a certain product by computing the difference between the revenue and cost. Result should describe the quantity of profit or loss.

Algorithm: Profit or Loss Pseudocode

Flowchart: Profit or Loss

Logical Operators

Logical Operators Logical operators are used to connect simple conditions into a more complex condition called a compound condition. The simple conditions each contain one relational operator. Using compound conditions reduces the amount of code that must be written.

Combining Logical and Relational Operators to Create Compound Conditions This code is equivalent to    Input X If X < 5 Then Write “OK” End If If X > 10 Then this code. But this code is shorter! Input X If (X < 5) OR (X > 10) Then Write “OK” End If

Hints In a compound condition, it is necessary to use complete simple conditions. This is correct: If (X < 5) OR (X > 10) Then … This is not correct: If (X < 5 OR > 10) Then …

If (X > 5) AND (X < 10) Then … The AND Operator A compound condition consisting of two simple conditions joined by an AND is true only if both simple conditions are true. It is false if even one of the conditions is false. The statement: If (X > 5) AND (X < 10) Then … is true only if X is 6, 7, 8, or 9. It has to be both greater than 5 and less than 10 at the same time.

If (Response ==“Y”) OR (Response ==“y”) Then … The OR Operator A compound condition consisting of two simple conditions joined by an OR is true if even one of the simple conditions is true. It is false only if both are false. For example: If (Response ==“Y”) OR (Response ==“y”) Then … This is true if Response is uppercase (‘Y’) or lowercase (‘y’). For the above condition to be false, Response would have to be something other than either ‘Y’ or ‘y’.

If (X > 100) AND NOT(X == Y) Then… The NOT Operator AND and OR affect 2 simple conditions. NOT affects only one condition. If you need to negate more than one simple condition, you will need more than one NOT A condition with the NOT operator is true only if the condition is false. NOT(A < B) is true only if A is greater than or equal to B. If (X > 100) AND NOT(X == Y) Then… is true only if X is greater than 100 but not equal to the value of Y.

Truth Tables for OR, AND, and NOT Operators X Y X OR Y X AND Y NOT X true false

Review Questions Assume X= 10, Y= 20 which of the following sentences are true: NOT (X<Y) X>Y OR X==Y X<Y AND Y!=X X==Y AND X!=Y X>=Y OR X==Y

Hierarchy of Operations Type Operator Order Performed Arithmetic operations are performed first, in order shown ( ) ^ * / % + - 1st parentheses 2nd exponentiation 3rd: multiplication, division, modulus 4th: addition, subtraction Relational operations are performed second == != < <= > >= All relational operators have equal precedence Logical operations are performed last, in the order shown NOT AND OR 1st: NOT (!) 2nd: AND (&&) 3rd: OR (||)

Combining Logical and Relational Operators Example: Let Q = 3 and let R = 5 Is the following expression true or false? NOT Q > 3 OR R < 3 AND Q – R <= 0 Step 1: (NOT(false)) OR (false AND true) Step 2: true OR (false AND true) Step 3: true OR false Step 4: true

Any Questions ?