1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.

Slides:



Advertisements
Similar presentations
The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a.
Advertisements

Control Structures Control structures are used to manage the order in which statements in computer programs will be executed Three different approaches.
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.
An Introduction to Programming with C++ Fifth Edition Chapter 5 The Selection Structure.
2-1 Making Decisions Sample assignment statements PayAmount = Hours * Rate PayAmount = 40*Rate + (Hours – 40)*Rate*1.5.
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
Chapter 4: Control Structures: Selection
Selection in C.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
True or False Unit 3 Lesson 7 Building Blocks of Decision Making With Additions & Modifications by Mr. Dave Clausen True or False.
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
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
Computer Science Selection Structures.
Chapter 3 Making Decisions
Chapter 4: The Selection Structure
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
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.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
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.
Conditionals & boolean operators
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
© 2000 Scott S Albert Selection Structures Structured Programming 256 Chapter 4.
CHAPTER 4: Selection Control Structure. Objectives  Use the relational comparison operators  Learn about AND logic  Learn about OR logic  Make selections.
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.
Conditional Expression One of the most useful tools for processing information in an event procedure is a conditional expression. A conditional expression.
Programming with Microsoft Visual Basic th Edition
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Introduction to branching.
Chapter 5: Making Decisions
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Computer Programming TCP1224 Chapter 5 The Selection Structure.
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
Controlling Program Flow with Decision Structures.
Tutorial 4: The Selection Structure 1 Tutorial 4 The Selection Structure.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 5 Making Decisions.
CONTROL STRUCTURES (SELECTION). PROGRAM COMPONENTS  SEQUENCE  Groups Of Sequential Steps  SELECTION  Making Choices  IF-THEN (one way)  IF-THEN-ELSE.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 4 Making Decisions.
More on the Selection Structure
Sequence, Selection, Iteration The IF Statement
The Selection Structure
Topics The if Statement The if-else Statement Comparing Strings
Control Structures – Selection
Making Decisions in a Program
Topics The if Statement The if-else Statement Comparing Strings
2-1 Making Decisions Sample assignment statements
VB Decisions, Conditions & If
Visual Basic – Decision Statements
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Truth tables Mrs. Palmer.
Chapter 3: Selection Structures: Making Decisions
The Selection Structure
Using C++ Arithmetic Operators and Control Structures
Chapter 3: Selection Structures: Making Decisions
Presentation transcript:

1 Selection Structures

2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount = 40*Rate + (Hours – 40)*Rate*1.5

3 Selection Structure Use to make a decision or comparison and then, based on the result of that decision or comparison, to select one of the paths. The condition must result in either a true (yes) or false (no) answer. If the condition is true, the program performs one set of tasks. If the condition is false, there may or may not be a different set of tasks to perform.

4 Selection Structure IF THEN Flowchart Condition True/Yes Statements False/No Note: The arms branch right and left Also, notice how the main sequence continues down the middle

5 The IF Statement The most common decision structure is the IF statement. A condition is a Boolean expression that evaluates to either true or false. Conditions typically involve one of the six relational operators.

6 Relational Operators = > >= < <= Equal to Greater than Greater than or equal to Less than Less than or equal to Not equal to These operators are evaluated from left to right, and are evaluated after any mathematical operators.

7 Expressions Containing Relational Operators < 5 * 2 5 * 2 is evaluated first, giving is evaluated second, giving < 10 is evaluated last, giving false 7 > 3 * 4 / 2 3 * 4 is evaluated first, giving / 2 is evaluated second, giving 6 7 > 6 is evaluated last, giving true All expressions containing a relational operator will result in either a true or false answer only.

8 Solving the Overtime Problem

9 Nested Selection Structure A nested selection structure is one in which either the true path or the false path includes yet another selection structure. Any of the statements within either the true or false path of one selection structure may be another selection structure.

10 Nested If Flowchart Condition True/YesFalse/No Condition True/YesFalse/No Condition True/YesFalse/No Notice how the main sequence continues down the middle

