Is it possible to make a switch statement?

Say I have a an if else statement like:
1
2
3
4
5
6
7
8
9
10
11
12
x = 100;
y = 500;


cin >> amount;

if(amount > x)
....some output...
else if(amount < x)
...some output...
else 
...some output...


then what if I want to do the same thing with y at the same time?

Is it possible to create a switch statement out of this? Because the only switch statements I've created are for a list of options.

1
2
3
4
5
6
7
8
9
10
11
12
x = 100;
y = 500;


cin >> amount;

if((amount > x)&&(amount>y)) 
....some output...
else if((amount < x)&&(amount<y)
...some output...
else 
...some output...


something like that?
http://www.cplusplus.com/doc/tutorial/operators/ for more :)

Cheers!
Last edited on
Not what I would recommend, but what about something like this?

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
short indx;

short eval (int amt, int val)
{ if (amt > val)
      return 0;
   if (amt < val)
     return 1;
  return 2; ! equal 
}

indx = eval(amount, x) * 3 + eval(amount, y);
switch (indx)
{ case 0: cout << "amt>x and amt>y"; break;
   case 1: cout << "amt>x and amt<y"; break;
   case 2: cout << "amt>x and amt=y"; break;

   case 3: cout << "amt<x and amt>y"; break;
   case 4: cout << "amt<x and amt<y"; break;
   case 5: cout << "amt<x and amt=y"; break;

   case 6: cout << "amt=x and amt>y"; break;
   case 7: cout << "amt=x and amt<y"; break;
   case 8: cout << "amt=x and amt=y"; break;
   default: 
}
  cout << endl;
Last edited on
Topic archived. No new replies allowed.