0

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.

  • 5
    Two answers. (1) This code causes your code review to fail, because your coworkers don't want to deal with tricky stuff like this. (2) Ask your professor after the exam. – JohnFilleau May 06 '23 at 14:47
  • Comparisons are considered arithmetic operators, similar to `+` or `*`. And like any other arithmetic expression, comparisons give a result. It should not be hard to figure out the result of a comparison. – Some programmer dude May 06 '23 at 14:48
  • 1
    Also worth noting that this doesn't compile. https://godbolt.org/z/W1a4oMMqq – JohnFilleau May 06 '23 at 14:49
  • 2
    Please clarify: since this is an exam question, are you asking for help in cheating on it? Be aware that your professor is entirely capable of finding this question on this site and noticing that it matches their exam question, then catching you when your answer looks similar to a posted answer. And if you were thinking of deleting the question after you get an answer, it won't work - moderators will undelete it. (Or your professor may have 10K rep which allows them to view deleted posts.) – Nate Eldredge May 06 '23 at 15:16
  • 2
    That code is syntactically incorrect, `=<` is the problem. Either you mistyped it and then correct it, or it has been given exactly like this and then has no meaning. – Jean-Baptiste Yunès May 06 '23 at 16:27
  • 1
    Yeah i mistyped that, it was supposed to be (<=). – ahailtotheKing May 06 '23 at 18:05
  • I also posted this question after the exam was over so it's not a problem – ahailtotheKing May 06 '23 at 18:07
  • @therealramos Update code to reflect the correct `<=`. Also review `printf("/n");`. was that `"\n"`? – chux - Reinstate Monica May 07 '23 at 03:52

1 Answers1

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;
}
nielsen
  • 5,641
  • 10
  • 27
  • 2
    Those are not equivalent because a `continue;` in the body skips an `` included in the body but not one in the `for` statement proper. – Eric Postpischil May 06 '23 at 16:23
  • Thanks a lot! That ternary operator you just put in the body of the loop made more sense. – ahailtotheKing May 06 '23 at 18:16
  • One more thing caught my attention, what would the output of this program be if x++ was replaced with ++x? I'd really appreciate if you answer. – ahailtotheKing May 06 '23 at 22:20
  • 1
    @therealramos "what would the output of this program be if x++ was replaced with ++x?" --> certainly you can code that yourself to determine the answer. – chux - Reinstate Monica May 07 '23 at 03:56
  • @EricPostpischil As always you have a keen eye on the details and you are of course right. I have tried to update the answer accordingly. Thanks. – nielsen May 07 '23 at 07:06
  • @therealramos Using `x++` or `++x` only makes a difference if the value is used within the given expression. In this case (unlike the OP code), `x++` is an expression by itself and the value of `x++` is not used within that expression so using `++x` or `x++` does not make any difference to the result (and most likely not to the generated code either). See also [here](https://stackoverflow.com/questions/17366847/what-is-the-difference-between-pre-increment-and-post-increment-in-the-cycle-fo) (C++, but it largely applies to C as well). – nielsen May 07 '23 at 07:18