1

I recently started learning C and I feel like this is a not so smart question but, I was wondering if you could take 2 variables, initialize them, evaluate their conditions and increment them in 1 for loop

say I have 2 ints: a and b and I would like to initialize both of them and increment them.

for(a=1, b= 1; a < 10 , b < 6; a++, b++)
{
    printf("a= %d\n", a);
    printf("/tb= %d\n", b);
}

Is there a reason this doesnt work? or am I just doing it wrong?

I've looked at this question but in it he/she only wants to increment 2 variables whereas I would like to apply everything for both of my variables

skdadle
  • 155
  • 1
  • 2
  • 17
  • 3
    `a < 10 , b < 6` is equivalent to `b < 6`. Did you mean `a < 10 && b < 6`? – melpomene Mar 07 '18 at 14:38
  • `a < 10 , b < 6` => `a < 10 && b < 6` else you're just testing `b` but since variables are incremented at the same time, what's the point? – Jean-François Fabre Mar 07 '18 at 14:39
  • it is working correctly, what error you are getting.? – yajiv Mar 07 '18 at 14:39
  • @Jean-FrançoisFabre - I think you need to check the comma operator – Ed Heal Mar 07 '18 at 14:39
  • 1
    @EdHeal I didn't imply that it's equivalent. I meant that to check both conditions you need `&&` not `,` – Jean-François Fabre Mar 07 '18 at 14:40
  • 1
    And coincidentally changing this particular condition from `a < 10, b < 6` to `a < 10 && b < 6` will result in the exact same output. Since they both increase linearly from 1. – StoryTeller - Unslander Monica Mar 07 '18 at 14:40
  • @Jean-FrançoisFabre My fault thinking that `=>` means implies.- Perhaps I should read this again https://en.wikipedia.org/wiki/List_of_logic_symbols – Ed Heal Mar 07 '18 at 14:42
  • @EdHeal: don't sweat this too much – Jean-François Fabre Mar 07 '18 at 14:58
  • @Jean-FrançoisFabre - I try not too. But the symbold `=>` is implies – Ed Heal Mar 07 '18 at 15:02
  • @EdHeal Yes, but only if you think in context of logic symbols. Arrows of any style and shape are frequently used to indicate the way something needs to be changed. – Gerhardh Mar 07 '18 at 15:07
  • Perhaps `->` would be better? – Ed Heal Mar 07 '18 at 15:13
  • Although perfectly possible, it is generally not recommended to use several iterators in the same for loop because it makes the code much harder to read. The single loop iterator should be the one used for accessing/calculating the "thing" you are iterating across. Any additional incrementing variables such as counters actually don't have anything to do with the loop itself and are better kept away from the 3 controlling expressions of the for loop. – Lundin Mar 07 '18 at 15:54

2 Answers2

4

Is there a reason this doesnt work? or am I just doing it wrong?

It does work, but not in the way you anticipated:

a < 10 , b < 6 evaluates a < 10 and then b < 6 but it's the result of b < 6 that gets returned. So your loop will only go to 5.

Comma oparator (wikipedia)

Let me explain how a for loop works:

You have three 'segments', all of which are optional:

  • initialisation this part is run once before the loop starts.
  • condition this part is evaluated before every iteration, if this condition evaluates to false the loop exits.
  • increment is executed after every iteration.
for ( initialisation ; condition ; increment ) {
     /* body of the for loop */
}

You can achieve the same semantics with a while loop:

initialisation;

while (condition) {
    /* body of the for loop */
    increment;
}

So for example:

for (;1;) will never exit and for (;0;) will never run.

To achieve the desired behaviour you could do:

//1-9, and values of "b" which are 1-5
int a, b;

for (a = 1, b = 1; a <= 9; ++a, (b <= 4 ? ++b : 0)) {
    printf("a: %d\n", a);
    printf("b: %d\n", b);

    printf("\n");
}

But you are better off doing this inside the for loop:

int a, b;

// This reads much better
for (a = 1, b = 1; a <= 9; ++a) {
    printf("a: %d\n", a);
    printf("b: %d\n", b);

    printf("\n");

    if (b <= 4) {
        ++b;
    }
}
Marco
  • 7,007
  • 2
  • 19
  • 49
  • thanks a ton! but Can my condition here be "a < 10 && b < 6" if I would like to have a = 1 - 9 ( a=1, a=2.. ) and b = 1- 5( b= 1, b=2..) as result? – skdadle Mar 07 '18 at 15:00
2

This almost works, however your stop condition is wrong. It will need to be something like a < 10 && b < 6 or similar, depending on what you want.

The reason for this is due to the behavior of the comma operator: only the right statement of the comma will be returned. So your loop will only stop when b < 6, ignoring your a < 10 condition. The other two pieces of the loop don't rely on a value being returned, so they don't have this problem.

0x5453
  • 12,753
  • 1
  • 32
  • 61
  • I added an "&&" instead of my "," in my "a < 10 , b < 6" part but it still only evaluates my "b < 6" condition. I would like it to print for me values of "a" that are 1-9, and values of "b" which are 1-5. Did I go about this wrong? thanks for the replies! – skdadle Mar 07 '18 at 14:54
  • @skdadle: well, yeah, `a < 10 && b < 6` will evaluate to `false` as soon as `b` reaches 6, which happens before `a` reaches 10. Based on your description, you want something like this: `for ( a = 1, b = 1; a < 10; a++, b++ ) { printf( "a: %d\n", a ); if ( b < 6 ) printf( "b: %d\n", b ); }`. Basically, you're looping and printing `a` while it's less than 10, but only printing `b` while it's less than 6. – John Bode Mar 07 '18 at 15:39