(5-1) Selection Structures III in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 21, 2015) Washington State University.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
(10-1) OOP: Inheritance in C++ D & D Chapter 20 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 4 (Conditional Statements) © CPCS
Arrays H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 13, 2011) Washington State University.
Recursion H&K Chapter 10 Instructor – Gokcen Cilingir Cpt S 121 (July 21, 2011) Washington State University.
Modular Programming (2) H&K Chapter 6 Instructor – Gokcen Cilingir Cpt S 121 (July 8, 2011) Washington State University.
Strings (II) H&K Chapter 9 Instructor – Gokcen Cilingir Cpt S 121 (July 20, 2011) Washington State University.
Recursion (II) H&K Chapter 10 Instructor – Gokcen Cilingir Cpt S 121 (July 25, 2011) Washington State University.
Structs H&K Chapter 11 Instructor – Gokcen Cilingir Cpt S 121 (July 18, 2011) Washington State University.
Iteration in C H&K Chapter 5 Instructor – Gokcen Cilingir Cpt S 121 (July 1, 2011) Washington State University.
Selection structures – logical expressions and if statements H&K Chapter 4 Instructor – Gokcen Cilingir Cpt S 121 (June 28, 2011) Washington State University.
Arrays (III) H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 15, 2011) Washington State University.
Data Types H&K Chapter 7 Instructor – Gokcen Cilingir Cpt S 121 (July 12, 2011) Washington State University.
Selection structures in C (II) H&K Chapter 4 Instructor – Gokcen Cilingir Cpt S 121 (June 30, 2011) Washington State University.
Arrays (II) H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 14, 2011) Washington State University.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
Multi-alternative Selection Both the if clause and the else clause of an if...else statement can contain any kind of statement, including another selection.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
(1 - 2) Abstract Data Types D & D Chapter 12 Instructor - Andrew S. O’Fallon CptS 122 (June 10, 2015) Washington State University.
C++ for Engineers and Scientists Third Edition
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
(1 - 2) C Language Elements H&K Chapter 2 Instructor - Andrew S. O’Fallon CptS 121 (August 28, 2015) Washington State University.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Chapter 4 Selection Structures: if and switch Statements Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information.
(4-2) Selection Structures in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 16, 2015) Washington State University.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 9 – Income Tax Calculator Application: Introducing.
Flow of Control Part 1: Selection
(2-2) Functions I H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (Spetember 9, 2015) Washington State University.
Chapter 05 (Part III) Control Statements: Part II.
The switch Statement.  Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each.
CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
(4-3) Selection Structures II in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 18, 2015) Washington State University.
COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.
(9-1) Strings I H&K Chapter 8 Instructor - Andrew S. O’Fallon CptS 121 (October 19, 2015) Washington State University.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
(6-3) Modular Programming H&K Chapter 6 Instructor - Andrew S. O’Fallon CptS 121 (October 2, 2015) Washington State University.
(13-1) Exception Handling in C++ D & D Chapter 17 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
(3-1) Functions II H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (September 9, 2015) Washington State University.
(3-2) File Processing with Functions Instructor - Andrew S. O’Fallon CptS 121 (September 11, 2015) Washington State University.
Operator Overloading D & D Chapter 10 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
(7-2) Arrays I H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (October 9, 2015) Washington State University.
Decision Statements, Short- Circuit Evaluation, Errors.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Streams and File Processing in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Operator Overloading D & D Chapter 10 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
(6-2) Iteration in C II H&K Chapter 5 Instructor - Andrew S. O’Fallon CptS 121 (February 19, 2016) Washington State University.
Data Types H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (March 4, 2016) Washington State University.
Classes: A Deeper Look D & D Chapter 9 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
(1-3) Basics of a Linked List I Instructor - Andrew S. O’Fallon CptS 122 (June 9, 2016) Washington State University.
Discussion 4 eecs 183 Hannah Westra.
Chapter 4 (Conditional Statements)
(2-1) Data Structures & The Basics of a Linked List I
(2-1) Data Structures & The Basics of a Linked List I
The Java switch Statement
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
CprE 185: Intro to Problem Solving (using C)
Presentation transcript:

(5-1) Selection Structures III in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 21, 2015) Washington State University

C. Hundhausen, A. O’Fallon 2 Advantages of switch Statements (1) One issue with nested if statements is readability – The deeper the nesting, the more difficult it can be to figure out what's happening Another issue is that the programmer could mistakenly "mis-nest" if statements, as in the previous example found in lecture set 4-3 In cases in which the nesting is based on the value of a single variable, a switch statement may be a better alternative Offer the potential for faster execution through the program

