OSU ENGR H192 - Lecture 09 - Switch Case Structures

Unformatted text preview:

Switch Case StructuresSwitch Multiple Selection StructureSwitch-Case StructuresSlide 4Slide 5A Sample Program to Illustrate Switch-CaseSlide 7Slide 8Slide 9Slide 10Switch-Case Structures Resultant Output from Grade ProgramSlide 12Fixed Program using Switch-Case StructuresFixed Program using Switch-Case StructuresSlide 15Comments on Last Example ProgramSlide 17Flawed Sample Character Input ProgramOutput from Flawed Sample ProgramCorrect Sample Input ProgramOutput from Correct Input ProgramSlide 22Lect 9 P. 1 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionSwitch Case StructuresLecture 9Lect 9 P. 2 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionSwitch Multiple Selection Structure•A multiple selection structure is useful when an algorithm contains a series of decisions in which a variable or expression is tested separately for one of several possible integral values.•Each integral value represents a different action to be taken in the algorithm.•C provides the switch multiple selection structure to implement this type of decision making.Lect 9 P. 3 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionSwitch-Case Structures•The switch - case syntax is: switch (integer expression test value) { case case _1_fixed_value : action(s) ; case case_2_fixed_value : action(s) ; default : action(s) ; }Note use of colon!Lect 9 P. 4 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionSwitch-Case Structures•The switch is the "controlling expression"–Can only be used with constant integer expressions.–Remember, a single character is a small positive integer.–The expression appears in ( )•The case is a "label"–The label must be followed by a " : "–Braces, { }, not required around statementsLect 9 P. 5 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionSwitch-Case Structures•Unlike if-else if-else structures, when the value in a case matches the test value, all of the actions in the rest of the structure take place.•This is shown in the following program where the user enters a value that matches the first case and every action in the structure is executed.Lect 9 P. 6 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionA Sample Program to Illustrate Switch-CaseProblem: Write a program to ask the user to enter his/her letter grade and then respond with an appropriate message regarding his/her academic status.Lect 9 P. 7 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionA Sample Program to Illustrate Switch-CaseAlgorithm:1. Set up the environment 2. Prompt user to enter his/her letter grade3. Get user’s response4. If grade is a or A say “Good Job” and go to 95. If grade is b or B say “Pretty good” and go to 96. If grade is c or C say “Better get to work” and go to 97 If grade is d or D say “You are in trouble” and go to 98. Say “You are failing”9. Terminate programLect 9 P. 8 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionA Sample Program to Illustrate Switch-Case/* This program associates a letter grade with a message appropriate to the score. */#include <stdio.h>int main ( ){char grade ;printf ("Enter your current letter grade\n") ;grade = getchar ( ) ;Lect 9 P. 9 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionA Sample Program to Illustrate Switch-Case switch (grade) {case ('a') :case ('A') :printf ("Good Job!\n") ;case ('b') :case ('B') :printf ("Pretty good.\n") ;Lect 9 P. 10 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionA Sample Program to Illustrate Switch-Casecase ('c') :case ('C') :printf ("Better get to work.\n") ;case ('d') :case ('D') :printf ("You are in trouble.\n") ;default : printf ("You are failing!!\n") ; } /* End of switch-case structure */} /* End of main program */Lect 9 P. 11 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionSwitch-Case StructuresResultant Output from Grade Program /* The following results are produced when the user enters an "A" as input to the program prompt. */Good Job!Pretty good.Better get to work.You are in trouble.You are failing!Lect 9 P. 12 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionSwitch-Case Structuresbreak ; •The problems with the previous program can be corrected by use of the break statement. •It can be used in either a repetition structure or a selection structure to break out of (that is, to exit from) the structure.•The syntax is:break ; •The following program is the previous one with the addition of the break statements.Lect 9 P. 13 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionFixed Program using Switch-Case Structures #include <stdio.h>int main ( ) {int grade ;printf ("Enter your current letter grade\n") ; while ( ( grade = getchar ( ) ) != EOF) { switch (grade) { case ('a') : case ('A') :printf ("Good Job!\n") ;break ;Lect 9 P. 14 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionFixed Program using Switch-Case Structurescase ('b') : case ('B') :printf ("Pretty good.\n") ;break ;case ('c') : case ('C') :printf ("Better get to work.\n") ;break ; case ('d') : case ('D') :printf ("You are in trouble.\n") ;break ;Lect 9 P. 15 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionFixed Program using Switch-Case Structures case ('f') : case ('F'):printf ("You are failing!!\n") ;break ; case (' ') : case ('\n') : //< See note pg 17break ; default :printf ("Invalid grade. Try again.\n") ; } /* End of switch/case */} /* End of while loop */} /* End of "main" function */Lect 9


View Full Document

OSU ENGR H192 - Lecture 09 - Switch Case Structures

Documents in this Course
Strings

Strings

22 pages

Load more
Download Lecture 09 - Switch Case Structures
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 Lecture 09 - Switch Case Structures 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 Lecture 09 - Switch Case Structures 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?