LECTURE_11_CS113_2010 - The switch Statement THE switch...

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

View Full Document Right Arrow Icon
The switch Statement THE “ switch ” STATEMENT: • Provides a convenient way to execute conditional code when there are many cases to choose from. • This construct can replace series of if- else-end statements
Background image of page 1

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

View Full DocumentRight Arrow Icon
LOGICAL PROGRAMMING CONSTRUCTS SYNTAX: switch expression (scalar or string) case value1 statement group 1 case value2 statement group 2 otherwise statement group n end
Background image of page 2
EXAMPLE for “switch” • Suppose you are given a variable angle in degrees to represent the following directions: Northeast, Southeast, Southwest, and Northwest. • Use the switch statement to display the desired direction given the angle.
Background image of page 3

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

View Full DocumentRight Arrow Icon
SOLUTION switch angle case 45 disp('Northeast') case 135 disp('Southeast') case 225 disp('Southwest') case 315 disp('Northwest') otherwise disp('Direction Unknown') end Decision variable name Value to test Default case
Background image of page 4
EXAMPLE #2 for “switch” • You input a numerical value of a quantity in one set of units (e.g., centimeters) and you desire an output in another set of units
Background image of page 5

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

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 14

LECTURE_11_CS113_2010 - The switch Statement THE switch...

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

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