J-1 University of Washington Computer Programming I Switch Statement © 2000 UW CSE.

Slides:



Advertisements
Similar presentations
Control Structures.
Advertisements

Chapter 4 Computation Bjarne Stroustrup
More on Algorithms and Problem Solving
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
1 CSE1301 Computer Programming Lecture 8 Selection.
Introduction to Control Statements Presented by: Parminder Singh BCA 5 th Sem. [ Batch] PCTE, Ludhiana 5/12/ Control Statements.
Data Types H&K Chapter 7 Instructor – Gokcen Cilingir Cpt S 121 (July 12, 2011) Washington State University.
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
1 CSE1301 Computer Programming Lecture 9 Selection.
N-1 University of Washington Computer Programming I Lecture 19: Strings © 2000 UW CSE.
B-1 Lecture 2: Problems, Algorithms, and Programs © 2000 UW CSE University of Washington Computer Programming I.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
ECE122 L8: More Conditional Statements February 7, 2007 ECE 122 Engineering Problem Solving with Java Lecture 8 More Conditional Statements.
Selection Structures: Switch CSC 1401: Introduction to Programming with Java Week 4 – Lecture 1 Wanda M. Kunkle.
E-1 University of Washington Computer Programming I Lecture 6: Conditionals © 2000 UW CSE.
Switch Statements Comparing Exact Values. 2 The Switch Statement The switch statement provides another way to decide which statement to execute next The.
The switch StatementtMyn1 The switch Statement Sometimes there can be a multiple-choice situation, in which you need to execute a particular set of statements.
Chapter 4 Program Control Statements
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
CCSA 221 Programming in C CHAPTER 14 MORE ON DATA TYPES 1 ALHANOUF ALAMR.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
The switch StatementtMyn1 The switch Statement Sometimes there can be a multiple-choice situation, in which you need to execute a particular set of statements.
1 CSC103: Introduction to Computer and Programming Lecture No 11.
IF-ELSE IF-ELSE STATEMENT SWITCH-CASE STATEMENT Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Conditional Statements For computer to make decisions, must be able to test CONDITIONS IF it is raining THEN I will not go outside IF Count is not zero.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
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.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
ITIP © Ron Poet Lecture 12 1 Finding Out About Objects.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Switch Statements Comparing Exact Values. The Switch Statement: Syntax The switch statement provides another way to decide which statement to execute.
Introduction to Computing Lecture 05: Selection (continued) Introduction to Computing Lecture 05: Selection (continued) Assist.Prof.Dr. Nükhet ÖZBEK Ege.
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
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.
G-1 10/4/99 CSE / ENGR 142 Programming I Conditionals © 1999 UW CSE.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
1 CS161 Introduction to Computer Science Topic #8.
Structures and Enumerations Introduction Structure definitions Nested structure Referring and initializing structure elements Passing structures to a function.
Decision Making. Alter the flow of the program Conditionally controlling execution of a set of commands if -statement: if a > b: print a.
CISC105 – General Computer Science Class 4 – 06/14/2006.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
CSE1301 Sem Selection Lecture 25 Lecture 9: Selection.
Decisions Chapter 4.
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Programming Fundamentals
Control Structures.
Lecture 2: Logical Problems with Choices
Writing a program with conditionals
CSI 121 Structure Programming Language Lecture 9 Selection
Conditionals.
State Machines 6-Apr-196-Apr-19.
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
Program Flow.
Introduction to Computing Lecture 04: Booleans & Selection
State Machines 8-May-19.
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
Chapter 3: Selection Structures: Making Decisions
Arrays Introduction to Arrays Reading for this Lecture:
Presentation transcript:

J-1 University of Washington Computer Programming I Switch Statement © 2000 UW CSE

J-2 Overview Concepts this lecture The switch statement Choosing between if and switch Reading Textbook sec. 4.8

J-3 Review: Conditional Control Flow The if statement choses one of two statements to execute before continuing An if statement could also be used to decide whether or not to to skip a statement before continuing

J-4 Multi-way Control Flow The choice may be “multi-way” rather than simply between two alternatives In C, if statements can be used, and sometimes a statement called the switch can be used

J-5 Multi-way Choice with if /* How many days in a month? */ if ( month == 1 ) {/* Jan */ days = 31 ; } else if ( month == 2 ) {/* Feb */ days = 28 ; } else if ( month == 3 ) {/* Mar */ days = 31 ; } else if ( month == 4 )/* Apr */ days = 30 ;.../* need 12 of these */

J-6 Better… if ( month == 9 || month == 4 || /* Sep, Apr */ month == 6 || month == 11 ) { /* Jun, Nov */ days = 30 ; } else if ( month == 2 ) {/* Feb */ days = 28 ; } else { days = 31;/* All the rest */ }

J-7 Alternative: switch A switch is a form of conditional statement. It is specifically designed to be useful in multi-way choice situations. Instead of a condition, there is a value which is tested, and a series of cases of which only one may be chosen.

J-8 Using switch /* How many days in a month? */ switch ( month ) { case 2:/* February*/ days = 28 ; break ; case 9:/* September*/ case 4:/* April */ case 6:/* June*/ case 11:/* November*/ days = 30 ; break ; default: /* All the rest have 31...*/ days = 31 ; } printf ( “There are %d days. \n ”, days ) ;

J-9 switch Statement The syntax of switch differs from other C statements switch (int expression) {... /*a series of cases */... } The value of the expression determines which of the cases is executed.

J-10 Cases A case is a section of code within the switch statement. A case is executed only if the switch expression has a specified value case value: /* a sequence of statements*/ The sequence is typically ended with special statement break; break causes the entire switch statement to end

J-11 The switch Expression The switch expression is not a conditional expression as it is in an if statement Only an integer expression is allowed Most often, the expression is a single integer variable The value of the variable determines which case is chosen

J-12 switch: Flow of Control month = 6 ; switch ( month ) { case 2: /* February */ days = 28 ; break ; case 9: /* September */ case 4: /* April */ case 6: /* June */ case 11: /* November */ days = 30 ; break ; default: /* All the rest have 31...*/ days = 31 ; } printf ( “There are %d days. \n ”, days ) ;

J-13 The One Big Pitfall of switch month = 6 ; switch (month) { case 2:/* February */ days = 28 ;/* break missing */ case 9:/* September */ case 4:/* April */ case 6:/* June */ case 11:/* November */ days = 30 ; /* break missing */ default: /* All the rest have */ days = 31 ; } printf ( “There are %d days. \n ”, days ) ;

J-14 char marital_status ;... switch ( marital_status ) { case ‘m’: case ‘M’: printf ( “Married \n” ) ; break ;int or char expression case ‘s’: case ‘S’: printf ( “Single \n” ) ; break ; default: printf ( “Sorry, I don’t recognize that code. \n” ) ; } switch on char is also legal

J-15 Switch is a form of conditional statement Switch is suitable for multi-way conditions that depend upon an integer (or char) value Pay attention to the syntax of switch The switch and if statements are not fully interchangeable Summing Up

J-16 char marital_status ;... switch ( marital_status ) { case ‘m’: case ‘M’: Why should a character be allowed here, when the expression is supposed to be an integer? Bonus Footnote

J-17 char marital_status ;... switch ( marital_status ) { case ‘m’: case ‘M’: Why should a character be allowed here, when the expression is supposed to be an integer? Answer: The actual machine representation of a character is a small integer. Most of the time, however, you should treat ints and chars as fully different types! Bonus Footnote