逻辑而不是if [英] logic instead of if
问题描述
嗨
如果开关为真,为了给变量赋值,我可以这样做
这个
开关&& var = val;
而不是
if(switch == true)var = val;
并且安全吗?
感谢
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?
thanks
推荐答案
Gary Wessle< ph **** @ yahoo.comwrote in
新闻:m3 ************ @ localhost.localdomain:
Gary Wessle <ph****@yahoo.comwrote in
news:m3************@localhost.localdomain:
嗨
如果一个开关为真,为了给一个变量赋值,我可以这个
开关&& var = val;
而不是
if(switch == true)var = val;
并且安全吗?
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?
首先......不能命名变量switch。这是一个关键词。
其次,你为什么要这样做?首先编写您的代码以使其可读并且
正确。 if表格更清晰易读。
First... can''t name a variable "switch". It''s a keyword.
Second, why would you want to? First write your code to be readable and
correct. The if form is far clearer to read.
Gary Wessle写道:
Gary Wessle wrote:
嗨
如果一个开关为真,为了给一个变量赋值,我可以吗
这个
开关&& var = val;
而不是
if(switch == true)var = val;
并且安全吗?
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?
你不能有一个名为switch的变量,因为switch是一个
关键字:-)除此之外,你的两个选择具有相同的效果[*]。和&&运算符保证首先运行其左侧操作数
,并保证只评估其右侧操作数,如果它的
左侧操作数被评估为真。 />
无论你是否想要混淆你的代码,这是一个不同的事情......
[*]使用&安培;&安培;在你的第一个选项中引入了一个序列点,即第二个选项中没有
。对于更复杂的表达式,
序列点可能很重要。为了等同于
尊重你的第二个选择必须成为
bool flag =(sw == true); //我用sw而不是switch作为
变量名
if(flag)var = val;
Gavin Deane
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
Gary Wessle skrev:
Gary Wessle skrev:
嗨
如果一个开关为真,为了给一个变量赋值,我可以这样做
这个开关&& var = val;
而不是
if(switch == true)var = val;
并且安全吗?
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?
不是一般情况。首先,var = val的结果必须是
可转换为bool,即使它是,转换的结果
可能会产生不良影响。 />
同样重要的是你无缘无故地模糊你的代码。
保持为
if(switch)var = val; //忽略这个例子,你不能用
命名变量switch。
没有理由让比较为真 - 这是另一个
混淆。
/ Peter
Not in the general case. For one thing, the result of var = val must be
convertible to bool, and even if it is, the result of that conversion
might have unwanted effects.
Also important is that you obscure your code for no reason at all.
Keep it as
if (switch) var = val; // disregarding for this example that you can''t
name a variable "switch".
there is no reason to have the comparison to true - this is another
obfuscation.
/Peter
这篇关于逻辑而不是if的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!