Originally Posted by
ssharish2005
Sample Code
Code:
#include<stdio.h>
int main()
{
switch(1)
{
case 1:
printf("One\t");
case 2:
printf("Two\t");
case 3:
printf("Three\t");
case 4:
printf("Four\t");
}
getchar();
return 0;
}
/* myoutput
One Two Three Four
*/
ssharish2005
That is called "fall through" and I am not sure if that is what he really wants with "run forever" as this will run one time only.
However, this will run forever. The switch block is inside of the while block that runs forever.. that gives you a chance to read a new value into n or compute n, etc. Otherwise, what would be the point?
Code:
while ( 1 ) { /* Run forever. . . */
/* variable n can be computed here, etc. */
switch ( n ) {
case 0:
/* code */
break;
case 1:
/* code */
break;
case 2:
/* code */
break;
}
}