Switch Statement Questions.

I'm learning about switch statements and came across this in my book:

"The switch statement is different from similar statements in languages such as Pascal in a very
important way. Each C++ case label functions only as a line label, not as a boundary between
choices. That is, after a program jumps to a particular line in a switch, it then sequentially
executes all the statements following that line in the switch unless you explicitly direct it otherwise.
Execution does not automatically stop at the next case. To make execution stop at the
end of a particular group of statements, you must use the break statement. This causes execution
to jump to the statement following the switch."

- excerpt from C++ Primer Plus 5th Ed. by Stephen Prata

Does this mean that if I have this snippet of code:
1
2
3
4
5
6
7
8
9
switch (num)
{
case 1 : cout << "GoodBye"
break;
case 2 : cout << "Good Morning"
break;
case 3 : cout << "Good Afternoon"
break;
default : cout << "Good Evening";


and omit the breaks, that it will go through all the switch cases that come after the triggered one.
Yes. Couldn't you just have removed the breaks and compiled to see what happens, though?
Take out the break then the switch statement will serve no purpose =.=
I'm can't compile on this computer. Am not able to access cmd, so no. I could when I got home, but I was curious. :)
so what do u think will happen?
I've been to plenty of schools that lock out the command prompt, what they don't do is prevent you from writing batch files and executing your code that way ;)
lol....they scared you will go use the cmd to get information on their networking devices :)
closed account (zb0S216C)
The book is correct. If you miss-out thebreak clause after each case, the compiler will execute all the cases until a break clause is found.
Last edited on
Topic archived. No new replies allowed.