Lecture 09 - Switch Case Structures - 08

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

Info icon This 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
Image of page 1

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

View Full Document Right 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.
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!
Image of page 3

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

View Full Document Right 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
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.
Image of page 5

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

View Full Document Right 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.
Image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern