cplusplus.com cplusplus.com
cplusplus.com   C++ : Forums : Beginners : Can you choose more than one case ?
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Forums
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Articles
Lounge
Jobs

-

post  Can you choose more than one case ?

katielynnsdad (34)
can you choose more than one case in a switch statement?
Or, how can you choose witch dice to keep in a dice game?
Say you roll five dice and you need to keep 3 at different places in your array of dice?
|
msram (19)
@katielynnsdad:
I understood only the first question. And the answer to that is 'yes'. It is possible to execute more than one cases in a switch statement. In the switch statement, once a case condition is met, the execution flow continues till it sees a break statement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
switch(i)
{
     case 1:
     case 2:
                printf(" 1 or 2 ");
                break;
     case 3:
     case 4:
                printf(" 3 or 4 ");
                break;
     case 5:
     case 6:
                printf(" 5 or 6");
                break;
     default:
               printf("i is some thing other than 1,2,3,4,5 and 6.");
}
|
ropez (312)
@msram:
I think what he meant was something like:
1
2
3
4
5
6
7
8
9
10
11
switch(i)
{
    case 1:
    case 2:
        // A
        break;
    case 1:
    case 3:
        // B
        break;
}


If i equals 1, then choose both case "A" and case "B". In general this isn't possible.
| Last edited on
msram (19)
@ropez: Not yet sure. Let's see what the original author says! :)
| Last edited on

This topic is archived - New replies not allowed.
Home page | Privacy policy
© cplusplus.com, 2000-2008 - All rights reserved - v2.2
Spotted an error? contact us