Sorry to post again today, I tried searching the forums for a similar issue but couldn't find a thread..
Right now I'm learning the switch case (lesson 5 ) and instead of repeatedly using case statements like this:
Code:
switch(example)
{
case 1:
break;
case 2:
break;
case 3:
break;
...etc
}
I'm trying to use a loop for simplifying this code, but it's not working out well..
My initial question is, can we even use loops with case?
This is an attempt:
Code:
printf("Enter a number less than 10:");
scanf("%d\n", &tester);
if(tester >= 10)
{
printf("Learn to follow instructions!");
}
else
{
switch(tester)
{
for (j=0;j<10;j++) //error 1
{
case j: //error 2
printf("This number is a +%d reference from 0", j);
break;
}
}
}
it's not the best style and its probably sloppy(like I dont even have a default case), but i'm just trying to learn the concept for simplifying the code
I get 2 errors:
1. unreachable code at beginning of switch statement
2. case label does not reduce to an integer constant
any suggestions would be nice