写作方式的不同和QUOT;若"声明 [英] Different ways of writing the "if" statement
本文介绍了写作方式的不同和QUOT;若"声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我看到写的如果
语句的不同方式。
你喜欢,为什么哪一个?
例1:
如果(VAL%2 == 1 ){输出=号是奇;}其他{输出=号甚至是;}
例2:
如果(VAL%2 == 1)
{
输出=号是奇;
}
,否则
{
输出=号甚至是;
}
例3:
如果(VAL%2 == 1)
输出=号是奇;
,否则
输出=号甚至是;
示例4:
如果(VAL%2 == 1){
输出=号是奇;
}其他{
输出=号甚至是;
}
类似的问题:
< BLOCKQUOTE>
块引用>
解决方案
有关这样的情况下,也条件运算符:
输出=(VAL%2 == 1)? 号是奇:号甚至是;
如果你一定要使用一个如果我会使用版本2或4版,这取决于你的支撑风格的其余部分。 (在工作中我使用4;个人项目我用2)最主要的是,有括号甚至围绕单个语句
BTW,测试校验它的咯。更快的使用方法:
IF((VAL&安培; 1)== 1)
I have seen different ways of writing an if
statement.
Which one do you prefer and why?
Example 1:
if (val % 2 == 1){output = "Number is odd";}else{output = "Number is even";}
Example 2:
if (val % 2 == 1)
{
output = "Number is odd";
}
else
{
output = "Number is even";
}
Example 3:
if (val % 2 == 1)
output = "Number is odd";
else
output = "Number is even";
Example 4:
if (val % 2 == 1){
output = "Number is odd";
} else {
output = "Number is even";
}
Similar question:
解决方案
For cases like this, there's also the conditional operator:
output = (val % 2 == 1) ? "Number is odd" : "Number is even";
If you're definitely going to use an "if" I'd use version 2 or version 4, depending on the rest of your bracing style. (At work I use 4; for personal projects I use 2.) The main thing is that there are braces even around single statements.
BTW, for testing parity it's slightly quicker to use:
if ((val & 1) == 1)
这篇关于写作方式的不同和QUOT;若"声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文