1. ## Simplifying Code

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("&#37;d\n", &tester);

if(tester >= 10)
{
}

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

2. Why do you have the switch at all?

3. well it's not really necessary in this case but i'm just trying to learn it

I made my own example (pretty much any example) just so I can use it

4. From here:
An important thing to note about the switch statement is that the case values may only be constant integral expressions. Sadly, it isn't legal to use case like this:
If it's showing that something won't work, don't copy it and expect it to work.

And I have no idea what you expect a loop to help with.
[edit=2]Oh, wait. I think I see. No you can't do that.

5. Originally Posted by Dave_Sinkula
From here:
If it's showing that something won't work, don't copy it and expect it to work.

And I have no idea what you expect a loop to help with.
[edit=2]Oh, wait. I think I see. No you can't do that.
so if I ever needed case, I can never use a loop to run through cases?

6. You can use a switch in a loop, yes. You can't create a "loop" of "cases".

What you are trying to invent, methinks, is an if statement.

That is, what I think you are implying by this:
Code:
```      switch ( var )
{
for ( i = 1; i < 5; ++i )
{
case i:
foo();
break;
}
}```
Is implemented like this.
Code:
```      if ( var >= 1 && var <= 5 )
{
foo();
}```

7. Originally Posted by Dave_Sinkula
You can use a switch in a loop, yes. You can't create a "loop" of "cases".

What you are trying to invent, methinks, is an if statement.

That is, what I think you are implying by this:
Code:
```      switch ( var )
{
for ( i = 1; i < 5; ++i )
{
case i:
foo();
break;
}
}```
Is implemented like this.
Code:
```      if ( var >= 1 && var <= 5 )
{
foo();
}```
yeah, that makes things much easier then, thanks! ^^