Engineering H 192 Computer Programming Switch Case Structures

  • Slides: 23
Download presentation
Engineering H 192 - Computer Programming Switch Case Structures Lecture 9 The Ohio State

Engineering H 192 - Computer Programming Switch Case Structures Lecture 9 The Ohio State University Gateway Engineering Education Coalition 1

Engineering H 192 - Computer Programming Switch Multiple Selection Structure • A multiple selection

Engineering H 192 - Computer Programming Switch 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. Winter Quarter The Ohio State University Gateway Engineering Education Coalition 2

Engineering H 192 - Computer Programming Switch-Case Structures • The switch - case syntax

Engineering H 192 - Computer Programming Switch-Case Structures • The switch - case syntax is: switch (integer expression test value) { Note use of colon! case _1_fixed_value : action(s) ; case_2_fixed_value : action(s) ; default : action(s) ; } Winter Quarter The Ohio State University Gateway Engineering Education Coalition 3

Engineering H 192 - Computer Programming Switch-Case Structures • The switch is the "controlling

Engineering H 192 - Computer Programming Switch-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 statements Winter Quarter The Ohio State University Gateway Engineering Education Coalition 4

Engineering H 192 - Computer Programming Switch-Case Structures • Unlike if-else structures, when the

Engineering H 192 - Computer Programming Switch-Case Structures • Unlike 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. Winter Quarter The Ohio State University Gateway Engineering Education Coalition 5

Engineering H 192 - Computer Programming A Sample Program to Illustrate Switch-Case Problem: Write

Engineering H 192 - Computer Programming A Sample Program to Illustrate Switch-Case Problem: 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. Winter Quarter The Ohio State University Gateway Engineering Education Coalition 6

Engineering H 192 - Computer Programming A Sample Program to Illustrate Switch-Case Algorithm: 1.

Engineering H 192 - Computer Programming A Sample Program to Illustrate Switch-Case Algorithm: 1. Set up the environment 2. Prompt user to enter his/her letter grade 3. Get user’s response 4. If grade is a or A say “Good Job” and go to 9 5. If grade is b or B say “Pretty good” and go to 9 6. If grade is c or C say “Better get to work” and go to 9 7 If grade is d or D say “You are in trouble” and go to 9 8. Say “You are failing” 9. Terminate program Winter Quarter The Ohio State University Gateway Engineering Education Coalition 7

Engineering H 192 - Computer Programming A Sample Program to Illustrate Switch-Case /* This

Engineering H 192 - Computer Programming A 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 graden") ; grade = getchar ( ) ; Winter Quarter The Ohio State University Gateway Engineering Education Coalition 8

Engineering H 192 - Computer Programming A Sample Program to Illustrate Switch-Case switch (grade)

Engineering H 192 - Computer Programming A Sample Program to Illustrate Switch-Case switch (grade) { case ('a') : case ('A') : printf ("Good Job!n") ; case ('b') : case ('B') : printf ("Pretty good. n") ; Winter Quarter The Ohio State University Gateway Engineering Education Coalition 9

Engineering H 192 - Computer Programming A Sample Program to Illustrate Switch-Case case ('c')

Engineering H 192 - Computer Programming A Sample Program to Illustrate Switch-Case case ('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 */ Winter Quarter The Ohio State University Gateway Engineering Education Coalition 10

Engineering H 192 - Computer Programming Switch-Case Structures Resultant Output from Grade Program /*

Engineering H 192 - Computer Programming Switch-Case Structures Resultant 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! Winter Quarter The Ohio State University Gateway Engineering Education Coalition 11

Engineering H 192 - Computer Programming Switch-Case Structures break ; • The problems with

Engineering H 192 - Computer Programming Switch-Case Structures break ; • 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. Winter Quarter The Ohio State University Gateway Engineering Education Coalition 12

Engineering H 192 - Computer Programming Fixed Program using Switch-Case Structures #include <stdio. h>

Engineering H 192 - Computer Programming Fixed Program using Switch-Case Structures #include <stdio. h> int main ( ) { int grade ; printf ("Enter your current letter graden") ; while ( ( grade = getchar ( ) ) != EOF) { switch (grade) { case ('a') : case ('A') : printf ("Good Job!n") ; break ; Winter Quarter The Ohio State University Gateway Engineering Education Coalition 13

Engineering H 192 - Computer Programming Fixed Program using Switch-Case Structures case ('b') :

Engineering H 192 - Computer Programming Fixed Program using Switch-Case Structures case ('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 ; Winter Quarter The Ohio State University Gateway Engineering Education Coalition 14

Engineering H 192 - Computer Programming Fixed Program using Switch-Case Structures case ('f') :

Engineering H 192 - Computer Programming Fixed Program using Switch-Case Structures case ('f') : case ('F'): printf ("You are failing!!n") ; break ; case (' ') : case ('n') : //< See note pg 17 break ; default : printf ("Invalid grade. Try again. n") ; } /* End of switch/case */ } /* End of while loop */ } /* End of "main" function */ Winter Quarter The Ohio State University Gateway Engineering Education Coalition 15

