Switch Case Structures in C: Multiple Selection with Examples, Slides of Computer Engineering and Programming

The concept of switch case structures in c programming, which is useful when an algorithm contains a series of decisions based on the integral value of a variable or expression. Examples and a sample program to illustrate the usage of switch case structures.

Typology: Slides

2012/2013

Uploaded on 05/06/2013

apsara
apsara 🇮🇳

4.5

(2)

86 documents

1 / 23

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Switch Case Structures
Lecture 9
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17

Partial preview of the text

Download Switch Case Structures in C: Multiple Selection with Examples and more Slides Computer Engineering and Programming in PDF only on Docsity!

Switch Case Structures

Lecture 9

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.

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

Switch-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.

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
  7. Say “You are failing”
  8. Terminate program

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 grade\n") ; grade = getchar ( ) ;

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 */

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. Docsity.com

Fixed Program using Switch-Case

#include <stdio.h> Structures

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 ;

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 ; Docsity.com

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 <cntrl-d> Docsity.com

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

Output from Flawed Sample Program

input a char followed by e

ans2 = >> e <<

input a char followed by ans2 = >>

In this case a newline character which is produced by the^ << or is read by getchar on the second time through the loop

Correct Sample Input Program

  • #include <stdio.h>
  • Int main()
  • {
  • char ans2,input_flush[256];
  • while(ans2 != ‘E’)
  • {
  • printf(“\n Input a character followed by ”);
  • ans2 = getchar();
  • fgets(input_flush,256,stdin); // Input buffer is flushed
  • // The is not in buffer
  • printf(“ ans2 >> %c <<\n”,ans2);
  • }
  • }