-
January 1st, 2013, 09:59 PM
#1
A question regarding switch/case
Here is the code,
Code:
int main()
{
int i=4;
switch(i)
{
default:printf("zero\n");
case 1: printf("one\n");
break;
case 2:printf("two\n");
break;
case 3: printf("three\n");
break;
}
return 0;
}
Why does the output result include "one"? Thanks.
-
January 1st, 2013, 11:09 PM
#2
Re: A question regarding switch/case
Originally Posted by LarryChen
Why does the output result include "one"? Thanks.
What did you expect to print? Where is the break statement for the default case?
Regards,
Paul McKenzie
-
January 1st, 2013, 11:30 PM
#3
Re: A question regarding switch/case
Originally Posted by Paul McKenzie
What did you expect to print? Where is the break statement for the default case?
Regards,
Paul McKenzie
Actually, I expected the output to be just "zero". Since i is 4, so it doesn't satisfy case 1.
-
January 2nd, 2013, 01:13 AM
#4
Re: A question regarding switch/case
Originally Posted by LarryChen
Actually, I expected the output to be just "zero". Since i is 4, so it doesn't satisfy case 1.
Again, where is the break statement for the default case? Do you know why break makes a difference?
Regards,
Paul McKenzie
-
January 2nd, 2013, 07:01 AM
#5
Re: A question regarding switch/case
Originally Posted by LarryChen
Actually, I expected the output to be just "zero". Since i is 4, so it doesn't satisfy case 1.
And if you set i to equal 1, and your case 1 didn't include a break statement, what do you think the output would be?
-
January 2nd, 2013, 09:22 AM
#6
Re: A question regarding switch/case
Originally Posted by Paul McKenzie
Again, where is the break statement for the default case? Do you know why break makes a difference?
Regards,
Paul McKenzie
I thought case statement is like if statement. So in my example, when i is 4 and it meets case 1, then case 1 is not going to be entered. Why am I wrong? Thanks.
-
January 2nd, 2013, 09:54 AM
#7
Re: A question regarding switch/case
Originally Posted by LarryChen
I thought case statement is like if statement. So in my example, when i is 4 and it meets case 1, then case 1 is not going to be entered. Why am I wrong? Thanks.
That's been answered twice by Paul and once by me already. Basically a switch statement starts at the first case that meets the expression and keeps going till it hits a break.
-
January 2nd, 2013, 03:05 PM
#8
Re: A question regarding switch/case
Originally Posted by LarryChen
I thought case statement is like if statement. So in my example, when i is 4 and it meets case 1, then case 1 is not going to be entered. Why am I wrong? Thanks.
Larry, don't you see your reasoning is faulty?
OK, so what does break do? What purpose does it serve if, by your reasoning, once a case condition is met, the switch is exited as soon as the next case condition is met? If that's your conclusion, then why not just remove the break statements from case 1, case 2, and case 3? It's just unnecessary typing, according to your conclusion.
The answer is as GCDEF mentioned -- break does mean something in a case statement, and that is to ensure that the condition doesn't "fall through" to the next case condition.
Regards,
Paul McKenzie
-
January 4th, 2013, 03:49 AM
#9
Re: A question regarding switch/case
This "fall through" into the next case behaviour is sometimes a very handy feature of how the switch in C++ works. (as opposed to other programming languages where this isn't possible)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|