0

The semicolon has been added after the first while loop, but why is the value of the i variable 3 here, where j is 2?

#include<stdio.h>
int main()
{
    int i=1;
    while(i++<=1);    
    printf("%d",i);  
    int j=1;
    while(j++<=1)
        printf("%d",j);
        
    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335

7 Answers7

2

Both while loops run once (and once only); the difference is that, in the second case (the j loop) you are printing the 'control variable' inside the loop but, for the first case, you are printing it after the loop condition has evaluated to false. Also note that, in the first case, the semicolon immediately following the while statement defines the body of that loop as empty1.

Let's break down the first loop into steps:

  • On the first test of the condition, i++ evaluates to 1 and then i is incremented – so the loop runs.

  • On the second test, i++ evaluates to 2 (so the loop doesn't run) but i is still (post-)incremented, leaving it with the value of 3 (as shown in the output).

The same thing happens with j in the second loop but, in that case, as previously mentioned, you are displaying the value in the body of the loop (on its only run), so you see the value after the first (post-)increment.

As noted in the comments, if you add another printf("%d", j); after the body of the loop (which, in that case, consists of a single statement), you will see that j, too, has the value 3 when that loop has finished.


1 More precisely, the semicolon (on its own) defines a null statement, which forms the body of the while loop.


It is often helpful to clarify such 'null loops' by putting the semicolon on a line by itself (some compilers, with full warnings or static analysis enabled, may even suggest you do this):

#include<stdio.h>
int main()
{
    int i = 1;
    while (i++ <= 1)
        ; // Body of loop - Null Statement
    printf("%d", i); // Loop has finished
    int j = 1;
    while (j++ <= 1)
        printf("%d", j); // Body of loop
    // Loop has finished
    return 0;
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
0

For starters let;s consider how the postfix increment operator works. From the C Standard (6.5.2.4 Postfix increment and decrement operators)

2 The result of the postfix ++ operator is the value of the operand. As a side effect, the value of the operand object is incremented (that is, the value 1 of the appropriate type is added to it).

Now consider this while loop

int i=1;
while(i++<=1);

In the first iteration of the loop the value of the expression i++ as the value of its operand that is 1. So the loop will iterate a second time, Due to applying the side effect to the variable i it will be equal already to 2 when the expression i++<=1 will evaluate the second time.

Now the value of i is equal to 2 and is greater than 1. so the loop will be interrupted. Again due to applying the side effect of the postfix increment operator to the variable i it will be equal to 3. This value is outputted in the following call of printf.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

If you add the semicolon in the end of the while you will take: i = 3 and j = 3(IN THE OUTPUT).

If you add the semicolon only in the end of the printf and not to the while you will take i = 2 and j = 2(IN THE OUTPUT), but in the end you will, also, have the values i = 3 and j = 3.

Akshay
  • 1,161
  • 1
  • 12
  • 33
0

In simplest terms,

consider three things for each iteration of first while loop:

first iteration:

int i = 1;//initialization  

while(i++<=1); //1. i evaluated as 1, 
               //2. loop condition is true causing loop to iterate again. 
               //3. ...but not before ++ causes i==2.

second iteration:

while(i++<=1); //1. i evaluated as 2,
               //2. loop condition is false causing loop to eventually exit
               //3. ...but not before ++ causes i==3.

printf is not included in the while loop (because of the semicolon) but when program flow finally reaches printf("%d",i); the value of i is output as 3

In the second loop, because printf is included in the while construct, it will output the value of j for each iteration. For the same reasons as in loop one, it will also iterate only twice, and its values at time of output will be 2 & 3.

Using a debugger to set break points, and a watch on i, you can step through code such as this to see the sequence of these effects as they happen.

ryyker
  • 22,849
  • 3
  • 43
  • 87
0

For explanation porpouses look at this example:

int main()
{
    int i=1;
    while(i++<=1)
       printf("%d",i);     
    printf("%d",i); 
}

The Output is:

2
3

Ok let's dive into the programm procedure:

  1. Declaring i (i = 1)
  2. while checking Condition and it's true because i++ returns 1(old Value). That's because the ++ are after the i. If you would code it like this ++i than it returns 2(new Value) (i = 2)
  3. execute printf (i = 2)
  4. while checking Condition and it's false because i++ returns 2 and 2 is not <= 1 (i = 3)
  5. execute printf (i = 3)

Do you understand?

If it solved your thoughts jumble ;) mark it as answer.

BierDav
  • 1,219
  • 1
  • 10
  • 27
0

You've got many answers already so I won't try to explain it in words but with an illustration in code. I've added two functions:

  • One that acts like a prefix increment operator (++i)
  • One that acts like a postfix increment operator (i++)

I'm only using the postfix version in the program though. I've added logging to the function so you can see how it works.

#include <stdio.h>

// this acts as if you put ++ before the variable
int preinc(int* p) { 
    ++*p;
    return *p;
}

// this acts as if you put ++ after the variable
int postinc(int* p) {
    int rv = *p;
    ++*p;
    printf("returning %d but incremented to %d\n", rv, *p);
    return rv;
}

int main() {
    int i=1;
    while(postinc(&i) <= 1);    
    printf("end result: %d\n---\n", i);

    int j=1;
    while(postinc(&j) <= 1)
        printf("in loop: %d\n", j);
    printf("end result: %d\n", j);
}

Output:

returning 1 but incremented to 2
returning 2 but incremented to 3
end result: 3
---
returning 1 but incremented to 2
in loop: 2
returning 2 but incremented to 3
end result: 3
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
-1

This happens because the "i" variable is incremented only after the first iteration of the loop, in which the empty statement (terminated by the semicolon) is evaluated. that is why it is highly recommended to do "++i" rather than "i++", because then the incrementation is performed prior to the evaluation. if you flip it here, you will see that i will be equal to 2 as well, regardless of the presence of the semicolon

elbud
  • 75
  • 6
  • 1
    "_the "i" variable is incremented only after the first iteration of the loop_" - Not quite right. It's incremented after the evaluation of `i++` (which has the unincremented value) and `i` will carry the incremented value inside the loop. – Ted Lyngmo Aug 02 '21 at 12:23