DOC PREVIEW
UMBC CMSC 104 - The switch Statement

This preview shows page 1-2-19-20 out of 20 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

The switch StatementMultiple SelectionMultiple Selection (con’t)Multiple Selection with ifMultiple Selection with if-elseThe switch Multiple-Selection Structureswitch Statement DetailsGood Programming Practicesswitch ExampleWhy Use a switch Statement?The char Data TypeThe char Data Type (con’t)char ExampleThe getchar ( ) Functiongetchar ( ) ExampleProblems with Reading CharactersImproved getchar( ) ExampleAdditional Concerns with Garbage in stdinEOF Predefined Constantgetchar( ) Example Using EOFCMSC 104, Version 9/01 1The switch StatementTopics•Multiple Selection•switch Statement•char Data Type and getchar( )•EOF constantReading•Section 4.7, 4.12CMSC 104, Version 9/01 2Multiple Selection•So far, we have only seen binary selection.if ( age >= 18 ){ printf(“Vote!\n”) ;}if ( age >= 18 ){ printf(“Vote!\n”) ;}else{ printf(“Maybe next time!\n”) ;}CMSC 104, Version 9/01 3Multiple Selection (con’t)•Sometimes it is necessary to branch in more than two directions.•We do this via multiple selection.•The multiple selection mechanism in C is the switch statement.CMSC 104, Version 9/01 4Multiple Selection with ifif (day == 0 ) { printf (“Sunday”) ;}if (day == 1 ) { printf (“Monday”) ;}if (day == 2) { printf (“Tuesday”) ;}if (day == 3) { printf (“Wednesday”) ;} (continued)if (day == 4) { printf (“Thursday”) ;}if (day == 5) { printf (“Friday”) ;}if (day == 6) { printf (“Saturday”) ;}if ((day < 0) || (day > 6)) { printf(“Error - invalid day.\n”) ;}CMSC 104, Version 9/01 5Multiple Selection with if-elseif (day == 0 ) { printf (“Sunday”) ;} else if (day == 1 ) { printf (“Monday”) ;} else if (day == 2) { printf (“Tuesday”) ;} else if (day == 3) { printf (“Wednesday”) ;} else if (day == 4) { printf (“Thursday”) ;} else if (day == 5) { printf (“Friday”) ;} else if (day = 6) { printf (“Saturday”) ;} else { printf (“Error - invalid day.\n”) ;}This if-else structure is more efficient than the corresponding if structure. Why?CMSC 104, Version 9/01 6The switch Multiple-Selection Structureswitch ( integer expression ){case constant1 :statement(s) break ;case constant2 :statement(s)break ;. . .default: :statement(s)break ;}CMSC 104, Version 9/01 7switch Statement Details•The last statement of each case in the switch should almost always be a break.•The break causes program control to jump to the closing brace of the switch structure.•Without the break, the code flows into the next case. This is almost never what you want.•A switch statement will compile without a default case, but always consider using one.CMSC 104, Version 9/01 8Good Programming Practices•Include a default case to catch invalid data.•Inform the user of the type of error that has occurred (e.g., “Error - invalid day.”).•If appropriate, display the invalid value.•If appropriate, terminate program execution (discussed in CMSC 201).CMSC 104, Version 9/01 9switch Exampleswitch ( day ){case 0: printf (“Sunday\n”) ; break ;case 1: printf (“Monday\n”) ; break ;case 2: printf (“Tuesday\n”) ; break ;case 3: printf (“Wednesday\n”) ; break ;case 4: printf (“Thursday\n”) ; break ;case 5: printf (“Friday\n”) ; break ;case 6: printf (“Saturday\n”) ; break ;default: printf (“Error -- invalid day.\n”) ; break ;}Is this structure more efficient than the equivalent nested if-else structure?CMSC 104, Version 9/01 10Why Use a switch Statement?•A nested if-else structure is just as efficient as a switch statement.•However, a switch statement may be easier to read.•Also, it is easier to add new cases to a switch statement than to a nested if-else structure.CMSC 104, Version 9/01 11The char Data Type•The char data type holds a single character. char ch;•Example assignments:char grade, symbol;grade = ‘B’;symbol = ‘$’;•The char is held as a one-byte integer in memory. The ASCII code is what is actually stored, so we can use them as characters or integers, depending on our need.CMSC 104, Version 9/01 12The char Data Type (con’t)•Use scanf (“%c”, &ch) ; to read a single character into the variable ch. (Note that the variable does not have to be called “ch”.”)•Useprintf(“%c”, ch) ;to display the value of a character variable.CMSC 104, Version 9/01 13char Example#include <stdio.h>int main ( ){char ch ;printf (“Enter a character: “) ;scanf (“%c”, &ch) ;printf (“The value of %c is %d.\n”, ch, ch) ; return 0 ;}If the user entered an A, the output would be:The value of A is 65.CMSC 104, Version 9/01 14The getchar ( ) Function•The getchar( ) function is found in the stdio library.•The getchar( ) function reads one character from stdin (the standard input buffer) and returns that character’s ASCII value.•The value can be stored in either a character variable or an integer variable.CMSC 104, Version 9/01 15getchar ( ) Example#include <stdio.h>int main ( ){char ch ; /* int ch would also work! */printf (“Enter a character: “) ; ch = getchar( ) ;printf (“The value of %c is %d.\n”, ch, ch) ; return 0 ;}If the user entered an A, the output would be:The value of A is 65.CMSC 104, Version 9/01 16Problems with Reading Characters•When getting characters, whether using scanf( ) or getchar( ), realize that you are reading only one character.•What will the user actually type? The character he/she wants to enter, followed by pressing ENTER. •So, the user is actually entering two characters, his/her response and the newline character.•Unless you handle this, the newline character will remain in the stdin stream causing problems the next time you want to read a character. Another call to scanf() or getchar( ) will remove it.CMSC 104, Version 9/01 17Improved getchar( ) Example#include <stdio.h>int main ( ){ char ch, newline ; printf (“Enter a character: “) ; ch = getchar( ) ; newline = getchar( ) ; /* could also use scanf(“%c”, &newline) ; */ printf (“The value of %c is %d.\n”, ch, ch) ; return 0 ;}If the user entered an A, the output would be:The value of A is 65.CMSC 104, Version 9/01 18Additional Concerns with Garbage in stdin•When we were reading integers using scanf( ), we didn’t seem to have problems with the newline character, even though the user was typing ENTER after the integer.•That is because scanf( ) was looking


View Full Document

UMBC CMSC 104 - The switch Statement

Download The switch Statement
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view The switch Statement and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view The switch Statement 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?