Engineering H 192 - Computer Programming Comments on Last Example Program • Use of

Engineering H 192 - Computer Programming Comments on Last Example Program • Use of the while repetition structure -- more discussion on repetition structures later this week. • Use of the end-of-file, or EOF, test. Note that EOF (a DEFINED constant) is a negative integral value, usually a -1 on most (but not all) systems. (EOF is actually defined in the <stdio. h> header file. ) • Use of ints (instead of chars). Why? • From the keyboard, a <return> <cntrl-d> generates an EOF signal on most UNIX systems. Winter Quarter The Ohio State University Gateway Engineering Education Coalition 16

Engineering H 192 - Computer Programming Comments on Last Example Program • The statements:

Engineering H 192 - Computer Programming Comments on Last Example Program • The statements: case (' ') : case ('n') : break ; were used to clear the keyboard input buffer. • Another way to clear it is with the statement: fgets(input_flush, 256, stdin); where fgets(char_strg, len_char_strg, file_pointer); • This fgets statement can prove very useful in today’s daily assignment. • The two sample programs which follow show why flushing the input stream is important Winter Quarter The Ohio State University Gateway Engineering Education Coalition 17

Engineering H 192 - Computer Programming Flawed Sample Character Input Program • • •

Engineering H 192 - Computer Programming Flawed Sample Character Input Program • • • #include <stdio. h> Int main() { char ans 2; while(ans 2 != ‘E’) // Here ‘E’ is a character constant { printf(“n Input a character followed by <enter>”); ans 2 = getchar(); // getchar takes one char from input buffer // and leaves the <enter> in the buffer printf(“ ans 2 >> %c <<n”, ans 2); } } This program runs until you put in an E. This program does not handle multiple inputs correctly. Winter Quarter The Ohio State University Gateway Engineering Education Coalition 18

Engineering H 192 - Computer Programming Output from Flawed Sample Program input a char

Engineering H 192 - Computer Programming Output from Flawed Sample Program input a char followed by <enter> e ans 2 = >> e << input a char followed by <enter> ans 2 = >> << In this case a newline character which is produced by the <return> or <enter> is read by getchar on the second time through the loop Winter Quarter The Ohio State University Gateway Engineering Education Coalition 19

Engineering H 192 - Computer Programming Correct Sample Input Program • • • •

Engineering H 192 - Computer Programming Correct Sample Input Program • • • • #include <stdio. h> Int main() { char ans 2, input_flush[256]; while(ans 2 != ‘E’) { printf(“n Input a character followed by <return>”); ans 2 = getchar(); fgets(input_flush, 256, stdin); // Input buffer is flushed // The <return> is not in buffer printf(“ ans 2 >> %c <<n”, ans 2); } } Winter Quarter The Ohio State University Gateway Engineering Education Coalition 20

Engineering H 192 - Computer Programming Output from Correct Input Program input a char

Engineering H 192 - Computer Programming Output from Correct Input Program input a char followed by <enter> e ans 2 = >> e << input a char followed by <enter> f ans 2 = >> f << Winter Quarter The Ohio State University Gateway Engineering Education Coalition 21

Engineering H 192 - Computer Programming Comments on Last Example Program • Example of

Engineering H 192 - Computer Programming Comments on Last Example Program • Example of use of fgets(input_flush, 256, stdin); char figure, input_flush[256] ; float size ; printf ("Enter figure type>") ; scanf ("%c", &figure) ; //or figure=getchar( ) ; fgets(input_flush, 256, stdin); printf ("Enter size of figure>") ; scanf ("%f", &size) ; fgets(input_flush, 256, stdin); Winter Quarter The Ohio State University Gateway Engineering Education Coalition 22

Engineering H 192 - Computer Programming Assignment G 08 • Use a switch-case structure

Engineering H 192 - Computer Programming Assignment G 08 • Use a switch-case structure to select from among the shapes for which calculations are to be made. • May use just first character of shape name to select which calculation to make. • Program only does one shape, and then exits. No looping required for today's assignment. The Ohio State University Gateway Engineering Education Coalition 23