C. Hundhausen, A. O’Fallon 3 Advantages of switch Statements (2) Easier to debug Understandability Maintainability

C. Hundhausen, A. O’Fallon 4 switch Statements Example (1) Let's revisit the previous baseball scenario: A high school baseball team awards merit points to players based on their offensive performance. A single (encoded 's') is worth 1 point, a double (encoded 'd') is worth 2 points, a triple (encoded 't') is worth 3 points, and a home run (encoded 'h') is worth 4 points. Any at-bat that leads to an out (encoded 'o') worth 0 points. Write a C statement that, given an at-bat character, properly awards points.

C. Hundhausen, A. O’Fallon 5 switch Statements Example (2) The switch statement provides a cleaner way to handle this scenario: char at_bat; int points; points = 0; printf("Enter the at-bat code (s,d,t,h,o): "); scanf(" %c",&at_bat); switch (at_bat) { case 's': /* single */ points = points + 1; break; case 'd': /* double */ points = points + 2; break; case 't': /* triple */ points = points + 3; break; case 'h': /* home run */ points = points + 4; break; case 'o': points = points + 0; break; default: /* Anything but 's','d','t','h' */ printf("Unrecognized at-bat code."); break; } Don't forget to end each case with break Don't forget the begin and end curly braces If at_bat does not match any other case labels, the default case is executed

C. Hundhausen, A. O’Fallon 6 switch Statements Example (3) What if we also want to allow capital letter codes? switch (at_bat) { case 's': /* single */ case 'S': points = points + 1; break; case 'd': /* double */ case 'D': points = points + 2; break; case 't': /* triple */ case 'T': points = points + 3; break; case 'h': /* home run */ case 'H': points = points + 4; break; case 'o': case 'O': points = points + 0; break; default: /* Anything but 's','d','t','h' */ printf("Unrecognized at-bat code."); break; } A single case can contain an arbitrary number of case labels

C. Hundhausen, A. O’Fallon 7 switch Statements Example (4) Let's revisit the following scenario: A high school baseball team awards merit points to players based on their offensive performance and the class standing ('f' = freshman, 'o' = sophomore, 'j' = junior, and 's' = senior). In particular, freshmen and sophomores earn an extra point for home runs, whereas juniors and seniors do not earn any points for singles. Write a C if-statement that, given an at-bat character and a class standing character, properly awards points.

C. Hundhausen, A. O’Fallon 8 switch Statements Example (5) We can write this more clearly as a switch statement with embedded if statements : … switch (at_bat) { case 's': /* single */ if (class_standing == 'f') || (class_standing == 'o') { points = 1; } break; case 'd': /* double */ points = 2; break; case 't': /* triple */ points = 3; break; case 'h': /* home run */ if ((class_standing == 'f') || (class_standing == 'o')) { points = 5; } else { points = 4; } break; case 'o': points = 0; break; default: /* Anything but 's','d','t','h' */ printf("Unrecognized at-bat code."); break; }

C. Hundhausen, A. O’Fallon 9 You Try It (revisited) Consider the following scenario: In an effort to make their weather reports more user-friendly, the national weather service would like to translate relative humidity percentages (integers between 0 and 100) into descriptions as follows: – 20% or lower "bone dry" – 21% - 40%:"dry" – 41% - 60%:"moderately dry" – 61% - 80%:"moderately humid" – 81% or higher:"humid" Write a switch statement that, given the relative humidity (an int between 0 and 100), prints out the appropriate description. Will this work? Why or why not?

C. Hundhausen, A. O’Fallon 10 switch Statements Best Practices Notes on switch statements – You cannot switch based on the value of a double or string variable: char code[20] = "home run"; double era = 1.67; switch (code) { … /* can't do this */ switch (era) { … /* can't do this either */ An arbitrary number of statements can follow a case label It's a good idea always to define a default case A common mistake is to forget to end a case with break. If a break is forgotten, execution "falls" through to the next case label!

C. Hundhausen, A. O’Fallon 11 Next Time… Repetition in C – Executing statements while a condition is true – Can eliminate redundant sequential statements

C. Hundhausen, A. O’Fallon 12 References J.R. Hanly & E.B. Koffman, Problem Solving and Program Design in C (8 th Ed.), Addison- Wesley, P.J. Deitel & H.M. Deitel, C How to Program (7 th Ed.), Pearson Education, Inc., 2013.

C. Hundhausen, A. O’Fallon 13 Collaborators Chris Hundhausen