1 / 20

Chap 4

Chap 4. The switch Multiple-Selection Statement. The switch Multiple-Selection Statement. switch Useful when a variable or expression is tested for all the values it can assume and different actions are taken Format Series of case labels and an optional default case

david-riggs
Download Presentation

Chap 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chap 4 The switch Multiple-Selection Statement

  2. The switch Multiple-Selection Statement • switch • Useful when a variable or expression is tested for all the values it can assume and different actions are taken • Format • Series of case labels and an optional default case switch ( value ){ case '1': actions case '2': actions default: actions }

  3. The switch Multiple-Selection Statement

  4. Counting letter grades • #include <stdio.h> • /* function main begins program execution */ • int main() • { • int grade; /* one grade */ • intaCount = 0; /* number of As */ • intbCount = 0; /* number of Bs */ • intcCount = 0; /* number of Cs */ • intdCount = 0; /* number of Ds */ • intfCount = 0; /* number of Fs */ • printf( "Enter the letter grades.\n" ); • printf( "Enter the EOF character to end input.\n" );

  5. /* determine which grade was input */ switch ( grade ) { /* switch nested in while */ case 'A': /* grade was uppercase A */ case 'a': /* or lowercase a */ ++aCount; /* increment aCount */ break; /* necessary to exit switch */ case 'B': /* grade was uppercase B */ case 'b': /* or lowercase b */ ++bCount; /* increment bCount */ break; /* exit switch */ case 'C': /* grade was uppercase C */ case 'c': /* or lowercase c */ ++cCount; /* increment cCount */ break; /* exit switch */

  6. case 'D': /* grade was uppercase D */ • case 'd': /* or lowercase d */ • ++dCount; /* increment dCount */ • break; /* exit switch */ • case 'F': /* grade was uppercase F */ • case 'f': /* or lowercase f */ • ++fCount; /* increment fCount */ • break; /* exit switch */ • case '\n': /* ignore newlines, */ • case '\t': /* tabs, */ • case ' ': /* and spaces in input */ • break; /* exit switch */

  7. default: /* catch all other characters */ • printf( "Incorrect letter grade entered." ); • printf( " Enter a new grade.\n" ); • break; /* optional; will exit switch anyway */ • } /* end switch */ • } /* end while */

  8. /* output summary of results */ • printf( "\nTotals for each letter grade are:\n" ); • printf( "A: %d\n", aCount ); /* display number of A grades */ • printf( "B: %d\n", bCount ); /* display number of B grades */ • printf( "C: %d\n", cCount ); /* display number of C grades */ • printf( "D: %d\n", dCount ); /* display number of D grades */ • printf( "F: %d\n", fCount ); /* display number of F grades */ • return 0; /* indicate program ended successfully */ • } /* end function main */

  9. while ( ( grade = getchar() ) != EOF ) • The getcharfunction (from <stdio.h>) reads one character from the keyboard and stores that character in the integer variable grade. • Important feature of C is that characters can be stored in any integer data type because they’re usually represented as one-byte integers in the computer.

  10. while ( ( grade = getchar() ) != EOF ) • We can treat a character as either an integer or a character, depending on its use. For example, the statement. • printf( "The character (%c) has the value %d.\n", 'a', 'a' ); • The character (a) has the value 97.

  11. while ( ( grade = getchar() ) != EOF ) • ASCII (American Standard Code for Information Interchange) • character set in which 97 represents the lowercase letter 'a'.

  12. EOF • A symbol whose acronym stands for “end of file”. • The user types a system-dependent keystroke combination to mean “end of file”—i.e., “I have no more data to enter.” • We have chosen to represent characters in this program as ints because EOF has an integervalue (again, normally -1).

  13. EOF • The keystroke combinations for entering EOF (end of file) are system dependent • Testing for the symbolic constant EOF rather than –1 makes programs more portable. The • C standard states that EOF is a negative integral value (but not necessarily –1). Thus, EOF • Could have different values on different systems.

  14. EOF - Microsoft Windows The EOF indicator can be entered by typing <Ctrl> z • You may also need to press Enteron Windows.

  15. The break statement • The break statement causes program control to continue with the first statement after the switch statement. • The break statement is used because the cases in a switch statement would otherwise run together. • If break is not used anywhere in a switch statement,theneach time a match occurs in the statement, the statements for all the remaining cases will be executed.

  16. The break statement… • If no match occurs, the default case is executed, and an error message is printed. • The switch statement is different from all other control statements in that braces are not required around multiple actions in a case of a switch.

  17. Program to skip newline, tab and blank characters • case '\n': /* ignore newlines, */ • case '\t': /* tabs, */ • case ' ': /* and spaces in input */ • break; /* exit switch */

  18. Program to skip newline, tab and blank characters • To have the program read the characters, they must be sent to the computer by pressing the Enter key. This causes the newline character to be placed in the input after the character we wish to process.

  19. Listing several case labels together (such as case 'D': case 'd') • Simply means that the same set of actions is to occur for either of these cases. • Any combination of character constants and integer constants that evaluates to a constant integer value • Remember that characters are represented as small integer values.

  20. Assignment • Please choose a color(1: red,2: green,3: blue) • Example • If someone enter 1 • Print “You chose red color”

More Related