Comparison operators in a for loop body do什么?

huangapple go评论66阅读模式
英文:

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 &lt;stdio.h&gt;

int main(void)
{
   int x = 1, y = 1;

   for (; y; printf(&quot;%d %d |&quot;, x, 
   y))
        y = x++ &lt;= 5;

   printf(&quot;\n&quot;);

   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(&lt;init&gt;; &lt;condition&gt;; &lt;iteration&gt;) {&lt;body&gt;}

which is exactly equivalent to the following (except that all occurrences of outer-loop continue in the &lt;body&gt; must be substituted with &lt;iteration&gt;; continue):

  {
     &lt;init&gt;;
     while(&lt;condition&gt;) {
       &lt;body&gt;
       &lt;iteration&gt;
     }
  }

Hence, the for-loop in the OP code (with obvious corrections) can be written as:

   {
     while(y) {
        y = x++ &lt;= 5;
        printf(&quot;%d %d |&quot;, x, y));
     }
   }

The expression y = x++ &lt;= 5; assigns to y the result of the boolean expression x++ &lt;= 5. There is nothing "magic" about this. It will assign 1 to y if and only if x &lt;= 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 &lt;stdio.h&gt;

int main(void)
{
   for(int x = 2; x &lt;= 7; x++) {
     int y = (x == 7) ? 0 : 1;
     printf(&quot;%d %d |&quot;, x, y));
   }
   printf(&quot;\n&quot;);

   return 0;
}

huangapple
  • 本文由 发表于 2023年5月6日 22:43:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189492.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定