stoletobe
stoletobe

Reputation: 49

Condition in a 'for' loop

I am experimenting about what can be put into a for loop declaration in C and how it can be used. I tried the following:

#include <stdio.h>

int stupid(int a)
{
    if(a == 3)
        return 1;
    else
        return 3;
}

int main(void)
{
    int i, j;
    for(i=0; stupid(i)==3,i<10; i++)
        printf("%d\n", i);
    return 0;
}

When I run the program it just prints the number from 1 to 10, and if I use && instead of comma between the stupid(i)==3 and i<10, then the program just prints the numbers up to 3. Why?

I don't really understand how this works and I was expecting the loop to pass all numbers and "skip" 3, but continue up to 10 and that's not really happening. Why does this happen? Is there some site where this is more clearly explained?

Upvotes: 4

Views: 18626

Answers (7)

Vovanium
Vovanium

Reputation: 3888

Use:

int main(void)
{
    int i,j;
    for(i=0; i<10; i++, i+=i == 3)
        printf("%d\n", i);
    return 0;
}

The limit condition on can only terminate the loop when the condition comes true, not skip iterations. If you want to skip some value, you have to do it at the counting part of for(), or do this with if().

i+=i==3 adds 1 to i when i becomes 3, as i==3 evaluates to 1 if the condition is met and to 0 otherwise (and adding 0 simply does not make any difference).

Upvotes: 0

Jan Thom&#228;
Jan Thom&#228;

Reputation: 13604

The comma operator evaluates the part before the comma, discards the result, evaluates the part after the comma, and returns that. So in your for loop the part after the comma is i < 10 and this is what is returned as condition for the for loop. That is why it prints the numbers 1 to 10 if you have the comma operator in it.

If you put the && operator in it, it means that both conditions before and after the && have to be met. Otherwise the loop terminates. So if i == 3 the left part evaluates to false and your loop ends.

Upvotes: 3

Lou Franco
Lou Franco

Reputation: 89152

In the for loop, there are three expressions needed, and they are separated by semicolons.

The first is an initializer, and it is run one time before the loop starts. It usually initializes the loop variables.

The second is a condition, and it is run right after the initializer and then before each subsequent iteration. If it is true, the loop statements are run. If it is false, the loop is over.

The third is an expression that is run right after each iteration and right before the condition is checked before the next iteration. It usually progresses the loop by changing the loop variable.

Your condition stupid(i)==3,i<10 uses the comma operator. The comma operator runs each side, but it returns only the value of the right hand side. The value of stupid(i)==3 is completely ignored. The condition stupid(i)==3 && i<10 is true only if both sides are true.

Remember, when the condition is false, the loop is over -- the iteration is not just skipped, the entire loop is over. To get what you want, use

 for(i=0; i < 10; ++i) {
    if (stupid(i)==3) {
       printf("%d\n",i);
    }
 }

This will go through 0-9, but skip the code if stupid(i) is not 3.

Upvotes: 0

Andrew Cooper
Andrew Cooper

Reputation: 32576

The second clause in the for loop (in your case stupid(i)==3,i<10) is a conditional that is evaluated prior to each entry of the loop body. If it evaluates to true then the loop body is executed. If it evaluates to false then the loop ends and execution continues after the loop body.

With the comma (stupid(i)==3,i<10), the code evaluates stupid(i)==3, forgets the result, and then evaluates i<10, and uses that result for the loop condition. So you get the numbers from 0 to 9.

stupid(i)==3 && i<10 will evaluate to true only if both parts of the expression are true, so when i=3, stupid(i)==3 is false, and the loop exits.

Upvotes: 8

AK.
AK.

Reputation: 763

Using the , operator expands to each line being run. The last line is expected to return a Boolean expression that indicates whether the next iteration should be executed.

In this case, while stupid() does get called, it only checks the return value from the expression i < 10 to decide further execution.

Upvotes: 0

Evan Mulawski
Evan Mulawski

Reputation: 55334

The for loop will only continue if the conditions are met. If you place an if statement within the for loop to verify that stupid(i) is equal to three, the for loop will continue.

Upvotes: 0

Donnie
Donnie

Reputation: 46903

The comma operator evaluates both, but then overall returns the value of its second operand. Since stupid() doesn't have any side effects, that means nothing much of use really happens here and you're overall just checking to see if i<10.

When you change it to && then both functions must return true (non-zero) for the iteration to continue. On the first pass through, on which the statement evaluates to false, the for loop halts and control continues past it.

Upvotes: 2

Related Questions