Consider this const declaration of int num
:
int main() {
bool a = true, b = false;
// ...
const int num = a ? (b? 2 : 4) : 4;
std::cout << num;
}
What I want is for const int num
to follow this truth table (which I apologize has been edited from my original question to reflect the pattern in my program):
b
a true false
true 2 4
false 4 2
How to modify the above const int num
declaration to achieve this using the ternary operator? I know how to declare such num
to be const using a lambda function and nested if-statements or switch statenents within the lambda function, but I just wanted to know how to do it using the ternary operator. As a bonus, what if 3 or more such bool values were to be used (with no specific pattern in the truth table)?
You can write the declaration like
At least it is more clear than
Here is a demonstrative program
The program output is