Code:main() { int x=0; switch(x) { case 1: printf( "One" ); case 0: printf( "Zero" ); case 2: printf( "Hello World" ); } }
Code:main() { int x=0; switch(x) { case 1: printf( "One" ); case 0: printf( "Zero" ); case 2: printf( "Hello World" ); } }
Because it does. In fact, it will print "OneZeroHello World".
>> Why is it that my program does not print for case 1?
Because it doesn't equal 1, maybe?
>> In fact, it will print "OneZeroHello World".
It'll actually skip the first case label, so the output would be "ZeroHello World".
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
Why would you expect it to? Fall-through doesn't mean it always hits every case label. It means if it matches one, and there's no fall-through protection (break), it will do everything that comes after it until it finds a reason to stop.
Quzah.
Hope is the first step on the road to disappointment.
When it falls through, it falls down, not up.
Set x=1 and it'll print them all.
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008
"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010
I think the problem was to illustrate the out of order sequence of the case statements and how it will effect the outcome. I think he was suppose to test the code and learn. Personly instead of changing x to equal 1 I would simply change the case 1: to case 0: and case 0: to case 1: keeping the outputs in the same order. If the point was to get all three to print. :P
Like :Code:main() { int x=0; switch(x) { case 0: printf( "One" ); case 1: printf( "Zero" ); case 2: printf( "Hello World" ); } }
I hope all of your code doesn't end up as "clear" as this.case 0: printf( "One" );
case 1: printf( "Zero" );
Quzah.
Hope is the first step on the road to disappointment.
Jus a nitpick, but you have things backwards - case 0 is printing 'one' and case 1 is printing 'zero' in the "fixed" code.