" - "运营商while()循环 [英] "--" operator in while ( ) loop

查看:118
本文介绍了" - "运营商while()循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读K&安培; R书,第63页上有一行

I am reading K&R book, on page 63 there is a line

while (--lim > 0 && (c=getchar()) != EOF && c != '\n')

其中LIM为int等于1000

where lim is int equal to 1000

我的问题,这是为什么 LIM 不是while循环的后果运行后下降?
在我看来, - LIM 等同于LIM = LIM - 1

My question, why is lim is not decreasing after consequential runs of while loop ? As I see it, --lim is equivalent to "lim = lim - 1"

=============================================== ====================

===================================================================

感谢所有的答案!

推荐答案

- LIM 表示需要一个从价值 LIM 并使用结果。

--lim means "take one from the value of lim and use the result".

替代 LIM - 将使用 LIM 的值,然后采取一离开

The alternative lim-- would be "use the value of lim and then take one away".

所以,如果 LIM 开始于1000首次循环执行它的值为 999 之前,它是检查,看它是否是大于0。如果它是 LIM - 那么这将被检查将是值 1000 ,但它仍然有在通过循环迭代结束的999的值。这是在循环的开始和结束的重要

So if lim starts at 1000 the first time the loop executes it will have the value 999 before it is checked to see if it's greater than 0. If it were lim-- then the value that would be checked would be 1000, but it would still have the value of 999 at the end of the iteration through the loop. This is important at the start and end of the loop.

在MSDN作为此 preFIX递增和递减操作符

当操作员操作数出现之前,操作数递增或递减和新的值是前pression的结果。

When the operator appears before its operand, the operand is incremented or decremented and its new value is the result of the expression.

这篇关于" - "运营商while()循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