Lecture 09 - Switch Case Structures - 08

Lecture 09 - Switch Case Structures - 08 - Engineering H192...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Lect 9 P. 1 Engineering H192 - Computer Programming Winter Quarter The Ohio State University Gateway Engineering Education Coalition Switch Case Structures Lecture 9
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lect 9 P. 2 Engineering H192 - Computer Programming Winter Quarter The Ohio State University Gateway Engineering Education Coalition 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.
Background image of page 2
Lect 9 P. 3 Engineering H192 - Computer Programming Winter Quarter The Ohio State University Gateway Engineering Education Coalition Switch-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!
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lect 9 P. 4 Engineering H192 - Computer Programming Winter Quarter The Ohio State University Gateway Engineering Education Coalition 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
Background image of page 4
Lect 9 P. 5 Engineering H192 - Computer Programming Winter Quarter The Ohio State University Gateway Engineering Education Coalition 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.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lect 9 P. 6 Engineering H192 - Computer Programming Winter Quarter The Ohio State University Gateway Engineering Education Coalition 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.
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 08/05/2011 for the course H 192 taught by Professor Glimcher during the Winter '08 term at Ohio State.

Page1 / 22

Lecture 09 - Switch Case Structures - 08 - Engineering H192...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online