C 在其他语句之前缺少语句

C 在其他语句之前缺少语句,c,C,我正在尝试这个基本代码块来熟悉条件语句。我不认为我漏掉了一个括号或任何东西,但是我得到了一个错误,我漏掉了第二个else子句之前的一个语句,但我不理解这一点 #include stdio.h; main() { int a = 2; int b = 4; int c = 6; int d = 8; if ( a > b ) { a = a - 1; printf("a = %d ", a); }

我正在尝试这个基本代码块来熟悉条件语句。我不认为我漏掉了一个括号或任何东西,但是我得到了一个错误,我漏掉了第二个else子句之前的一个语句,但我不理解这一点

#include stdio.h;
main()
{
    int a = 2;
    int b = 4;
    int c = 6;
    int d = 8;
    if ( a > b )
    {
        a = a - 1;
        printf("a = %d ", a);
    }
    else
    {
        if ( b >= c )
        {
            b == b ? : 2;
        }
        printf("b = %d ", b);
    }
    else
    {
        if ( c > d)
        {
            c = c + d;
        }
    }
    else
    {
        d = d / 2;
    }
}

有什么建议吗?

如果正确缩进代码,您将看到问题:

} else {
    if ( c > d) {
        c = c + d; 
    } 
} else {
    d = d / 2;
}

如果正确缩进代码,您将看到问题:

} else {
    if ( c > d) {
        c = c + d; 
    } 
} else {
    d = d / 2;
}

此代码与您的代码相同,缩进为几种更正统的样式之一

int main(void)
{
    int a = 2;
    int b = 4;
    int c = 6;
    int d = 8;

    if (a > b)    
    {
        a = a - 1;
        printf("a = %d ", a); 
    }
    else
    {
        if (b >= c)   
        {
            b == b ? : 2; // Syntax errors here too (and statement with no effect?)
        }
        printf("b = %d ", b); 
    }
    else
    {
        if (c > d)
        {
            c = c + d; 
        } 
    }
    else
    {
        d = d / 2;
    }
}
如您所见,有3个连续的
else
子句,其中只允许您使用一个


还有其他语法问题。

此代码与您的代码相同,缩进为几种更正统的样式之一

int main(void)
{
    int a = 2;
    int b = 4;
    int c = 6;
    int d = 8;

    if (a > b)    
    {
        a = a - 1;
        printf("a = %d ", a); 
    }
    else
    {
        if (b >= c)   
        {
            b == b ? : 2; // Syntax errors here too (and statement with no effect?)
        }
        printf("b = %d ", b); 
    }
    else
    {
        if (c > d)
        {
            c = c + d; 
        } 
    }
    else
    {
        d = d / 2;
    }
}
如您所见,有3个连续的
else
子句,其中只允许您使用一个

还有其他语法问题。

在C编程中 如果。。其他的像这样

 if( condition 1 )
        statement1;
     else if( condition 2 )
        statement2;
     else if( condition 3 )
        statement3;
     else
        statement4;
在C语言编程中 如果。。其他的像这样

 if( condition 1 )
        statement1;
     else if( condition 2 )
        statement2;
     else if( condition 3 )
        statement3;
     else
        statement4;

对于if语句,C的结构只能有一个else语句。相反,它可以有几个其他的语句。为if语句添加更多的else语句将报告其语法错误

程序中的错误说明第二个else必须在其前面有if。
因此,将所有带有嵌套if的中间else语句转换为elseif语句。保持最后一条else语句不变,您可以摆脱该错误

对于if语句,C的结构只能有一个else语句。相反,它可以有几个其他的语句。为if语句添加更多的else语句将报告其语法错误

程序中的错误说明第二个else必须在其前面有if。
因此,将所有带有嵌套if的中间else语句转换为elseif语句。保持最后一条else语句不变,您可以摆脱该错误

我看不出有什么问题?if在else中,而另一个else是独立的?@SonnyOrdell:不;<代码>如果不与<代码> E/<代码>,因为中间有一个额外的<代码> } /代码>。注意缩进。if在else中,因为c=c+d被括在括号中,而whol;e如果block在else子句的括号内?问题在于多个else语句,而不是括号。我看不出问题?if在else中,而另一个else是独立的?@SonnyOrdell:不;<代码>如果不与<代码> E/<代码>,因为中间有一个额外的<代码> } /代码>。注意缩进。if在else中,因为c=c+d被括在括号中,而whol;e如果block在else子句的括号内?问题在于多个else语句,而不是括号。DOWNDOVERT似乎没有必要…DOWNDOVERT似乎没有必要。。。