+ 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.
11 Respostas
+ 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 😀
+ 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;
+ 6
https://code.sololearn.com/cGxb2468gy1x/?ref=app
here is an example of the first method which works here
+ 6
Thx Jay for pointing it out. Sometimes it is very confusing.. 😀
+ 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...
+ 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!
+ 1
aaah thank you so much!!! This is very helpful to me!
+ 1
Thank you! I really appreciate the help