#include <stdio.h>
int main(void) {
int i= 1, j = 1;
for (; j; printf("%d %d",i, j))
j = i++ <= 5;
return 0;
}
output-
2 13 14 15 16 17 0
Can anyone explain what is happening inside the loop? This question was asked in the interview process.
任何人都可以解释循环内发生的事情吗?在面试过程中询问了这个问题。
5 个解决方案
#1
3
The code is equivalent to:
代码相当于:
int main(void) {
int i=1,j=1;
while (j != 0) {
j = (i<=5);
i = i+1;
printf("%d %d",i,j);
}
return 0;
}
I think that its meaning and the output is evident now. Note that printf
does not print any separator after the second number. It means that the two digit "numbers" in the output are printed by two successive invocations of printf
each. If you use printf("%d %d; ",i,j);
instead of printf("%d %d",i,j);
the output will be:
我认为它的意义和产出现在很明显。请注意,printf在第二个数字后不会打印任何分隔符。这意味着输出中的两位“数字”通过两次连续的printf调用打印出来。如果你使用printf(“%d%d;”,i,j);而不是printf(“%d%d”,i,j);输出将是:
2 1; 3 1; 4 1; 5 1; 6 1; 7 0;
#2
1
Your code is the same as
你的代码是一样的
#include <stdio.h>
int main() {
int i = 1, j = 1;
while (j > 0) {
if (i <= 5) {
j = 1;
} else {
j = 0;
}
i = i + 1;
printf("%d %d",i, j);
}
}
It should now be easy to understand.
现在应该很容易理解。
#3
0
De-obfuscated, the code is equivalent to this:
去混淆,代码相当于:
for(int i=1; i<7; i++)
{
printf("%d\t%d\n", i+1, (i+1) < 7);
}
What such a loop would be needed for, I have no idea.
这个循环需要什么,我不知道。
#4
0
Initially, i
and j
are 1
so condition for loop will be true and it will enter into the loop. At j = i++<=5;
statement first it will execute conditional operation and assign result of conditional operation (true = 1, or, false = 0) to j
and increment i
by one. After this, it will execute print statement from the loop which will print value for i
(which is now incremented by one is 2) and j
(will be 1 as the condition is true). This will continue until the i<=5
condition is true. Whenever i
becomes greater than 5 j
will be 0, breaking the loop.
最初,i和j为1,因此循环的条件为真,它将进入循环。在j = i ++ <= 5;语句首先它将执行条件操作并将条件操作的结果(true = 1,或,false = 0)分配给j并将i递增1。在此之后,它将从循环执行print语句,该循环将打印i的值(现在增加1为2)和j(条件为真时将为1)。这将持续到i <= 5条件为真。每当我变得大于5时j将为0,打破循环。
#5
0
To make the code and its output more clear rewrite the program the following way
要使代码及其输出更清晰,请按以下方式重写程序
#include <stdio.h>
int main(void)
{
for ( int i = 1, j = 1; j != 0; )
{
j = i++ <= 5;
printf("%d %d\n", i, j );
}
return 0;
}
The program output is
程序输出是
2 1
3 1
4 1
5 1
6 1
7 0
As the variable j
is calculated as a result of this condtion
因为变量j是作为该条件的结果而计算的
i++ <= 5
then it will be always equal to 1
except when i
will be equal to 6
because for this value of i
the condition is evaluted to false that is to 0
and this value is assigned to j
.
那么它将总是等于1,除非当i等于6时,因为对于i的这个值,条件被评估为假,即0,并且该值被分配给j。
So the last iteration of the loop is when i
is equal to 6. In this case after this statement
因此,循环的最后一次迭代是当i等于6.在这种情况下,在此语句之后
j = i++ <= 5;
i will be equal to 7
and j will be equal to 0
. These values are outputted in the last iteration of the loop.
i将等于7,j将等于0.这些值在循环的最后一次迭代中输出。
7 0
#1
3
The code is equivalent to:
代码相当于:
int main(void) {
int i=1,j=1;
while (j != 0) {
j = (i<=5);
i = i+1;
printf("%d %d",i,j);
}
return 0;
}
I think that its meaning and the output is evident now. Note that printf
does not print any separator after the second number. It means that the two digit "numbers" in the output are printed by two successive invocations of printf
each. If you use printf("%d %d; ",i,j);
instead of printf("%d %d",i,j);
the output will be:
我认为它的意义和产出现在很明显。请注意,printf在第二个数字后不会打印任何分隔符。这意味着输出中的两位“数字”通过两次连续的printf调用打印出来。如果你使用printf(“%d%d;”,i,j);而不是printf(“%d%d”,i,j);输出将是:
2 1; 3 1; 4 1; 5 1; 6 1; 7 0;
#2
1
Your code is the same as
你的代码是一样的
#include <stdio.h>
int main() {
int i = 1, j = 1;
while (j > 0) {
if (i <= 5) {
j = 1;
} else {
j = 0;
}
i = i + 1;
printf("%d %d",i, j);
}
}
It should now be easy to understand.
现在应该很容易理解。
#3
0
De-obfuscated, the code is equivalent to this:
去混淆,代码相当于:
for(int i=1; i<7; i++)
{
printf("%d\t%d\n", i+1, (i+1) < 7);
}
What such a loop would be needed for, I have no idea.
这个循环需要什么,我不知道。
#4
0
Initially, i
and j
are 1
so condition for loop will be true and it will enter into the loop. At j = i++<=5;
statement first it will execute conditional operation and assign result of conditional operation (true = 1, or, false = 0) to j
and increment i
by one. After this, it will execute print statement from the loop which will print value for i
(which is now incremented by one is 2) and j
(will be 1 as the condition is true). This will continue until the i<=5
condition is true. Whenever i
becomes greater than 5 j
will be 0, breaking the loop.
最初,i和j为1,因此循环的条件为真,它将进入循环。在j = i ++ <= 5;语句首先它将执行条件操作并将条件操作的结果(true = 1,或,false = 0)分配给j并将i递增1。在此之后,它将从循环执行print语句,该循环将打印i的值(现在增加1为2)和j(条件为真时将为1)。这将持续到i <= 5条件为真。每当我变得大于5时j将为0,打破循环。
#5
0
To make the code and its output more clear rewrite the program the following way
要使代码及其输出更清晰,请按以下方式重写程序
#include <stdio.h>
int main(void)
{
for ( int i = 1, j = 1; j != 0; )
{
j = i++ <= 5;
printf("%d %d\n", i, j );
}
return 0;
}
The program output is
程序输出是
2 1
3 1
4 1
5 1
6 1
7 0
As the variable j
is calculated as a result of this condtion
因为变量j是作为该条件的结果而计算的
i++ <= 5
then it will be always equal to 1
except when i
will be equal to 6
because for this value of i
the condition is evaluted to false that is to 0
and this value is assigned to j
.
那么它将总是等于1,除非当i等于6时,因为对于i的这个值,条件被评估为假,即0,并且该值被分配给j。
So the last iteration of the loop is when i
is equal to 6. In this case after this statement
因此,循环的最后一次迭代是当i等于6.在这种情况下,在此语句之后
j = i++ <= 5;
i will be equal to 7
and j will be equal to 0
. These values are outputted in the last iteration of the loop.
i将等于7,j将等于0.这些值在循环的最后一次迭代中输出。
7 0