snakeopus121
snakeopus121

Reputation: 139

For loop with printf as 3rd argument

Studying for a computer science final......

I really cannot figure this example out.....

I understand that leaving the first argument blank makes it act like TRUE....

but I don't understand what leaving a variable in the second argument accomplishes....

What I don't understand the most is how a printf statement "updates" the variable condition...

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

The output is:

2 1
3 1
4 1
5 1
6 1
7 0

edit:

I understand now the for-loop structure part.....

Thanks for the answers - very insightful thanks!

Upvotes: 6

Views: 6754

Answers (6)

vish213
vish213

Reputation: 786

Although Answer provided by simonc is perfect, there is also a practical solution. Type this program in Visual Studio, add a breakpoint at start of for loop and run each statement using F10 key. It'll clearly show you what flow is taken at execution time. It'll help clear all your doubts. Also don't forget to keep a watch on variables' values which will help further. This was what helped me through most of my doubts. Enjoy coding!

Upvotes: 0

Jeff Langemeier
Jeff Langemeier

Reputation: 1018

A trick I found early on was, if I couldn't understand the for loop, try and break it down into an equivalent while loop. You can also paren things to make them more readable (as long as you follow the order of operations on your parens to not screw up the evaluation. Your loop would look like this with those changes:

#include<stdio.h>
int main()
{
    int x=1, y=1;
    while(y)
    {
        y = (x++ <= 5);
        printf("%d %d\n", x, y)
    }
    printf("\n");
    return 0;
}

With those couple changes it makes it easy to see that your print statement isn't changing/updating anything, but is only printing the resultant of your y value.

Upvotes: 2

Coffee_lover
Coffee_lover

Reputation: 557

 Leaving a variable y implies the loop will run till y is true
 loop will stop when y becomes false

 Now this condition that you have written in body evaluates value of y
 y = x++ <= 5;

 Whenever x++ <= 5 implies y is true that is 1 
 So it starts with x=1 and prints until value of x becomes 7
 and y=x++ <= 5; returns false and loop exits.

Upvotes: 0

simonc
simonc

Reputation: 42165

A for loop can be thought of as for (INITIALIZATION; CONDITION; AFTERTHOUGHT)

The first part of the loop is for initialisation. Leaving this empty is fine, it just indicates that you have already initialised any variables required by the loop.

The y in the second expression (or condition) of the for loop is equivalent to y!=0. It keeps the for loop running until y==0.

The printf in the afterthought is run at the end of each iteration but doesn't change the value of y. The loop's body does change y however.

Most textbooks will describe this. Or see Wikipedia or cplusplus.

Upvotes: 16

user123
user123

Reputation: 9071

Consider this structure in a for loop:

for (a; b; c)
{
    d;
}

This is what will happen:

  1. Code a will be executed
  2. Condition b will be evaluated. If it's false, the for loop breaks.
  3. Code d is executed.
  4. Code c is executed.
  5. Go to step 2

What's happening in yours at the end is that the "c" part of the code is printing the value of y and it happens to be 0. The condition is then evaluated. Since y == 0, the for loop will break because 0 is equivalent to false.

Upvotes: 6

Evo510
Evo510

Reputation: 173

This runs the program until y is 0 because 0 as an integer is also False

Upvotes: 0

Related Questions