为什么“-i”和“i--”在Java for循环中具有相同的行为?

为什么“-i”和“i--”在Java for循环中具有相同的行为?,java,for-loop,Java,For Loop,为什么在Java中,我和我在for循环中有相同的行为 例: 我的变量i在循环之前不会减少: for(int i = 5; i > 0; --i) { System.out.println(i); } 及 。。。将同时打印5,4,3,2,1 但这是: int i = 5; System.out.println(--i); int i = 5; System.out.println(i--); …将打印4和5。这是因为for循环的工作原理如下: for (<1. vari

为什么在Java中,我和我在for循环中有相同的行为

例: 我的变量i在循环之前不会减少:

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

。。。将同时打印5,4,3,2,1

但这是:

int i = 5;
System.out.println(--i);


int i = 5;
System.out.println(i--);
…将打印4和5。

这是因为for循环的工作原理如下:

for (<1. variable declaration and initialization>;
     <2. condition to loop>;
     <4. for update>) {
    <3. statements>
}
for(<Part that will be executed before the loop>;
    <Part that is the condition of the loop>;
    <Part that will be executed at the end of each iteration) {
    <statements>
}
System.out.println(i);
在执行for循环中的语句之后,在检查要循环的条件之前,执行i或i条件。也就是说,在for update部分使用i或-i并不重要。

这是因为for循环的工作原理如下:

for (<1. variable declaration and initialization>;
     <2. condition to loop>;
     <4. for update>) {
    <3. statements>
}
for(<Part that will be executed before the loop>;
    <Part that is the condition of the loop>;
    <Part that will be executed at the end of each iteration) {
    <statements>
}
System.out.println(i);

在执行for循环中的语句之后,在检查要循环的条件之前,执行i或i条件。也就是说,在for update部分使用i-或-i并不重要。

i和-i-都有相同的副作用,将i减少1。它们的结果值不同。在循环代码中,您只使用了副作用,而忽略了结果。在独立的println代码中,您将显示结果。

i和i都有相同的副作用,将i减小1。它们的结果值不同。在循环代码中,您只使用了副作用,而忽略了结果。在独立的println代码中显示结果。

For循环的工作方式如下:

for (<1. variable declaration and initialization>;
     <2. condition to loop>;
     <4. for update>) {
    <3. statements>
}
for(<Part that will be executed before the loop>;
    <Part that is the condition of the loop>;
    <Part that will be executed at the end of each iteration) {
    <statements>
}
System.out.println(i);
正如您所看到的,输出是-i还是-i并不重要,因为print调用总是在变量递减之前发生。为了达到预期效果,您可以尝试以下方法:

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

For循环的工作方式如下:

for (<1. variable declaration and initialization>;
     <2. condition to loop>;
     <4. for update>) {
    <3. statements>
}
for(<Part that will be executed before the loop>;
    <Part that is the condition of the loop>;
    <Part that will be executed at the end of each iteration) {
    <statements>
}
System.out.println(i);
正如您所看到的,输出是-i还是-i并不重要,因为print调用总是在变量递减之前发生。为了达到预期效果,您可以尝试以下方法:

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

我认为最简单的方式是,在循环中,您可以这样打印:

for (<1. variable declaration and initialization>;
     <2. condition to loop>;
     <4. for update>) {
    <3. statements>
}
for(<Part that will be executed before the loop>;
    <Part that is the condition of the loop>;
    <Part that will be executed at the end of each iteration) {
    <statements>
}
System.out.println(i);

注意println的参数是i,而不是i-或-i。这种减量已经发生在其他地方。你不是在打印递减的结果,而是在循环中打印i的值。

我认为最简单的方法是,在循环中,你是这样打印的:

for (<1. variable declaration and initialization>;
     <2. condition to loop>;
     <4. for update>) {
    <3. statements>
}
for(<Part that will be executed before the loop>;
    <Part that is the condition of the loop>;
    <Part that will be executed at the end of each iteration) {
    <statements>
}
System.out.println(i);

注意println的参数是i,而不是i-或-i。这种减量已经发生在其他地方。打印的不是递减的结果,而是循环中i的值。

请参阅关于递减前和递减后运算符之间差异的问题:为什么-i在java for循环中不起作用?-它工作得很好。请看这个关于前减量运算符和后减量运算符之间差异的问题:为什么-我不能在java for loop中工作?-它工作得很好。谢谢mhlz!那么我们能不能说forint i=5;i>0-i与forint i=5完全相同;i>0;我-?两者都会产生完全相同的结果,所以我想你可以这么说。谢谢mhlz!那么我们能不能说forint i=5;i>0-i与forint i=5完全相同;i>0;两者都会产生完全相同的结果,所以我想你可以这么说。