CSC 107 - Programming for Science Lecture 11: Switch Statements & ?: Operator.

Slides:



Advertisements
Similar presentations
CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Advertisements

8-May-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
(c) 2003 E.S.Boese1 Conditionals Chapter 10 - Student.
16-Jun-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Loops – While, Do, For Repetition Statements Introduction to Arrays
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 9 – Income Tax Calculator Application: Introducing.
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
TODAY’S LECTURE Review Chapter 2 Go over exercises.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
1 CSC103: Introduction to Computer and Programming Lecture No 11.
CPS120: Introduction to Computer Science Decision Making in Programs.
CMSC 104, Version 8/061L11Relational&LogicalOps.ppt Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else.
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
© 2000 Scott S Albert Selection Structures Structured Programming 256 Chapter 4.
CPS120: Introduction to Computer Science Decision Making in Programs.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
CSC 107 – Programming For Science. The Week’s Goal.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
CSC 107 – Programming For Science. Announcement Today’s Goal  Know how to write selections besides if-else  Why we use select or if - else and how.
Switch Statements Comparing Exact Values. The Switch Statement: Syntax The switch statement provides another way to decide which statement to execute.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Compound Statements If you want to do more than one statement if an if- else case, you can form a block of statements, or compound statement, by enclosing.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Decisions. Three Forms of Decision Making in Java if statements (test a boolean expression) switch statements (test an integer expression) conditional.
CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CSC Programming for Science Lecture 10: Boolean Expressions & More If ­ Else Statements.
CSC 107 – Programming For Science. Today’s Goal  Know how to use and write for loops  Explain why to use for, while, & do-while loops  Convert between.
CSC 107 – Programming For Science. Today’s Goal  Know how to write selections besides if-else  When each of the options makes sense  When each selection.
Control statements Mostafa Abdallah
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 b Boolean expressions b truth tables b conditional operator b switch statement b repetition statements: whilewhile do/whiledo/while forfor Lecture 3.
PHY 107 – Programming For Science. Today’s Goal  Know how to write decisions besides if-else select  Why we use select or if - else and how to choose.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
CMSC 104, Version 9/011 Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else Statement Nesting of.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Program Control: Selection Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
CSC Programming for Science Lecture 5: Actual Programming.
Lecture 3 Selection Statements
Week 3 Part 2 Kyle Dewey.
Selections Java.
Conditionals & Boolean Expressions
Expressions and Control Flow in JavaScript
Control Structures – Selection
Selection (if-then-else)
Selection CSCE 121 J. Michael Moore.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
The Java switch Statement
Chap 7. Advanced Control Statements in Java
CSC215 Lecture Control Flow.
Controlling Program Flow
Presentation transcript:

CSC Programming for Science Lecture 11: Switch Statements & ?: Operator

Today’s Goal After today, should know ways of writing selections besides if-else statements  When each of the options makes sense  When each selection statements should NOT be used  How to covert between them

Choosing From Many Options Consider software controlling playback of voice mail  Action depends on which of 12 buttons is hit  Pushing many keys has same effect  Makes for long, complex if-else statement Imagine choosing from 256 options  Necessary for running Java programs  Code would be very long & hard to read

Long if-else statement if (keyHit == 1) { printf(“Please say message to record”); } else if (keyHit == 2) { printf(“Skipping to next message”); } else if (keyHit == 3) { printf(“Playing first new message”); } else if (keyHit == 4) { printf(“Message will be saved for 30 days.”); } else if ((keyHit == 5) || (keyHit == 8)) { printf(“I’m sorry Dave, I can’t do that.”); } else if (keyHit == 6) { printf(“Message deleted”); } else if (keyHit == 7) { printf(“gnidniwer egasseM “); }

Better idea: switch Statement switch (keyHit) { case 1: printf(“Please say message to record”); break; case 2: printf(“Skipping to next message”); break; case 3: printf(“Playing first new message”); break; case 4: printf(“Message will be saved for 30 days.”); break; case 5: case 8: printf(“I’m sorry Dave, I can’t do that.”); break; case 6: printf(“Message deleted”); break; case 7: printf(“gnidniwer egasseM “); break; }

Outline of switch statement switch (expression) { case label_1: statement;... break; case label_2: case label_3: statement;... break;... default: statement;... break; }

switch Statement Selects statements using expression value  Easier to read than if-else Expression evaluated could be literal, variable, or something more complex  Result must be char, short, int, or long Labels must be literal values  E.g., 0, -345, ‘a’, 4351  Can use a symbolic constant (#define) if its value is a literal

Executing a switch Statement First evaluates the expression Finds matching case (or default)  Default label is optional  Without default, may not have any match --- program continues execution after switch Execution starts from 1 st matching label  Continues executing until break; found  Forgetting break is a common error -- execution will continue into the next case! Once break hit, continues running from after the switch

Tracing switch Statement 0 float temp = -40; 1 float convert; 2 char c = getchar(); 3 switch (toupper(c)) { 4 case ‘C’: 5 convert = (temp / 5) * 9) + 32; 6 break; 7 case ‘F’: 8 convert = ((temp - 32) / 9) * 5; 9 break; 10 default: 11 printf(“Don’t know that scale.”); 12 break; 13 } 14 printf(“Converted temp: %lf\n”, convert); Line #tempconvertc

Tracing switch Statement 0 float temp = -40; 1 float convert; 2 char c = getchar(); 3 switch (toupper(c)) { 4 case ‘C’: 5 convert = (temp / 5) * 9) + 32; 6 break; 7 case ‘F’: 8 convert = ((temp - 32) / 9) * 5; 9 break; 10 default: 11 printf(“Don’t know that scale.”); 12 break; 13 } 14 printf(“Converted temp: %lf\n”, convert); Line #tempconvertc

Tracing switch Statement 0 float temp = -40; 1 float convert; 2 char c = getchar(); 3 switch (toupper(c)) { 4 case ‘C’: 5 convert = (temp / 5) * 9) + 32; 6 break; 7 case ‘F’: 8 convert = ((temp - 32) / 9) * 5; 9 break; 10 default: 11 printf(“Don’t know that scale.”); 12 break; 13 } 14 printf(“Converted temp: %lf\n”, convert); Line #tempconvertc

If-else versus switch switch performs multiple comparisons on single integer expression  Comparisons are for equality only  Labels must be literal values  Actions can overlap by not using break if-else tests many “boolean” expressions  Works with any data types needed  Tests do not have to be related  Actions cannot be overlapped

Confession I hate the conditional operator  Makes code hard to read, difficult to debug  Usually used by hackers who think it “cool”  Compiler ultimately converts it to if-else It is important to know, however  Used by hackers who think it “cool”  On rare occasion, can simplify coding  People not required to follow my opinion (yet)

Conditional Operator Operator takes three operands 1. “Boolean” expression to evaluate 2. Action to take if expression is “true” 3. Action taken when expression “false” Operator is written like: (expression) ? true_action : false_action

Using Conditional Operator Can be used by itself: (a < b) ? foo += 1 : printf(“A is huge”); which is identical to if (a < b) { foo += 1; } else { printf(“A is huge”); }

Using Conditional Operator (2) Can also be used within a command: max = (a < b) ? b : a; which would be equivalent to: if (a < b) { max = b; } else { max = a; }

Your Turn Get into groups and complete daily activity

For Next Lecture Read Sections 3.4 of book  Do not need to understand all the details  But important knowing what is not understood Finish week #4 weekly assignment Finish #3 up and running First programming assignment on web  Due 2 weeks from today