© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Slides:



Advertisements
Similar presentations
Chapter 13 Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional.
Advertisements

Nested if-else Statements.  Should be indented to make the logic clear.  Nested statement executed only when the branch it is in is executed. For example,
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Chapter 9 IF Statement Bernard Chen. If Statement The main statement used for selecting from alternative actions based on test results It’s the primary.
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
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 4: 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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Chapter 5 - VB 2005 by Schneider1 Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
© The McGraw-Hill Companies, 2006 Chapter 2 Selection.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Input Validation 10/09/13. Input Validation with if Statements You, the C++ programmer, doing Quality Assurance (by hand!) C++ for Everyone by Cay Horstmann.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Lesson thirteen Conditional Statement "if- else" ©
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
C++ Programming Control Structures I (Selection).
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Chapter 4 - VB 2008 by Schneider1 Chapter 4 – Decisions 4.1 Relational and Logical Operators 4.2 If Blocks 4.3 Select Case Blocks.
Decision making If.. else statement.
Chapter 4 Selections © Copyright 2012 by Pearson Education, Inc. All Rights Reserved.
CNG 140 C Programming (Lecture set 3)
Chapter 5 The if Statement
Chapter 4: Making Decisions.
Operator Overloading; Class string
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
Chapter 4: Decision Structures and Boolean Logic
Chapter 4: Making Decisions.
Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Topics The if Statement The if-else Statement Comparing Strings
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Chapter 3: Introduction to Problem Solving and Control Statements
1) C program development 2) Selection structure
Multiple Selections (ELIF Statements)
Chapter 4: Decision Structures and Boolean Logic
VB Decisions, Conditions & If
Visual Basic – Decision Statements
© 2016 Pearson Education, Ltd. All rights reserved.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Topics The if Statement The if-else Statement Comparing Strings
The if Statement Control structure: logical design that controls order in which set of statements execute Sequence structure: set of statements that execute.
CS2011 Introduction to Programming I Selections (I)
Program Flow.
Selection Structures: Single-alternative
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
© 2012 Elsevier, Inc. All rights reserved.
Section 10.5 The Dot Product
Chapter 3: Selection Structures: Making Decisions
Chapter 4: Decision Structures and Boolean Logic
Chapter 2, Part III Arithmetic Operators and Decision Making
Presentation transcript:

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. Decision Structures Section 2 Chapter 3 © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. if-else Statements Also known as branching structures Allow program to decide on course of action Based on whether a certain condition is true or false. Form: © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. if-else Statements Example 1: Program finds larger of two numbers input by user. © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

if-else Statements FIGURE 3.1 Flowchart for the if-else statement in Example 1. © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. if-else Statements Example 2: if-else statement in program has relational operators in its condition. © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. if Statements The else part of an if-else statement can be omitted When the condition is false Execution continues with line after the if statement block © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. if Statements Example 3: Program contains two if statements © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

if Statements FIGURE 3.2 Flowchart for Example 3. © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

Nested if-else Statements Indented blocks of if-else and if statements can contain other if-else and if statements The if-else statements are said to be nested Consider the task of interpreting a beacon The color of the beacon light atop Boston’s old John Hancock building forecasts the weather © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

Nested if-else Statements Example 4: Program requests a color (Blue or Red) and a mode (Steady or Flashing) as input Then displays the weather forecast © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

Nested if-else Statements Example 4: Program requests a color (Blue or Red) and a mode (Steady or Flashing) as input Then displays the weather forecast © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

Nested if-else Statements Example 5: Program requests costs, revenue for company Displays “Break even”, profit, or loss © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

Nested if-else Statements Example 5: Program requests costs, revenue for company Displays “Break even”, profit, or loss © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. The elif Clause Allows for more than two possible alternatives with inclusion of elif clauses. © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. The elif Clause Example 6: Program reports if the two numbers are equal. © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. The elif Clause Example 7: Program calculates a single employee’s FICA tax withheld. © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. The elif Clause Example 7: Program calculates a single employee’s FICA tax withheld. © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. The elif Clause Example 8: program assumes that the user will graduate and determines if they will graduate with honors. © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

The elif Clause Figure 3.3 Program and Flowchart for Example 8. © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

Input Validation with if-elif-else Statements Example 9: Program uses the method isdigit to guard against improper input. © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

Input Validation with if-elif-else Statements Example 9: Program uses the method isdigit to guard against improper input. © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. True and False Example 10: Program illustrates truth values of objects © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. End Section 2 Chapter 3 © 2016 Pearson Education, Inc., Hoboken, NJ.  All rights reserved.