Could I do something like this with C/C++?
var a = 8;
switch(a){
case a>=12:
console.log("a");
break;
case a>1 || a<6:
console.log("aa");
break;
default:
console.log("aaa");
break;
}
I tried something similar in C/C++ but I get an error when compiling, something like:
the value of 'a' cannot be used in a constant expression note: 'int a' is not a const
The code I tried was something like:
int miVar;
char letra;
cout<<"Digite el valor:"<<endl;
cin>>miVar;
switch(miVar){
case miVar<=30000:
letra = 'A';
break;
case (miVar>30000 & miVar<=60000):
letra = 'B';
break;
default:
letra = 'C';
break;
}
You can't, you have to use a constant value (defined at compile time).
The error it shows you is because it tries to interpret the expression you pass to it as a constant and it can't.
Also, even if a were a constant, what it would do to you would be to evaluate the expression on compilation and give you a boolean value, and that fixed value would be what it would compare. Don't use a curve because in C/C++ booleans are actually integer : the result would be equivalent to case 0 (if the expression evaluated to false ) or case [number other than 0] if the expression evaluated to true .
The only thing similar to an or would be to put the values in several cases and not put a break between them .