6

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!

1
  • 2
    The statement "leaving the first argument blank makes it act like TRUE" is not true. The second part of a for loop is what is the condition, not the first. Commented May 8, 2013 at 19:21

6 Answers 6

16

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.

Sign up to request clarification or add additional context in comments.

Comments

6

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.

Comments

2

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.

Comments

0

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

1 Comment

Try not to give a what, he/she can obviously see the what, they're wondering why.
0
 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.

Comments

0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.