4
#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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 4
    Why does "i become 38 when n becomes 39"? – Raymond Chen Jul 17 '13 at 06:41
  • 1
    `i` is is smaller than `n` only for the first iteration of the loop. After that `n` is decremented and is now equal to `i`. – Nobilis Jul 17 '13 at 06:45
  • 1
    In the style of The Crocodile Hunter... "That's not an infinite loop. *That's* an infinite loop." Just imagine I just pulled out a `while(1)` before the second sentence. – Nik Bougalis Jul 17 '13 at 06:58
  • You seem to think `i=n-1` happens at every iteration. But it only happens the first time. – ugoren Jul 17 '13 at 13:32

10 Answers10

14

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.

Maroun
  • 94,125
  • 30
  • 188
  • 241
7

This loop only runs once. Consider:

 for(int i=n-1;i<n;n--)
  • With n == 40, on the first iteration, i = 39.
  • The condition i < n is true (39 < 40 == true), so we go in to the loop for the first time.
  • At the end of the first loop, n gets decremented to 39
  • The condition i < 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":

  • Eventually, 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).
  • Making an integer larger than INT_MAX causes integer overflow.
  • The result of integer overflow on a signed integer is undefined, which means the result could be anything (and indeed, your program could crash).

On most systems, however, the value will probably wrap around to INT_MIN, or -2,147,483,648.

  • If this happens, 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.

Timothy Jones
  • 21,495
  • 6
  • 60
  • 90
3

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--)
Tobia Zambon
  • 7,479
  • 3
  • 37
  • 69
2

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.

bkausbk
  • 2,740
  • 1
  • 36
  • 52
2

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.

sashang
  • 11,704
  • 6
  • 44
  • 58
2

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.

jxh
  • 69,070
  • 8
  • 110
  • 193
1

This happens because you are decrementing n. At the second iteration i < n is false, you are exiting from the loop.

Alex
  • 9,891
  • 11
  • 53
  • 87
1

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)
Floris Velleman
  • 4,848
  • 4
  • 29
  • 46
1

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);
   }
Coffee_lover
  • 545
  • 6
  • 15
1

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.