简体   繁体   English

if语句中的if语句

[英]If statement in if statements

I'm trying to understand the logic behind this code 我正在尝试了解此代码背后的逻辑

int i = 13;
if (i<10) if (i>=5)   Console.WriteLine("Case number 1");
else Console.WriteLine("case number 2");
Console.ReadLine();

So I know this isn't proper code (since there should be brackets to make this app works). 所以我知道这不是正确的代码(因为应该有方括号才能使此应用正常工作)。

I know how to make this code "clean" by modifying it 我知道如何通过修改使代码“干净”

But I'm trying to understand how Csharp is actually behaving with this erronated code, the logic behind the execution of this code 但我试图了解Csharp实际如何处理此错误代码,执行此代码的逻辑

From what I read, the else applies to the closest if, in this case (if >= 5). 根据我的阅读,在这种情况下,else适用于最接近的情况(如果> = 5)。

So when I write i = 3 it reads the first if and goes to the first else and console gives me "Case nr 2". 因此,当我编写i = 3时,它将读取第一个if并转到第一个else,并且控制台会显示“ Case nr 2”。

When I write i = 7 it reads the 2nd and gives me "Case nr 1" 当我写i = 7它读第二个,并给我“ Case nr 1”

when I write i = 13 it gives me nothing 当我写i = 13时,我什么也没有

From what I understood while researching it should go the else since 2 if's have been tested before, so from what I understand when it tests the first if it should go to the else, if it tests both if's it should go to the 2nd too since the if's don't really make sense. 根据我在研究中所了解的内容,应该从2之前进行过if的测试,所以从我所了解的方面来看,自从它进行第一个测试是否应该进行其他测试之后,如果同时进行了测试,那么它也应该进行第二次测试。如果不是真的没有道理。

If it only tests the 2nd if it should give me case number. 如果只测试第二个,应该给我案例编号。

So I'm definitely wrong in my way of thinking, but I can't find why it doesn't show up anything 所以我的思维方式肯定是错误的,但是我找不到为什么它没有显示任何东西

This is how your if loop works (i indended your code and added braces): 这就是if循环的工作方式(我喜欢您的代码并添加了花括号):

int i = 13;
if (i<10) {
    if (i>=5) {  
        Console.WriteLine("Case number 1");
    } else {
        Console.WriteLine("case number 2");
}
Console.ReadLine();

So inner else corresponds to if with condition i >= 5 Hence when your i = 13, it behaves like: 因此,内部else对应于条件i >= 5因此,当您的i = 13时,其行为类似于:

 is i < 10? No

And hence never enter's if and executes next statement ie Console.ReadLine(); 因此,永远不要输入if并执行下Console.ReadLine();语句,即Console.ReadLine();

Your code is like this: 您的代码是这样的:

if (i < 10)
{
    if (i >= 5) {
        Console.WriteLine("Case number 1");
    }
    else {
        Console.WriteLine("case number 2");
    }
}

Console.ReadLine();

so if i is in [5,10) it is case number 1 and if it is in (-Infinity,10) it is case number 2 因此,如果i[5,10) ,则为案例编号1;如果在(-Infinity,10) ,则为案例编号2

Some hints: 一些提示:

  • consider using some well known code conventions 考虑使用一些众所周知的代码约定
  • use indentations to make the code more readable 使用缩进使代码更具可读性
  • use opening and closing curly braces whether you have a block with one statement or multiple statement 无论您是否有一个语句块或多个语句块,请使用大括号和大括号
  • try not to have some magic text in your code, here "Case" and "case" is something that can cause later issues for you mainly in test scenarios 尝试在代码中不要包含一些魔术文字,这里的“ Case”和“ case”可能会在以后的测试场景中给您带来以后的问题

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

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