In this piece of code the switch statement is evaluated only one time or not?
Code:const int n=1; while(1) { switch( n ) { case 1: { printf("1\n"); break; } case 2: { printf("2\n"); break; } } }
In this piece of code the switch statement is evaluated only one time or not?
Code:const int n=1; while(1) { switch( n ) { case 1: { printf("1\n"); break; } case 2: { printf("2\n"); break; } } }
Last edited by frs; 09-05-2010 at 12:24 PM.
a break in a switch only breaks from that switch-case.
The code does not make sense. Why do you declare opt as const but never initialise it, and then go on to use it?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Right. I suspect that rags_to_riches has guessed the real question that you are asking. If not, I can tell you that the code either will not compile, or we cannot say, since it depends on the input and what exactly is the "do something" and "do something else".
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Edit Again.
Maybe I haven't expressed myself well.
Since n is a const variable, the compiler replaces the 'while' content into printf("1\n") only?
What do you mean? Ignore compiler optimisations: do you understand what your code snippet does? Tell us.Originally Posted by frs
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Are you saying that you believe the compiler optimizes away the rest of the switch case because the use of a constant guarantees that only one case will be entered?
EDIT: Beaten by laserlight. This seems like another straight-up exam/quiz/homework question.
Excellent. Now, observe that the switch's conditional expression is evaluated once on every iteration. Since we are dealing with an infinite loop, the answer to your question must be a no. That was easyOriginally Posted by frs
If you want to bring in compiler optimisations, then it seems to me that the best that can be achieved in optimisation is to write it akin to:
Look ma, no switch! But will this happen? It depends on the compiler and the options used. (Oh, and the answer to your question will still be a no, since the switch's conditional expression is then evaluated zero times.)Code:while (1) { printf("1\n"); }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I'm against just giving people the answers to these kinds of questions, that's what. I'm funny and old fashioned that way.
In my mind you were given the question by your teacher because you were expected to be able to answer it on your own, either by applying the knowledge received through your class or your textbook or by otherwise researching it. In the latter case, I'm pretty sure research is not equivalent to running to a forum and get someone to hand you the answer; it's tantamount to asking the person sitting next to you in the exam, or copying another person's homework.
Last edited by rags_to_riches; 09-05-2010 at 12:58 PM. Reason: Fixed quote
The idea was to assign an input parameter to the 'const char' , so it would have to be evaluated at least one time.Look ma, no switch! But will this happen? It depends on the compiler and the options used. (Oh, and the answer to your question will still be a no, since the switch's conditional expression is then evaluated zero times.)
And the point of all this is to...?Originally Posted by frs
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
The compiler will see two things
1) n is const. It can safely replace n with 1, rather read from a variable read from n. As lasersight said it depends on compiler/options. But you can imagine that "it is a good idea to do so"
2) You check a switch statement with a constant. I would think that some compilers will give a warning about this. Like "why do you switch with something constant?". Again, depends but it seems a "good idea" to warn. Well, if it went to that point, it probably will optimize the code if optimization are enabled.
Now how will it optimize? It might do
Then it will have to think that some code is never going to be executed and not have it (note that break is not mandatory to exist in a case). So finally have what lasersight posted.Code:{ printf("1\n"); goto ATRIP; printf("2\n"); goto ATRIP: } ATRIP: ...
Again, it might give you a warning also about this like "Unreachable code". So two warnings. One because you check a constant, two because some code is unreachable.
The thing is that Compilers are NOT standardize. They can do whatever they want as long as the follow the standards of C. Or at least the standard.
So my final answer: it depends of the compiler. Meaning you should NOT assume anything. Once you have found the compiler you would have to specify the options you use (or just say default). Then you can get a specific answer.