CHAP09 - Chapter 9 Additional Control Structures...

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

View Full Document Right Arrow Icon
1 Chapter 9 Additional Control Structures Dale/Weems/Headington
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using break and continue Statements
Background image of page 2
3 Switch Statement Is a selection control structure for multi-way branching. SYNTAX switch ( IntegralExpression ) { case Constant1 : Statement(s); // optional case Constant2 : Statement(s); // optional . . . default : // optional Statement(s); // optional }
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 float weightInPounds = 165.8 ; char weightUnit ; . . . // user enters letter for desired weightUnit switch ( weightUnit ) { case ‘P’ : case ‘p’ : cout << weightInPounds << “ pounds “ << endl ; break ; case ‘O’ : case ‘o’ : cout << 16.0 * weightInPounds << “ ounces “ << endl ; break ; case ‘K’ : case ‘k’ : cout << weightInPounds / 2.2 << “ kilos “ << endl ; break ; case ‘G’ : case ‘g’ : cout << 454.0 * weightInPounds << “ grams “ << endl ; break ; default : cout << “That unit is not handled! “ << endl ; break ; }
Background image of page 4
5 Switch Statement the value of IntegralExpression (of char, short, int, long or enum type ) determines which branch is executed case labels are constant ( possibly named ) integral expressions. Several case labels can precede a statement
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 Control in Switch Statement control branches to the statement following the case label that matches the value of IntegralExpression . Control proceeds through all remaining statements, including the default, unless redirected with break if no case label matches the value of IntegralExpression , control branches to the default label, if present--otherwise control passes to the statement following the entire switch statement forgetting to use break can cause logical errors because after a branch is taken, control proceeds sequentially until either break or the end of the switch statement occurs
Background image of page 6
Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the
Background image of page 7

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

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

Page1 / 40

CHAP09 - Chapter 9 Additional Control Structures...

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

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