2

I have a feeling I'm gonna feel really stupid here, but I'm just learning about using ++ and -- to increment and decrements variables for while loops, and was wondering why this piece of code works and why this doesn't?

Bad code:

int ctr = 0;
while (ctr < 10)
  printf("%d",ctr);
  ctr=ctr+1;

The bad code outputs zeros indefinitely.

Working code:

int ctr=0;
while (ctr++ < 10)
    printf("%d",ctr);

The idea is for the output to be 012345678910 but even in the working code, it starts at 1 and goes to 10, instead of starting at 0. Even though the initial value of ctr is 0.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
King Sutter
  • 61
  • 2
  • 4
  • 11
  • 3
    C doesn't care about your whitespace and manual indentation. You needs some `{}`. – Cornstalks Feb 02 '17 at 06:05
  • 3
    C is not Python. Indentation doesn't matter in C — at least, not to the compiler, but humans trying to read your code care about it. – Jonathan Leffler Feb 02 '17 at 06:14
  • 2
    Just to add to what @JonathanLeffler said, compiler will read and translate your code anyway, it's just humans who will abandon your code. – Sourav Ghosh Feb 02 '17 at 06:16
  • Your Bad code is missing curly braces: int ctr = 0; while (ctr < 10){ printf("%d",ctr); ctr=ctr+1;} Now print satement and the ctr increment statement would be within scope of while loop, and you will get correc value of ctr ie; 0 to 9. your Working code: int ctr=0; while (ctr++ < 10) { printf("%d",ctr);} here you are getting output for ctr from 1 to 9 because ctr++ is incrementing ctr to ctr+1 ie; 0+1 = 1, and then printing. if you want it to be printed starting from 0 to 9, use the first approah, just remember to add curly brace to decide the scope of while – Sweekritee Singh Feb 02 '17 at 07:23

3 Answers3

5

In the first case

while (ctr < 10)
  printf("%d",ctr);
  ctr=ctr+1;

the while loop body is considered only the printf() statement. ctr=ctr+1; is not part of the loop body. So you have an unchanged variable in loop condition check, which makes it infinite loop.

You need to enclose both the statements in a block scope, using {} so that both the statements become part of the loop body. Something like

while (ctr < 10) {
  printf("%d",ctr);
  ctr=ctr+1;
}

will do.


In the second case

int ctr=0;
while (ctr++ < 10)
    printf("%d",ctr);

ctr is already incremented as the side effect of the postfix increment operator, in the while condition checking expression. Thus, while printing the value, the already incremented value is being printed.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

It is quite simple, indeed.

int ctr = 0;
while (ctr < 10)
  printf("%d",ctr);
  ctr=ctr+1;

In this first piece of code, in spite of the indentation, your while is only involving printf("%d",ctr);, because there is no block making ctr=ctr+1; belong to the while.

It could be written:

int ctr = 0;
while (ctr < 10)
  printf("%d",ctr);
ctr=ctr+1;     // This is not in the loop, even with the previous indentation.

There is no increment to ctr in this loop and then it will run forever printing zeros.

In this second piece of code

int ctr=0;
while (ctr++ < 10)
    printf("%d",ctr);

you do increment ctr every pass and it will work fine.

If you want to make the first loop work, write it like this:

int ctr = 0;
while (ctr < 10) {
  printf("%d",ctr);
  ctr=ctr+1;
}

Now ctr=ctr+1; is really inside the while loop.

Ed de Almeida
  • 3,675
  • 4
  • 25
  • 57
-3
int ctr = 0;
while (ctr++ <= 10)
{ 
    printf("%d",ctr-1);
}

Output to be 012345678910.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    Output? Where are you printing something in this code? – Ed de Almeida Feb 02 '17 at 06:16
  • 2
    `var test` is invalid C. You've not printed anything? – Jonathan Leffler Feb 02 '17 at 06:16
  • 1
    The edited code is at least C. However, it is aconventional (and unnecessary) to use `ctr - 1` in the printing. You could apply the `++` in the call to `printf()` — as in `printf("%d", ctr++);` — and omit it from the `while` condition. The output includes a leading zero as requested — but the sample output didn't include that originally. Good answers on SO tend to have more explanation of why the fix is appropriate and why the original was broken. – Jonathan Leffler Feb 02 '17 at 18:32