0

In switch can I show the condition like I do with 'if else'?

In switch can I show the condition like I do with 'if else'? For example if (a>18){ cout <<"you can use this app"<<endl; } else { cout <<"you can't use this app"<<endl; } Here after "if" I used condition (a>18). My question is can I show smth like that (a>18) in 'switch' ?

21st Apr 2018, 5:28 PM
Shosalim Baxtiyorov
Shosalim Baxtiyorov - avatar
2 odpowiedzi
+ 2
switch doesn't work with ranges of values. only int and string. You could do something like: switch (a) { case 1: case 2: case 3: case n: case 17: cout <<"you can't use this app"<<endl; break; default: cout <<"you can use this app"<<endl; break; } But that is a bit ridiculous and certainly not good code practice. Stick with if/elseif/else for situations like yours. Is there a particular reason you want it to be a switch? (also do you want the condition to be older than 18 or 18 and older? you have a > 18 which does not include 18.)
21st Apr 2018, 5:34 PM
Adam
Adam - avatar
0
Thank you a lot!
21st Apr 2018, 5:45 PM
Shosalim Baxtiyorov
Shosalim Baxtiyorov - avatar