if / else和if / elseif

问题描述 投票:6回答:10

如果我有这样的语句块:

if (/*condition here*/){ }
else{ }

或者像这样:

if (/*condition here*/)
else if (/*condition here*/) {}
else if (/*condition here*/) {}

有什么不同?

似乎使用if / else,如果part为true状态,​​else部分为所有其他可能选项(false)。一个else-if对许多条件都有用。这是我的理解,还有什么我应该知道的吗?

programming-languages syntax if-statement
10个回答
14
投票

情况a:

if( condition )
{
}
else
{
}

当上述语句中的条件为false时,将始终执行else块中的语句。

情况b:

if( condition )
{
}
else if( condition2 )
{
}
else
{
}

当'condition'为false时,else if块中的语句只会在condition2为true时执行。当condition2为false时,将执行else块中的语句。


0
投票
if (a == 2) { do one thing };
if (a == 3) { do another thing };
...
if (a != 2 && a != 3 ...) { do something else };

21
投票

如果没有“elseif”语法,您必须编写链式if语句,以便以这种方式处理以下几种可能结果之一:

if( str == "string1" ) {
   //handle first case
} else {
    if( str == "string2" ) {
       //handle second case
    } else {
       if( str == "string3" ) {
           //handle third case
       } else {
          //default case
       }
    }
 }

相反,你可以写

if( str == "string1" ) {
   //handle first case
} else if( str == "string2" ) {
   //handle second case
} else if( str == "string3" ) {
   //handle third case
} else {
   //default case
}

这与前一个完全相同,但看起来更好,更容易阅读。


3
投票

许多语言都有这样的语法(这里:qazxsw poi,所以JavaScript):

如果声明: ECMAScript Language Specification表达if (声明)声明 qazxsw poi表达qazxsw poi声明

声明: 块 VariableStatement EmptyStatement ExpressionStatement IfStatement IterationStatement Continue语句 BreakStatement ReturnStatement WithStatement LabelledStatement SwitchStatement ThrowStatement TryStatement

块: else StatementListopt if (

StatementList: 声明 StatementList语句

所以if语句的分支可能包含一个语句块(Block)或一个其他语句(Block除外)。这意味着这是有效的:

)

由于StatementList可能只包含一个语句,因此这些示例与前一个示例相同:

{

当我们用额外的IfStatement替换}时,我们得到:

if (expr)
    someStatement;
else
    otherStatement;

其余的只是代码格式化:

if (expr) {
    someStatement;
} else {
    otherStatement;
}

if (expr)
    someStatement;
else {
    otherStatement;
}

if (expr) {
    someStatement;
} else
    otherStatement;

3
投票

强调Gumbo所说的话。

此外,如果一种语言有一个真正的elif / elsif / elseif(比如说,一个“真正的”else-if指令,而不是一种隐藏在格式化之外的嵌套链接),那么编译器可以轻松地在一个抽象中发出一个节点语法树(或类似的,请参阅otherStatement)而不是嵌套它们。

举个例子:

用C / C ++说你有:

if (expr) {
    someStatement;
} else
    if (expr) {
        someOtherStatement;
    }

然后编译器将构建一个这样的AST节点:

if (expr) {
    someStatement;
} else if (expr) {
    someOtherStatement;
}

但如果选择的语言有一个真实的if-else:

http://en.wikipedia.org/wiki/Abstract_syntax_tree

然后AST可以更容易看起来像这样:

if (a) {
    X
} else if (b) {
    Y
} else if (c) {
    Z
} else {
    0
}

在这种语言中,只有在大括号不是强制性的情况下才能使用“if else”:

   a
  / \
 X   b
    / \
   Y   c
      / \
     Z   0

相应的AST(如果括号不是强制性的):

if (a) {
    X
} elif (b) {
    Y
} elif (c) {
    Z
} else {
    0
}

这可以使CFG-Analysis( (a--b--c) / / / \ X Y Z 0 )更容易实现(尽管可能没有实际的优化优势;因此,它只会让懒惰的程序员受益:D)。


2
投票

if (a) { X } elif (b) { Y } else if (c) { // syntax error "missing braces" if braces mandatory Z } else { 0 } 基本上意味着 (a--b) / / \ X Y c / \ Z 0 http://en.wikipedia.org/wiki/Control_flow_graph部分是另一个else if声明。


2
投票
else

if / else if / else

if

if / else和if / else如果也使用这样的话


0
投票

给定一个变量,您将使用简单的if结构。当存在多个变量且您有不同的路径可以执行不同的可能性时,您将使用**if/else** if(condition) statement; else statement; 。注意,后者也以if(condition) { if(condition) statement; else statement; } else if(condition) { if(condition) statement; else statement; } else statement; 语句结束。


0
投票

你自己已经给出了答案。 if / else是for true / false结果,如int = 2或任何其他可能的int值,if / elseif是否超过2个结果,如int = 2,int = 3等等。

它还将变量的上下文分组。你可以检查每一个结果

if-else

使用if / else / elseif它更具可读性。


0
投票

如果你想查看更多条件我们可以使用if..elseif。单一条件然后我们可以使用if或if ... else。 在这里,我无法通过示例上传完整的说明,因此请通过以下链接。 if..else语句详细信息 if-else if-...-else if ... elseif详情 else

© www.soinside.com 2019 - 2024. All rights reserved.