1

I am not understanding for loop statement and expression following it. Please do help me understand.

#include<stdio.h>

int main()
{
   int x = 1;
   int y = 1;
   for( ; y ; printf("%d %d\n",x,y))
       y = x++ <= 5;
   return 0;
}

And the output I got

2 1
3 1
4 1
5 1
6 1
7 0
Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
Sandeep
  • 43
  • 1
  • 4
  • 3
    +1 im not clear why the downvotes, the code appears to be deliberately convoluted and the op is asking for help understanding what its doing, a valid question that might help many beginners. It is not the OP's code. – John Faulkner Jul 22 '13 at 06:25

6 Answers6

7

y = x++ <= 5; ==> y = (x++ <= 5); ==> first compare x with 5 to check whether x is small then or equals to 5 or not. Result of (x++ <= 5) is either 1, 0 assigned to y,

As x becomes > 5, (x++ <= 5) becomes 0 so y = 0 and condition false and loop break,

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
  • 1
    @Sandeep you may like to check: [What is the name of this operator: “`-->`”?](http://stackoverflow.com/questions/1642028/what-is-the-name-of-this-operator) – Grijesh Chauhan Jul 22 '13 at 09:18
  • @sjas may be not as I could understand Sandeep's (OP) main confusion was expression `y = x++ <= 5`. – Grijesh Chauhan Jul 22 '13 at 11:33
1

Initialize your variables:

int x = 1; int y = 1; 

There are 3 statements for the for loop: -1. Initialize, 2. Condition, 3. Iteration:increment/decrement
In your case, you did not provide the initialize condition, however, you have the part of condition and incrementation. I do not think your for loop is used in the correct way.

You should swap the part of incrementation with your body like this:

for(; y; y = x++ <= 5;)
   printf("%d %d\n", x, y)

First, you check whether the condition is true or not, y is true or not. Then, you print x and y out. Then, the part of incrementation is executed, x++ <= 5 or not. The result is assigned to y. It does so until your condition is false, y == false.

NOTE: For the good programming, you should enclose your body with a curly braces.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
lvarayut
  • 13,963
  • 17
  • 63
  • 87
  • 2
    Good practice is also keep space after `;` and `,` and expression `a<=b;` should be written like `a <= b`. I given some formatting to your answer to make it readable. btw good answer +1 – Grijesh Chauhan Jul 22 '13 at 20:51
1

Basically the for syntax is: for(StartCondition; Test; PostLoopOperation) DoWhileTestPasses;

In this case:

StartCondition == None
Test == (y != 0)
PostLoopOperation == do some printing 
DoWhileTestPasses == set y to zero if x > 5 otherwise to non-zero THEN increment x.

Which is all rather bad practice because it is confusing.

Would be better written as:

int x=0;
int y=0;
for(y=0; y = (x <= 6); x++)
{
    printff("%d %d\n",x,y);
}
return(0);
Steve Barnes
  • 27,618
  • 6
  • 63
  • 73
1

In y = x++ <= 5;, y stores the value that is output by the condition x++ <= 5 (here x++ is post increment). If the condition is true then y = 1 else y = 0.

for( ; y ; printf("%d %d\n",x,y))

In the for loop you are printing the values of x and y after executing the for loop body.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
0

similar to this

   int x = 1;
   for( int y = 1; y!=0 ; )
   {
       if (x++ <= 5)
       {
          y = 1;
       } 
       else
       {
          y = 0;
       }
       printf("%d %d\n",x,y);
   }
aah134
  • 860
  • 12
  • 25
0

Perhaps this slightly transformed (but functionally equal) code will help:

int x = 1;
int y = 1;
while (y) {
   y = (x <= 5);
   x = x + 1;
   printf("%d %d\n", x, y)
}
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173