+ 1

Can you use things like && or || in switch? (c++)

Whenever using the switch command (switch, case, break thing) can you make a case be like, the thing that happens if its for example 1 or 2, instead of just 1? Does that make sense? Im terrible at explaining, sorry. Im also fairly new, and its long since i last even used this app. Thanks. and sorry.

7th Jun 2017, 9:26 PM
marty
marty - avatar
11 odpowiedzi
+ 9
Bit of confusion going on here huh. Annatoli is saying do the logical expressions before getting to the switch statement. Not that they can be used in case statements. 😊 You are both correct 😀
8th Jun 2017, 10:17 AM
jay
jay - avatar
+ 7
no, but you can do the following (dependant on the compiler) some you can do the following case 1 ... 2: //the action break, others you need to stack the cases case 1: case 2: //the action break;
7th Jun 2017, 9:32 PM
jay
jay - avatar
+ 6
https://code.sololearn.com/cGxb2468gy1x/?ref=app here is an example of the first method which works here
7th Jun 2017, 9:35 PM
jay
jay - avatar
+ 6
Thx Jay for pointing it out. Sometimes it is very confusing.. 😀
8th Jun 2017, 10:19 AM
🇺🇸 Anatoli🇧🇪🇪🇺 ,
🇺🇸 Anatoli🇧🇪🇪🇺 , - avatar
+ 5
You can use the && and || condition to actually decide if you are going through the switch case that you build. It would be more interesting if you use it in a if else condition or a while do while. construction. Actually it depends what you want to accomplish. To make a choice condition more than two options your switch case system will execute much faster. Keep this in mind cause a large program could slow down your code dramatically. But perhaps you could explain what you are trying to do, so we could be more helpful...
7th Jun 2017, 9:48 PM
🇺🇸 Anatoli🇧🇪🇪🇺 ,
🇺🇸 Anatoli🇧🇪🇪🇺 , - avatar
+ 4
@Martin: I mean before you enter the switch case loop you can set a logic condition... and even a case can be used to handle && or || why not!
8th Jun 2017, 9:05 AM
🇺🇸 Anatoli🇧🇪🇪🇺 ,
🇺🇸 Anatoli🇧🇪🇪🇺 , - avatar
+ 1
aaah thank you so much!!! This is very helpful to me!
7th Jun 2017, 9:33 PM
marty
marty - avatar
+ 1
Thank you! I really appreciate the help
7th Jun 2017, 9:37 PM
marty
marty - avatar