11 Nested IF Statements

12 Long Distance Billing Problem

13 Logical Operators Used to reverse the condition that follows it. Used to combine two relational conditions together. Both conditions must be true in order for the combined condition to be true. Used to combine two relational conditions together. If either of the conditions is true the combined condition is true. Used to combine two relational conditions together. If conditions are opposite then combined condition is true. Note: logical operators are evaluated after any mathematical and relational operators and have the precedence as listed here which can be altered by ( ) NOT AND OR XOR

14 NOT Operator Truth Table NOT 1=1is FALSE NOT 5<2is TRUE NOT “a”=“b”is TRUE ConditionNOT Condition True False True

15 AND Operator Truth Table 1=1 AND 2=2is TRUE 1=1 AND 2=3 is FALSE “a”=“a” AND 2<4is TRUE 5<3 AND 6<2is FALSE AND First Condition True False True False Second Condition TrueFalse

16 OR Operator Truth Table 1=1 OR 2=2is TRUE 1=1 OR 2=3 is TRUE “a”=“a” OR 2<4is TRUE 5<3 OR 6<2is FALSE OR First Condition True False True Second Condition TrueFalse True False

17 XOR Operator Truth Table 1=1 XOR 2=2is FALSE 1=1 XOR 2=3 is TRUE “a”=“a” XOR 2<4is FALSE 5<3 XOR 6<2is FALSE XOR First Condition True False True Second Condition TrueFalse True False

18 Compound Conditions In Visual Logic, a compound condition consists of two conditions within parentheses joined by a logical operator. (condition) logical operator (condition) Requiring the parentheses around the conditions is syntax requirement of Visual Logic. If you don’t do this you will receive an error or an incorrect result.

19 Compound Conditions

20 Expressions Containing the And Logical Operator 3 > 2 And 6 > 5 3 > 2 is evaluated first, giving true 6 > 5 is evaluated second, giving true true And true is evaluated last, giving true is evaluated first, giving 6 10 < 25 is evaluated second, giving true 6 > 6 is evaluated third, giving false true And false is evaluated last, giving false

21 Expression Containing the Or Logical Operator 8 = 4 * 2 Or 7 < 5 4 * 2 is evaluated first, giving 8 8 = 8 is evaluated second, giving true 7 < 5 is evaluated third, giving false true Or false is evaluated last, giving true All expressions containing a relational operator will result in either a true or false answer only.

22 Example of Logical Operators used in the condition To pass a course, a student must have an average test score of at least 75 and an average project score of at least 35. Write the compound condition using the variables AvgTest and AvgProject. AvgTest >= 75 And AvgProject >= 35

23 Example of Logical Operators used in the condition Only employees with job codes of 34 and 67 will receive a raise. Write the compound condition using the variable Code. Code = 34 Or Code = 67

24 Nested If Example 1 Show a selection structure that assigns a sales tax rate to the Tax variable. The tax rate is determined by the state code stored in the Code variable. Codes of 1 and 3 represent a 4% rate; a code of 2 represents a 5% rate. All other codes represent a 2% rate.

25 Nested If Example 1

26 Select Case Form of the Selection Structure Referred to as the extended selection structure Easier than the nested If to write and understand Typically used when a selection structure has several paths from which to choose one

27 Select Case Flowchart Select Expression DoA DoN Each case can contain multiple instructions. The main sequence continues down the middle. DoB AB N …

28 Select Case Example 1 Write a selection structure that assigns a sales tax rate to the Tax variable. The tax rate is determined by the state code stored in the Code variable. Codes of 1 and 3 represent a 4% rate; a code of 2 represents a 5% rate. All other codes represent a 2% rate.

29 Select Case Example 1 Select Case Code Tax =.04Tax =.05Tax =.02 1, 3 2else

30 Write a program that inputs a number between 1 and 10 and displays the number with the appropriate two- letter ending (e.g., 1st, 2nd, 3rd, 4th, 5th,...).