带有for循环的程序

Cod*_*ius 0 java for-loop pre-increment post-increment

for当这段代码出现在我脑海中时,我正在编写一个循环程序.

for(int i=1; i<=10; i++,i++)
Run Code Online (Sandbox Code Playgroud)

程序运行正常,输出也正确.但后来我尝试了以下代码:

for(int i=1; i<=10; ++i,++i)

for(int i=1; i<=10; ++i,i++)

for(int i=1; i<=10; i++,++i)
Run Code Online (Sandbox Code Playgroud)

令我惊讶的是,它们都产生相同的输出,1 3 5 7 9.现在我的问题是,for循环是如何工作的以及为什么所有代码​​在我使用预增量和后增量时产生相同的输出同样的for循环?

Epi*_*rce 5

它相当于

int i = 1;
while(i <= 10)
{
   //stuff would happen here but these loops are all empty
   i++;
   i++;
}
Run Code Online (Sandbox Code Playgroud)

int i = 1;
while(i <= 10)
{
   //stuff would happen here but these loops are all empty
   ++i;
   ++i;
}
Run Code Online (Sandbox Code Playgroud)

int i = 1;
while(i <= 10)
{
   //stuff would happen here but these loops are all empty
   ++i;
   i++;
}
Run Code Online (Sandbox Code Playgroud)

int i = 1;
while(i <= 10)
{
   //stuff would happen here but these loops are all empty
   i++;
   ++i;
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,无论是预增量还是后增量,它都无关紧要.它只是增加了iby 的值1.