英文:
What do the comparison operators in the body of a for loop do?
问题
我进入了我的编程课考试,在那里我遇到了一个问题,被要求解释用C语言编写的代码块的结果将会是什么,代码块如下...
#include <stdio.h>
int main(void)
{
int x = 1, y = 1;
for (; y; printf("%d %d |", x,
y))
y = x++ <= 5;
printf("\n");
return 0;
}
令我感到奇怪的是,一个比较运算符(<=)被放在for循环的主体部分,而不是for循环的条件测试部分。你能解释它是做什么的吗?
我尽量描述得尽可能清楚,希望你理解得很好。
英文:
I've entered my programming class' exam where i've encountered a question in which i was asked to interpret what would the result of the code block written in C would be and the code block is as follows...
#include <stdio.h>
int main(void)
{
int x = 1, y = 1;
for (; y; printf("%d %d |", x,
y))
y = x++ <= 5;
printf("\n");
return 0;
}
What struck me ass odd is that a comparison operator (=<) is put inside the body of the for loop instead of the condition-test part of the for loop.
Could you explain what it does?
I tried to be as descriptive as i can, hope you understood well.
答案1
得分: 2
First of all, a for-loop has the form:
for(<init>; <condition>; <iteration>) {<body>}
which is exactly equivalent to the following (except that all occurrences of outer-loop continue in the <body> must be substituted with <iteration>; continue):
{
<init>;
while(<condition>) {
<body>
<iteration>
}
}
Hence, the for-loop in the OP code (with obvious corrections) can be written as:
{
while(y) {
y = x++ <= 5;
printf("%d %d |", x, y);
}
}
The expression y = x++ <= 5; assigns to y the result of the boolean expression x++ <= 5. There is nothing "magic" about this. It will assign 1 to y if and only if x <= 5 and then increment x by one. The value of y (0 or 1) will then be the condition for executing the next iteration of the loop.
Thus, the loop will run for x (initially in the body) being 1, 2, 3, 4, 5, and 6. When x is 6, y will become 0, and the printf statement will be run a final time with x having the incremented value, 7.
The result of this code is simple, but it is unnecessarily difficult to understand. A better version could be:
#include <stdio.h>
int main(void)
{
for(int x = 2; x <= 7; x++) {
int y = (x == 7) ? 0 : 1;
printf("%d %d |", x, y);
}
printf("\n");
return 0;
}
英文:
First of all, a for-loop has the form
for(<init>; <condition>; <iteration>) {<body>}
which is exactly equivalent to the following (except that all occurrences of outer-loop continue in the <body> must be substituted with <iteration>; continue):
{
<init>;
while(<condition>) {
<body>
<iteration>
}
}
Hence, the for-loop in the OP code (with obvious corrections) can be written as:
{
while(y) {
y = x++ <= 5;
printf("%d %d |", x, y));
}
}
The expression y = x++ <= 5; assigns to y the result of the boolean expression x++ <= 5. There is nothing "magic" about this. It will assign 1 to y if and only if x <= 5 and then increment x by one. The value of y (0 or 1) will then be the condition for executing the next iteration of the loop.
Thus, the loop will run for x (initially in the body) being 1, 2, 3, 4, 5 and 6. When x is 6, y will become 0 and the printf-statement will be run a final time with x having the incremented value, 7.
The result of this code is simple, but it is unnecessarily difficult to understand. A better version could be:
#include <stdio.h>
int main(void)
{
for(int x = 2; x <= 7; x++) {
int y = (x == 7) ? 0 : 1;
printf("%d %d |", x, y));
}
printf("\n");
return 0;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论