if,else和else之间有什么区别?

Zac*_*ith 7 c

我试图辨别它们之间的区别

if 
else 
else if 
Run Code Online (Sandbox Code Playgroud)

你什么时候使用它们什么时候不使用?

我有大量实例的家庭作业,由于不知道每个实例之间的差异,我遇到了代码错误.

有人可以定义如何使用这些?

Ada*_*son 54

一个if声明如下这种结构:

if (condition)
{
    // executed only if "condition" is true
}
else if (other condition)
{
    // executed only if "condition" was false and "other condition" is true
}
else
{
    // executed only if both "condition" and "other condition" were false
}
Run Code Online (Sandbox Code Playgroud)

if部分是唯一绝对强制的块.else if允许你说"好吧,如果先前的条件不正确,那么如果这个条件是真的......".其他人说"如果上述条件都不正确......"

您可以拥有多个else if块,但只有一个if块,只有一个(或零)else块.

  • 仅供参考:真的没有"其他如果"这样的东西.它只是一个包含if语句的else语句. (9认同)
  • 为了清楚OP:在传递一组if/else语句期间,将执行一个且只有一个语句块.因此,如果`condition`和`other condition`都为真,则只执行条件的代码. (6认同)
  • @pod:在语义上你是对的,但要了解它仍然是一个重要的想法. (2认同)

Mar*_*off 30

If-elseif-else可以写成嵌套的if-else.这些(逻辑上讲)是等价的:

if (A) 
{
    doA();
}
else if (B)
{
    doB();
}
else if (C)
{
    doC();
}
else
{
    doX();
}
Run Code Online (Sandbox Code Playgroud)

是相同的:

if (A) 
{
    doA();
}
else
{
    if (B)
    {
        doB();
    }
    else
    {
         if (C)
         {
             doC();
         }
         else
         {
             doX();
         }
    }
}
Run Code Online (Sandbox Code Playgroud)

其结果是,最终只有一个doA,doB,doC,或doX将被评估.

  • 我很高兴有人指出`else if if不是关键字,只是`else`和`if`的格式化组合 (8认同)

Ray*_*sen 22

**IF** you are confused
 read the c# spec
**ELSE IF** you are kind of confused
 read some books
**ELSE**
 everything should be OK.
Run Code Online (Sandbox Code Playgroud)

:)

  • 阅读C#规范来学习C可能会让人更加困惑...... (12认同)

Mal*_*eur 13

if,else和else if是否所有构造都有助于"分支"代码.基本上,只要您想做出决定,就可以使用它们.

一个例子是'如果天气晴朗,我会去外面.否则,我会呆在里面'

在代码中(忽略额外的东西)

if (sunny) {
  goOutside();
}
else {
  stayInside();
}
Run Code Online (Sandbox Code Playgroud)

如果要添加"附加"条件,可以使用"else if"语句.扩展前面的例子,"如果天气晴朗,我会去外面.如果暴风雨,我会进入地下室,否则我会呆在里面"

在代码中

if (sunny) {
  goOutside();
}
else if (stormy) {
  goDownstairs();
}
else {
  stayInside();
}
Run Code Online (Sandbox Code Playgroud)

编辑部分:

以下是如何编写多个ifs和条件的方法.以下示例至少可以用两种方式编写:

'如果阳光明媚,温暖,就到外面去吧.如果天气晴朗,冷漠,什么也不做

if (sunny) {
   if (warm) {
     goOutside();
   }
   else if (cold) {
     doNothing();
   }
}
Run Code Online (Sandbox Code Playgroud)

要么

if (sunny && warm) {
   goOutside();
}
else if (sunny && cold) {
   doNothing();
}
Run Code Online (Sandbox Code Playgroud)

  • +1使用真实世界的概念,而不仅仅是像`condition`或`a`,`b`等占位符. (2认同)

Dan*_*ral 9

没有" else if".你有以下几点:

if (condition)
    statement or block
Run Code Online (Sandbox Code Playgroud)

要么:

if (condition)
    statement or block
else
    statement or block
Run Code Online (Sandbox Code Playgroud)

在第一种情况下,如果条件为真(不同于0),则执行语句或块.在第二种情况下,如果条件为真,则执行第一个语句或块,否则执行第二个语句或块.

所以,当你写" else if"时,那是一个" else statement",其中第二个语句是一个if语句.如果您尝试这样做,可能会遇到问题:

if (condition)
    if (condition)
        statement or block
else
    statement or block
Run Code Online (Sandbox Code Playgroud)

这里的问题是你想要" else"引用第一个" if",但你实际上指的是第二个.你通过这样做解决这个问题

if (condition)
{
    if (condition)
        statement or block
} else
    statement or block
Run Code Online (Sandbox Code Playgroud)

  • 正是这个问题被称为"悬空ELSE"问题,并且在C开发之前很早就出现在PASCAL(或可能是ALGOL)中,这让Jean Ichbiah和他的团队在Ada采用了完全括号的控制结构. . (3认同)

小智 5

死简单伪代码说明:

/* If Example */
if(condition_is_true){
   do_this
}
now_do_this_regardless_of_whether_condition_was_true_or_false

/*  If-Else Example  */
if(condition_is_true){
    do_this
}else{
    do_this_if_condition_was_false
}
now_do_this_regardless_of_whether_condition_was_true_or_false

/* If-ElseIf-Else Example */
if(condition_is_true){
    do_this
}else if(different_condition_is_true){
    do_this_only_if_first_condition_was_false_and_different_condition_was_true
}else{
    do_this_only_if_neither_condition_was_true
}
now_do_this_regardless_of_whether_condition_was_true_or_false
Run Code Online (Sandbox Code Playgroud)