0% found this document useful (0 votes)
26 views15 pages

University of Washington-7

The document discusses the switch statement in C programming. It explains what a switch statement is, how it provides an alternative to multiple if/else statements for multi-way conditional control flow. It covers the syntax of switch, how the cases work, and emphasizes the importance of the break statement to end each case. Examples are provided to illustrate switch statements for determining the number of days in a month based on the month number.

Uploaded by

osmandag34
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views15 pages

University of Washington-7

The document discusses the switch statement in C programming. It explains what a switch statement is, how it provides an alternative to multiple if/else statements for multi-way conditional control flow. It covers the syntax of switch, how the cases work, and emphasizes the importance of the break statement to end each case. Examples are provided to illustrate switch statements for determining the number of days in a month based on the month number.

Uploaded by

osmandag34
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

University of

Washington
Computer
Programming I

Switch Statement

© 2000 UW CSE J-1


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

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

J-3
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-4
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-5
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-6
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-7
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-8
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-9
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-10
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-11
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-12
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 31 ... */
days = 31 ;
}
printf ( “There are %d days. \n ”, days ) ; J-13
switch on char is also legal
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” ) ;
}
J-14
Summing Up
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 J-15

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy