简体   繁体   English

不使用花括号的嵌套IF条件语句的规则

[英]The rules of Nested IF conditional statements without using curly braces

This might be an extremely beginner question given that I have about 2 years of programming experience already, however something always bothered me with nested IF conditional statements and how they work without having curly braces on them. 考虑到我已经有2年的编程经验,这可能是一个非常初学者的问题,但是,嵌套IF条件语句及其在没有花括号的情况下如何工作始终使我感到困扰。

I've always used the braces as to keep my coding organized. 我一直使用花括号来使编码井井有条。 For example like this. 例如这样。

    public static void main(String[] args) 
{
    int x = 9;
    int y = 8;
    int z = 7; 
    if (x > 9)
    {
        if (y > 8)
        {
            System.out.println("x > 9 and y > 8");
        }
    }
    else if (z >= 7)
    {
        System.out.println("x <= 9 and z >= 7");
    }
    else 
    {
        System.out.println("x <= 9 and z < 7");
    }
}

I've always used this pattern as it has always worked for me. 我一直使用这种模式,因为它一直对我有用。

However, why is it that something written in this format doesn't work the same way? 但是,为什么用这种格式写的东西却不能以同样的方式工作呢?

    public static void main(String[] args) 
{
    int x = 9;
    int y = 8;
    int z = 7; 
    if (x > 9)
        if (y > 8)
            System.out.println("x > 9 and y > 8");
    else if (z >= 7)
        System.out.println("x <= 9 and z >= 7");
    else 
        System.out.println("x <= 9 and z < 7");
}

The first code would print out x <= 9 and z >= 7, but the second code would print nothing. 第一个代码将打印出x <= 9和z> = 7,但是第二个代码将不打印任何内容。 (I'm assuming that the else if and else statements are inside the initial if statement). (我假设else if和else语句位于初始if语句内)。

In other words, what are the rules in how the compiler tests the conditional statements when no curly braces take place like the example above? 换句话说,当没有花括号发生时,如上例所示,编译器如何测试条件语句的规则是什么? I've tried looking for information online, but I can't seem to find information and/or I don't know how to specifically call this problem in order to research my doubts. 我曾尝试过在线查找信息,但似乎找不到信息和/或不知道该如何专门称呼此问题以研究我的疑虑。

The compiler does not care about indentation. 编译器不关心缩进。 An else clause is always associated with the last if that doesn't already have an else . else条款总是与最后一个关联if不已经有一个else This: 这个:

if (x > 9)
    if (y > 8)
        System.out.println("x > 9 and y > 8");
else if (z >= 7)
    System.out.println("x <= 9 and z >= 7");
else 
    System.out.println("x <= 9 and z < 7");

is the same as this: 与此相同:

if (x > 9)
{
    if (y > 8)
    {
        System.out.println("x > 9 and y > 8");
    }
    else
    {
        if (z >= 7)
        {
            System.out.println("x <= 9 and z >= 7");
        }
        else
        {
            System.out.println("x <= 9 and z < 7");
        }
    }
}

Also, notice that else if(...) ... really just means else {if(...) {...}} - it's a combination of else and if , not a special feature. 另外,请注意else if(...) ...实际上只是意味着else {if(...) {...}} -它是elseif的组合,不是特殊功能。

On second if I put braces on it, It will be like this to the compiler. 第二,如果我在上面加上花括号,编译器将像这样。 Because else if or else finds immediate if . 因为else if or else发现if

if (x > 9) {
        if (y > 8){
            System.out.println("x > 9 and y > 8");
        }else if (z >= 7) {
            System.out.println("x <= 9 and z >= 7");
        }else {
            System.out.println("x <= 9 and z < 7");
        }
}

If you have to replicate you should put at least one braces like: 如果必须复制,则应至少放置一个括号,例如:

if (x > 9) {
        if (y > 8)
            System.out.println("x > 9 and y > 8");
        else if (z >= 7)
            System.out.println("x <= 9 and z >= 7");
}else 
    System.out.println("x <= 9 and z < 7");

Now, Last else is off first if statement for sure. 现在,如果可以肯定的话,Last else首先关闭。

This isn't really a matter of curly braces, it's a matter of how you've formatted your code. 这实际上不是花括号的问题,而是代码格式的问题。 Whitespace doesn't matter in Java (or C++.) 在Java(或C ++)中,空格并不重要。

So while you've written this: 因此,当您编写此代码时:

if (x > 9)
    if (y > 8)
        System.out.println("x > 9 and y > 8");
else if (z >= 7)
    System.out.println("x <= 9 and z >= 7");
else 
    System.out.println("x <= 9 and z < 7");

That doesn't mean it's equivalent to your original code. 这并不意味着它等同于您的原始代码。 To illustrate why, here's how this code would be more reasonably formatted: 为了说明原因,这是如何更合理地格式化此代码:

if (x > 9)
    if (y > 8)
        System.out.println("x > 9 and y > 8");
    else if (z >= 7)
        System.out.println("x <= 9 and z >= 7");
    else 
        System.out.println("x <= 9 and z < 7");

I hope that illustrates what's going on here. 我希望这可以说明这里发生的事情。

Curly braces are, essentially, to keep the compiler on track. 本质上,大括号可以使编译器保持正常运行。 If you only have 1 if, and 1 else, such as: 如果只有1个if和另外1个,例如:

 if (x < 3)
    // some code here
 else
    // something else here

When you do math problems in real life, order of operations tells you to do what's in the parentheses first, no? 当您在现实生活中遇到数学问题时,运算顺序会告诉您首先执行括号中的内容,不是吗? The curly brackets are a grouping symbol used to denote what order of operations the compiler is to take. 大括号是一个分组符号,用于表示编译器要执行的操作顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM