0

The following code I came across though I have basic idea of C. I still get the logic of below code.

  1. How does the loop terminate and when does the loop terminate — at what condition?

  2. At last counter becomes zero, but why doesn't the loop continue to execute with -1, -2 and instead it terminates?

Code:

#include <stdio.h>

void func(void);
static int count = 5;
int main()
{
    while (count--)
    {
        func();
    }
    return 0;
}

void func(void)
{
    static int i = 5;
    i++;
    printf("i is %d and count is %d\n", i, count);
}

Output:

i is 6 and count is 4
i is 7 and count is 3
i is 8 and count is 2
i is 9 and count is 1
i is 10 and count is 0
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
hash
  • 3
  • 4

7 Answers7

1

while(term)

When term(condition/variable/function) evaluates to 0 then while loop terminates.

count-- return the value of count then decrements it.

So when count-- return zero loop terminates. Which will be when count has value 0. Also see comment.

C takes 0 as false and rest as true so the while loop will break when condition evaluates to false (0).

cruxion effux
  • 1,054
  • 3
  • 14
  • 27
  • Actually, the loop goes through a final iteration after `count` becomes zero, because `while (count--)` evaluates `count` and then decrements it, so there is a cycle where `count` is `1` before it is decremented to zero, but the loop continues for one more iteration because the value of the post-decrement expression is `1`, which evaluates to true so the loop condition is still satisfied. – Jonathan Leffler Jun 06 '15 at 05:58
1

Here the loop terminates when count becomes 0 because in C, 0 acts like false in Java (boolean type) and the condition is therefore false. The loop exits.

when count=

0 — loop condition is false

non 0 — loop condition is true

Sourav Kanta
  • 2,727
  • 1
  • 18
  • 29
  • You need to be quite careful. The condition is `while (count--)`, so in the iteration when `count` starts at `1`, the condition evaluates to true (because `1` is not `0`) but the variable `count` is decremented to `0` and has the value `0` in the body of the loop. – Jonathan Leffler Jun 06 '15 at 06:01
  • yeah as its post decrement the value is taken first then the decrementation is done – Sourav Kanta Jun 06 '15 at 06:04
1

The conditional of the while loop, count--, evaluates to 1 when the value of count is 1 before the evaluation of the expression. The side effect of the evaluation of the expression is that the value of count is decremented by 1, i.e. its value becomes 0.

That's why you see

i is 10 and count is 0

as the output.

When the conditional of the loop is evaluated next time, the value of the expression is 0 and the value of count is set to -1. Since the value of the expression is 0, execution of the loop stops.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

count-- is post-decrement. So, the value of count is not decremented instantly in the while condition. It is decremented afterwards (just after the evaluation of while condition). Thus, while continues to run when count = 1 and count-- is done. But just after the entering while, count becomes 0, thus, next time when the condition evaluation reaches, while breaks.

In simple terms, count is equal to 5 and you expect it to run 5 iterations (under simple flow), so it runs 4 to 0. If you want it other way around, you can do, try this and see for yourself

while( 0 != count )
{
   func() ;
   count-- ;
}
Abhineet
  • 5,320
  • 1
  • 25
  • 43
0

In C and C++ any expression which results in zero (0) will be evaluated to false if the expression is used in place of condition. Look -

if(0){
  //never reached; never executed
}  

Similarly any non-zero in an expression will be evaluated to true -

if(5){
   //always reached and executed
}  

This fact is applicable for condition in while-loop -

int i = 5;
while(i){
  //do something

  i--;
}
Razib
  • 10,965
  • 11
  • 53
  • 80
0

question1:how does the loop terminates and when does the loop terminates at what condition.

ans: The while loop start with count = 5. The loop condition satisfied because count is non-zero .But immediately count value is decresed by one and becomes count =4, due to count--. So the Output is

i is 6 and count is 4

Upto count =1,it continues.Count =1 too satisfy loop condtion. But immediately count-- happens and now count =0. So the output is

i is 10 and count is 0

So on next check loop condition not satisfied (Count = 0) and the loop terminates.

question2:at last counter becomes zero,but why the loop don't continue to exist to -1,-2 instead it terminates?

ans: When the loop condition becomes zero it creates a FALSE condition. So it gets terminated there.

Note : Only zero value in the condition block terminates the loop. If it is less than zero it will not.

check this.

void func(void);
static int count = -5;
main()
{
    while (count++)
    {
        func();
    }
    return 0;
}

void func(void)
{
    static int i = 5;
    i++;
    printf("i is %d and count is %d\n", i, count);
}

creates output as

i is 6 and count is -4
i is 7 and count is -3
i is 8 and count is -2
i is 9 and count is -1
i is 10 and count is 0
Tintu Thomas
  • 85
  • 10
0

The while expression is boolean; the integer expression count-- is therefore implicitly cast to boolean such that any non-zero value is true and zero is false.

Clifford
  • 88,407
  • 13
  • 85
  • 165