switch() statement

Pretty easy question. Are you able to use && or || in a case value?

ie.

1
2
3
4
switch(i)
{
     case : 1 || 2 || 3 : do something;
}
Why not try? ;)

No you can't, but there is a possibility for something similiar to ||. && makes no sense because the switch is using only one value.
1 || 2 || 3 makes no sense since it is equal to 1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
switch(i){
 case 1:
 case 2:
 case 3:
  // my code if(i == 1 || i == 2 || i == 3)
 break;
 case 28:
 case 33:
  // my code if(i == 28 || i == 33)
 break;
 case 545:
  // my code if(i == 545)
 break;
 default:
  // default (else) code
}
Last edited on
I see. I havent started coding with it yet. I was just doing text exercises. but that makes sense. Thanks!
Topic archived. No new replies allowed.