About Switch Statement

Can i use 2 or more expression in switch statement??
like..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62


switch(order1)(order2)(order3(order4)(order5)
{
case 1:
cout<<"Large Pepperoni Pizza  =P200\n";
break;
case 2:
cout<<"Medium Pepperoni Pizza =P150\n";
break;
case 3:
cout<<"Small Pepperoni Pizza  =P100\n";
break;
case 4:
cout<<"Large Mushroom  Pizaa  =P200\n";
break;
case 5:
cout<<"Medium Mushroom  Pizza =P150\n";
break;

case 6:
cout<<"Coke  =P15\n";
break;
case 7:
cout<<"Royal  =P15\n";
break;
case 8:
cout<<"Sprite  =P15\n";
break;
case 9:
cout<<"Pine Apple Juice  =P20\n";
break;
case 10:
cout<<"Apple Juice  =P20\n";
break;

case 11:
cout<<"Beef Barbeque  =P30\n";
break;
case 12:
cout<<"Pork Barbeque  =P20\n";
break;
case 13:
cout<<"Chicken Barbeque  =P25\n";
break;
case 14:
cout<<"Spaghetti  =P60\n";
break;
case 15:
cout<<"Carbonara  =P80\n";
break;

case 16:
cout<<"Fruit Salad  =P75\n";
break;
case 17:
cout<<"Macaroni Salad  =P50\n";
break;

default:
cout<<"Wrong Order Code !\n";
}
You can't as you've written it, and I'm not certain what you're looking for. Each of the cases corresponds to the value of the expression you are switching on, so what would a case mean for multiple expressions?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
switch ( order1 )
{
   case 1:
   // ...
   break;

   case 2:
   // ...
   break;

   default:
   switch ( order2 )
   {
      case 1:
      // ...
      break;

      case 2:
      // ...
      break;

      default:
      switch ( order3 )
      {
      case 1:
      // ...
      break;

      default:
      // ...
      break;
      }
      break;
   }
   break;
} 
closed account (z05DSL3A)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int main () 
{ 
    int orders[5] = {1,2,3,4,5};

    for( int index = 0 ; index < 5; index++)
    {
        switch(orders[index])
        {
            case 1:
                std::cout<<"Large Pepperoni Pizza  =P200\n";
                break;
            case 2:
                std::cout<<"Medium Pepperoni Pizza =P150\n";
                break;
            case 3:
                std::cout<<"Small Pepperoni Pizza  =P100\n";
                break;
            case 4:
                std::cout<<"Large Mushroom  Pizaa  =P200\n";
                break;
            //...
            default:
                std::cout<<"Wrong Order Code !\n";
        }
    }
    return 0; 
}
??
@vlad form moscow
@Grey Wolf


Can't we use it like this:
1
2
3
4
5
6
7
8
9
10
11
12
.
.
.
switch(order1 || order2 || order3 || order4 || order5)
{
   .
   .  // cases go here
   .
}
.
.
.
closed account (z05DSL3A)
Hmm, That would give you a bool switch condition so the cases would not work passed a true , false, default...
Rather than a switch statement, I think a list of strings would be more useful:
1
2
3
4
5
6
7
8
9
10
11
12
13
std::vector<std::string> options(numberOfOptions);
options[0] = "Large Pepperoni Pizza  =P200";
options[1] = "Medium Pepperoni Pizza =P150";
options[2] = "Small Pepperoni Pizza  =P100";
// ...
std::vector<int> orders(numberOfOrders, -1); // Set orders to "nothing".
// Read orders into vector.
// ...
// Print out orders.
for (auto it = 0; it < orders.size(); ++it) {
    if (orders[it] == -1) continue; 
    std::cout << options[orders[i]] << std::endl;
}
Topic archived. No new replies allowed.