Gary said:
Hi
in order to assign a value to a variable if a switch is true, can I do
this
switch && var=val;
instead of
if( switch == true ) var = val;
and be safe?
Well you can't have a variable called switch because switch is a
keyword
Other than that, your two alternatives have the same effect
[*]. The && operator is guaranteed to operate its left-hand operand
first, and guaranteed only to evaluate its right-hand operand if its
left-hand operand evaluated to true.
Whether you would ever want to obfuscate your code like this is a
different matter ...
[*] The use of && in your first option introduces a sequence point that
is not present in your second option. With more complex expressions,
that sequence point may be significant. To be equivalent in that
respect your second option would have to become
bool flag = (sw == true); // I used sw instead of switch as the
variable name
if (flag) var = val;
Gavin Deane