问题标签 [pre-increment]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
1551 浏览

c++ - 如果我在同一个语句中使用前置增量和后置增量会发生什么?

我今天看到了一个有趣的声明,其中包含后增量和前增量。请考虑以下程序-

那个有趣的陈述实际上发生了什么?后增量应该在语句完成后增加 x 的值。那么对于该语句,第一个 x的值保持为 5。在预增量的情况下,第二个 x的值应该是 6 或 7(不确定)。

为什么它给z的值是0?是5 - 5还是6 - 6?请解释。

0 投票
1 回答
113 浏览

c - 一元操作有时是自杀性的。用 C 代码搞砸

C语言代码。

我的问题是为什么i是:392?根据我的输出应该是336。(因为6 * 7 * 8 = 336

我真的在这里搞砸了吗??

0 投票
1 回答
217 浏览

c - Pre vs post increment operator profiling results

I was profiling the pre vs post increment operator in C (out of curiosity, not for micro-optimization purposes!), and I got some surprising results. I expected the post increment operator to be slower, but all of my tests show that it's (non-trivially) faster. I've even looked at the assembly code generated by using the -S flag in gcc, and post has one extra instruction (as expected). Can anyone explain this? I'm using gcc 4.8.1 on Arch Linux.

Here is my code.

EDIT: There is an integer overflow error in my code. It doesn't effect the question, but you should note the actual number of iterations is different from the argument passed in.

Post.c:

Pre.c:

Here are the results:

I also wrote a timing script a while ago. Running that gave the same results:

The output is mostly self explanatory, but the idea is that it runs both programs 10 times (by default) and calculates the mean and standard deviation of the results in milliseconds.

I've included the assembly code generated by gcc for post.c and pre.c on pastebin, since I didn't want to clutter this post with things that might be unnecessary.

Sorry for bringing this debate up again, but these numbers just seem strange to me.

0 投票
2 回答
73 浏览

post-increment - 程序的输出((困惑))

您能否解释以下程序的输出:

输出是:

0 投票
5 回答
4015 浏览

java - Java:前置、后置运算符优先级

我对 Java 中的运算符优先级有两个类似的问题。

第一:

根据Oracle 教程
后缀 (expr++, expr--) 运算符的优先级高于前缀 (++expr, --expr)

所以,我想评估顺序:

但是 Java 似乎忽略了 PRE/POST 排序并将它们放在一个级别上。所以真正的顺序:

是什么导致答案为 (10 * 12 * 12) = 1440。

第二个:

这个问题的例子:

部分接受的答案:“到分配时,++已经增加了ato的值2(因为优先级),所以=覆盖了增加的值。”

好的,让我们一步一步看:

似乎一切都很好。但是让我们对该代码进行一些更改(将“=”替换为“+=”)

步骤 1-4 应与上述相同。所以,在第 4 步之后,我们有类似的东西:

在哪里a==2

然后我想:好的,a = 2+3应该a是这样5。但答案只是4

我真的很困惑。我已经花了几个小时,但仍然无法理解我错在哪里。

PS我知道,我不应该在实际应用程序中使用这种“风格”。我只是想了解我的想法有什么问题。

0 投票
3 回答
129 浏览

c++ - c++中的运算符优先级和运算符关联性规则

我不明白为什么以下程序的输出是63

我期待它是61。究竟是a += a + ++a;做什么的?

0 投票
5 回答
815 浏览

c++ - simple c++ loop prefix postfix operator

So I am working on a book, and I understand how these things work, but I am seeking a more concrete explanation of why this prints 6 and 8. Please help me.

Thanks

0 投票
3 回答
162 浏览

c++ - 前置增量和后置增量

我最近一直在尝试了解后期和前期增量是如何工作的,但我已经想太多了。

一次迭代后“产品”是否变为 25?

并且“商”是否在一次迭代后变为 5/6?

0 投票
2 回答
185 浏览

c# - C# 中的增量运算符魔术

为什么此代码的输出给出值 100。请帮助我理解这种行为。

我在 C 和 C# 编译器中运行了相同的代码。在 C 编译器中给出值 200 在 C# 编译器中给出值 100。

为什么同一段代码在两个编译器中的行为是这样的?

0 投票
3 回答
2216 浏览

c - 为什么 if 语句中的表达式 a==--a true?

为什么第二个 if 语句为真?

输出为:真 1 真 2

这是由于未定义的行为而发生的,因为我将相同的变量与其递减值进行比较?