Loop question

#include "stdafx.h"
#include <iostream>
using namespace std;

void func(void);

static int count = 10;

int main()
{
while(count--)
{
func();
}
return 0;

}

void func(void)
{
static int i = 5;
i++;
cout << "i is " << i;
cout << " and count is " << count << endl;
}
//this programs output is://
i is 6 and count is 9
i is 7 and count is 8
i is 8 and count is 7
i is 9 and count is 6
i is 10 and count is 5
i is 11 and count is 4
i is 12 and count is 3
i is 13 and count is 2
i is 14 and count is 1
i is 15 and count is 0
My question is, "Why does the while loop stop? What exactly is the condition that makes the loop terminate? Why is this not an infinite loop?"
Because 0 is false,otherwise is true.
What do you mean? What is the conditional statement?
while(count--) ?
Why does it stop at 0, why not keep subtracting into negative numbers?
it means that the conditional statement inside the while is false.

http://msdn.microsoft.com/pt-br/library/b0kk5few.aspx
Last edited on
What do you mean? What is the conditional statement?

The conditional statement is count--. This will evaluate to true when the value is anything other than 0, and to false when the value is 0.

So as soon as count becomes 0, the expression is evaluated as false, and the loop stops.

EDIT: And please use code tags when posting code, to make it readable.
Last edited on
Topic archived. No new replies allowed.