Why does this program stop decrementing at 0?

Feb 29, 2016 at 9:22pm
Why does it stop at 0? What is the reason? Why is it not continuing the output with the negative numbers?

1
2
3
4
5
6
7
8
9
10
 #include <stdio.h>
int main(void)
{
	int i;
	printf("Enter an integer: ");
	scanf_s("%d", &i);
	for (; i; i--)
		printf("%d ", i);
	return 0;
}
Feb 29, 2016 at 9:28pm
When (i) reached 0 the condition for the loop becomes false and the loop terminates ...
Also , to what negative number you want to decrement ??
Feb 29, 2016 at 9:38pm
Oh, yeah... that's right... 'cause 0 is false.
One more question: what does the condition i stand for. I mean... just i... there is no operator!?
Feb 29, 2016 at 9:55pm
In general .. Any non-zero number will be treated as true ..
and zero is treated as false ..

In the above program it will display the numbers from the number the user inputs to 1 .. but if you enter a negative number it will go to infinite loop ..



I suggest you to read more about loops ...
http://www.cplusplus.com/doc/tutorial/control/
Topic archived. No new replies allowed.