Reputation: 55
#include <stdio.h>
#include <cs50.h>
int main(void)
{
int n;
printf("Please give me an integer greater than zero!\n");
n=GetInt();
if(n<0)
{
printf("You are giving me a bad value!\n");
return 1;
}
for(int i=n-1;i<n;n--)
printf("%d\n",n);
return 0;
}
I would like to know why the loop is not going to infinity if the user enters in a number for n
. Lets say that the user puts in 40 for n
; wouldn't i
always be n-1
, so 39 and n
being 40, then i
becomes 38 when n
becomes 39 and so on — so wouldn't that make an infinite loop?
Upvotes: 4
Views: 370
Reputation: 70472
Your for
loop:
for(int i=n-1;i<n;n--) {
printf("%d\n",n);
}
Translates into the following while
loop:
{
int i = n - 1;
while (i < n) {
printf(%d\n", n);
n--;
}
}
The first clause of the for
statement performs initialization. It is not repeated at each iteration, but only once. Thus, i
never changes value, and so the loop ends after a single iteration.
Upvotes: 2
Reputation:
you written perfect for loop... for(int i=n-1;i
#include<stdio.h>
#include<conio.h>
int main()
{
int i;
for(i=0;;)
{
printf("%d",i);
}
return 0;
}
or you can do anything else.... to put in infinite simply make ;; in the other two condition in loop.
Upvotes: 1
Reputation: 557
your condition is wrong in the for loop..in your loop i is not changing only n is changing try this
for(i=n-1;i<n;i--)
{
printf("%d\n",i);
}
Upvotes: 1
Reputation: 22145
This loop only runs once. Consider:
for(int i=n-1;i<n;n--)
n == 40
, on the first iteration, i = 39
.i < n
is true (39 < 40 == true
), so we go in to the loop for the first time.n
gets decremented to 39i < n
is false (39 < 39 == false
), so we don't get a second time through the loop.Now, what happens if we make n
increase instead of decrease? Will that run forever?
for(int i=n-1;i<n;n++)
The answer is "maybe, but probably not":
n
will reach the largest value that can be stored in an integer, INT_MAX
(defined in limits.h
, and on my system it is 2,147,483,647). INT_MAX
causes integer overflow. On most systems, however, the value will probably wrap around to INT_MIN
, or -2,147,483,648.
i < n
will be false, and your loop will terminate. But, since integer overflow on signed integers is undefined behaviour, you can't be sure that this will happen. It is better to write your program to avoid this situation.
If you really want it to run forever - just write:
while(1) { ... }
or
for(;;) { ... }
These two loops have the advantage that they are common ways to write an infinite loop, and so they are easy for other programmers to read.
Upvotes: 7
Reputation: 4888
What will happen is:
//Example n = 100
for (int i = 100 - 1; 99 < 100; 100--)
//We now have 99 < 99 on the next loop
//After that you will have 99 < 98 etc.. it will only run once
To make the loop you want use:
for(int i = n-1; i > n ; n--)
To make an endless loop use:
for(;;) //or while(true)
Upvotes: 1
Reputation: 95998
for(int i=n-1;i<n;n--)
Lets draw a (really short) table for n = 40
:
i | n
-----+-----
39 | 40 i < n ? true
39 | 39 i < n ? false
Thus, we'll exit the loop after the 1st iteration.
Clarification:
I guess you're confused because you think that i
is updated in each iteration, but that's the point - it doesn't, its value is fixed and only n
is changing.
Upvotes: 14
Reputation: 2790
n
will underflow somewhen because int is signed and has a value range of -2147483648 to 2147483647 for example (x86).
Somewhen n will get more positive than i.
Edit: The loop has at most 1 iteration.
Edit 2: The loop would have no iterations if n would have the value -2147483648 for example because -2147483648 - 1 will make the value positive (two complement integer arithmetic). But this could never the case because the pre condition is that n may not be negative.
Upvotes: 2
Reputation: 7629
The reason is that i
is never decremented, so it does only 1 loop:
i=39 n=40
i=39 n=39 -> stop
In order to decrement also i
you should write:
for(int i = n-1;i<n;n--,i--)
Upvotes: 3
Reputation: 10126
This happens because you are decrementing n
. At the second iteration i < n
is false, you are exiting from the loop.
Upvotes: 1
Reputation: 12214
i is only ever set once at the start of the loop. For example if the user enters 10 then i is 9 for the 1st iteration. By the 2nd iteration n is decremented by 1 and i is still 9.
Upvotes: 2