简体   繁体   English

如果条件为假,为什么while循环被视为真?

[英]Why is while loop treated true if condition is false?

I am trying to do in Java:我正在尝试用 Java 做:

int i=5;
while(i-- >0) {
   System.out.println(i);

}

When running this program the output is:运行此程序时,输出为:

4
3
2
1
0

I am very surprised to see 0 in output.我很惊讶在输出中看到0 I am new in development.我是新开发的。 Can anyone justify this?谁能证明这一点?

In your while condition i-- > 0 , the variable i is evaluated first and then decremented.在您的while条件i-- > 0中,首先评估变量i然后递减。

When i reaches the value 1 , it will pass the loop test and then get decremented to 0 .i达到值1时,它将通过循环测试,然后递减为0 This is why the print statement shows 0 in the output.这就是 print 语句在输出中显示0的原因。

Here is a mnemonic you can use to keep track of how the decrement and increment operators work:这是一个助记符,您可以使用它来跟踪递减和递增运算符的工作方式:

int i = 5;
System.out.println("When i = 5 then:");
System.out.println("i-- is " + i--);
i = 5;
System.out.println("--i is " + --i);

Output:输出:

When i = 5 then:
i-- is 5
--i is 4

Simply, because you compare i>0 and decrement i afterwards.很简单,因为你比较i>0并在之后递减i

// If I is 1, you compare 1>0 and decrement i afterwards.
// This is how the postdecrement operator works
while(i-- >0) {
   System.out.println(i);
}

the loop will behave like the following.循环的行为如下。

is i=5 > 0?
decrement i to 4
output i = 4.
is i=4 > 0?
decrement i to 3
output i = 3.
...
and so on

As you can see the value you compare to 0 is allways higher then the one you are outputing.如您所见,您与0比较的值总是高于您输出的值。 This happens due to how the -- operator works.这是由于--运算符的工作方式造成的。 If it´s preceding to the i as --i it will decrement the variable i first and return it´s value afterwards.如果它在i之前作为--i它将首先递减变量i然后返回它的值。 If it´s not preceding as in your case i-- you will have the value of i returned first and i beeing decremented afterwards.如果它没有像您的情况i--在 i 之前 - 您将首先返回i的值,然后i减少。

Postdecrement/Increment operator works on the principle "Use first and then Change" Postdecrement/Increment运算符的工作原理是“先使用后更改”

Initially value of i=5, when it enters while loop it will compare value of i first and then it prints the decremented value.初始值 i=5,当它进入 while 循环时,它将首先比较 i 的值,然后打印递减的值。 Here i will show you each iteration along with checks performed in each iteration,在这里,我将向您展示每次迭代以及在每次迭代中执行的检查,

  1. Now value of i=5(in memory), inside while(5>0), it prints 4.现在 i=5(在内存中)的值,在 while(5>0) 内,它打印 4。

  2. Now value of i=4(in memory), inside while(4>0), it prints 3.现在 i=4(在内存中)的值,在 while(4>0) 内,它打印 3。

  3. Now value of i=3(in memory), inside while(3>0), it prints 2.现在 i=3(在内存中)的值,在 while(3>0) 内,它打印 2。

  4. Now value of i=2(in memory), inside while(2>0), it prints 1.现在 i=2(在内存中)的值,在 while(2>0) 内,它打印 1。

  5. Now value of i=1(in memory), inside while(1>0), it prints 0.现在 i=1(在内存中)的值,在 while(1>0) 内,它打印 0。

Hope now you are clear to go ahead.希望现在你清楚地继续前进。 Gud Luck.好运。

The post-decrement operator -- is like a post-paid service .后减算符--就像是后付费服务 Like a credit card, first you use, then you pay.就像信用卡一样,先使用,然后付款。

I thought I can give you a real-life idea of what really is occurring in this statement, when i == 1我想我可以给你一个真实的想法,当i == 1时,这个语句中到底发生了什么

while(i-- >0) 

So, first you check if i(1)>0 .所以,首先你检查是否i(1)>0 1>0 So, yes it is. 1>0所以,是的。 Right after this statement is done, i becomes 0. Then, you print that value.在此语句完成后, i变为 0。然后,您打印该值。

Alternatively, you might also get this intuition by noticing that although your loop started with i=5 , the value 5 never got printed.或者,您也可以通过注意到虽然您的循环以i=5开始,但值5从未被打印出来,从而获得这种直觉。

由于您在 while 循环中使用后减运算符,因此当i为 1 时, i--返回 1,然后在循环内最后一次打印i时得到 0。

Only because of post decrement operator (i--) will check the condition first then decrease the value of i .仅因为后减运算符(i--)将首先检查条件然后减小i的值。 Output is giving such.输出是这样的。 Thank you谢谢

int i=5; //initialize with 5
while(i-- >0) { //post decrements operator so, check condition first then decrease the value. 
   System.out.println(i);
}

In first iteration of while loop will check 5 > 0 will be checked after that decrease the value of i and i will become 4 So, Print it 4 not 5 .while循环的第一次迭代中,将检查5 > 0之后将检查i的值,并且i将变为4因此,打印4而不是5

  • When i = 5 conditional statement will be (5>0) ( true ) and print 4 .i = 5条件语句将为(5>0) ( true ) 并打印4
  • i = 4 conditional statement will be (4>0) ( true ) and print 3 . i = 4条件语句将是(4>0) ( true ) 并打印3
  • i = 3 conditional statement will be (3>0) ( true ) and print 2 . i = 3条件语句将是(3>0) ( true ) 并打印2
  • i = 2 conditional statement will be (2>0) ( true ) and print 1 . i = 2条件语句将为(2>0) ( true ) 并打印1
  • i = 1 conditional statement will be (1>0) ( true ) and print 0 . i = 1条件语句将是(1>0) ( true ) 并打印0

Now, i became 0 so conditional statement will be (0>0) ( False ).现在, i变成了0 ,所以条件语句将是(0>0) ( False )。

So, loop exits.所以,循环退出。

To get desired output try this要获得所需的输出,请尝试此

while(--i >0) {
   System.out.println(i);

}

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

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