Does any one has any idea if the while statement in the following code is ever executed.
Code:switch XYZ
{
while(1)
{
case X:
-------
-------
return p;
case Y:
-------
-------
return q;
}
}
Printable View
Does any one has any idea if the while statement in the following code is ever executed.
Code:switch XYZ
{
while(1)
{
case X:
-------
-------
return p;
case Y:
-------
-------
return q;
}
}
If that is representative of your real code, then anything that isn't X or Y when you enter the switch, will just loop around within the while-loop, since there is no way out of that loop other than in the case X and case Y options.
--
Mats
ok, I understood.
Now my problem is I am getting a warning when compiling this code.
"loop is not reachable from preceding code".
Is there a way to fix this warning? Any ideas??
What are you ACTUALLY trying to achieve? Normally, you do not put a switch, then a while, and then case-labels inside that, so it would help to understand what you want to achieve...
--
Mats
This compiles, runs and produces expected output. I don't see what would be the purpose of that, though.
Code:#include <stdio.h>
int foo(int n)
{
switch (n) {
while (1) {
case 1:
return 2;
case 2:
return 4;
case 3:; //a case that doesn't return
}
}
return 7;
}
int main()
{
printf("%d %d %d %d", foo(1), foo(2), foo(3), foo(4));
return 0;
}
Yes, it is VALID to do that. There is an example of a piece of code that puts case-labels in an if-statement in the Samuel P. Harbison "C - A reference manual" [at least, there was one in my older copy].
--
Mats
yes but u have to declare the return values if u dunt want one of them to break out of the loop
but i prefer forever loops for infinite loops