Why is my loop not infinite

#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 cycle is not going indefinitely, if the user enters the number to n . Assume that the user places 40 to n ; there would be i always n-1 , so 39 and n are equal to 40, then i becomes 38, when n becomes 39, etc. - so that it would not make an endless cycle?

+4
source share
10 answers

for(int i=n-1;i<n;n--)

Let's draw a (very short) table for n = 40 :

  i | n -----+----- 39 | 40 i < n ? true 39 | 39 i < n ? false 

Thus, we come out of the cycle after iteration 1 st.

explanation

I think you are confused because you think that i is updated at each iteration, but the point - it does not, its value is fixed and is changed only n .

+14
source

This loop runs only once. Consider:

  for(int i=n-1;i<n;n--) 
  • C n == 40 , on the first iteration i = 39 .
  • The condition i < n is true ( 39 < 40 == true ), so we enter the cycle for the first time.
  • At the end of the cycle n is reduced to 39
  • The condition i < n is equal to false ( 39 < 39 == false ), so we do not get a second time through the loop.

Now, what happens if we increase n instead of decreasing? Will it work forever?

  for(int i=n-1;i<n;n++) 

The answer is "maybe, but probably not":

  • In the end, n reaches the maximum value that can be stored in an integer size, INT_MAX (defined in limits.h , and in my system - 2,147,483,647).
  • Create an integer greater than INT_MAX , causing overflow of integers .
  • The result of an integer overflow signed integer is undefined The , which means that the result can be anything (and, indeed, your program may crash).

However, most of the value of systems is likely to be rounded up to INT_MIN or -2147,483,648.

  • If that happens, i < n is false, and your loop will end.

But, as an integer overflow signed integers is undefined behavior , you can not be sure that this will happen. It’s better to write your own program to avoid this situation.


If you really want it to work 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 to read for other programmers.

+7
source

The reason is that i never decreases, so it performs only 1 cycle:

  i=39 n=40 i=39 n=39 -> stop 

To reduce and i you should write:

  for(int i = n-1;i<n;n--,i--) 
+3
source

n will be used below, because int signed and has a range of values between -2147483648 and 2147483647, for example (x86). When n becomes more positive than I am.

Edit: Cycle has no more than one iteration.

Edit 2: In a series of iterations would not be if it would be in the n value -2147483648 because -2147483648 - 1 will make a positive value (two additional integer arithmetic). But this can never be so, because the precondition is that n can not be negative.

+2
source

i is set only once at the beginning of the cycle. For example, if the user enters 10, then I for the 9th iteration - 9. At the 2nd iteration, n decreases by 1, but I still 9.

+2
source

Your cycle for :

 for(int i=n-1;i<n;n--) { printf("%d\n",n); } 

Translates into the following loop while :

 { int i = n - 1; while (i < n) { printf(%d\n", n); n--; } } 

The first sentence of the operator for performing initialization. This is not repeated at each iteration, but only once. Thus, i never change the value, and so the cycle ends after one iteration.

+2
source

This is because you reduce n . On the second iteration of the i < n is false, you get out of the cycle.

+1
source

What will happen:

 //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 to use:

 for(int i = n-1; i > n ; n--) 

To use infinite loop usage:

 for(;;) //or while(true) 
+1
source

your condition is incorrect in a for loop. In your cycle, I do not change, only n changes, try this

  for(i=n-1;i<n;i--) { printf("%d\n",i); } , i);  for(i=n-1;i<n;i--) { printf("%d\n",i); } 
+1
source

you wrote perfectly for the 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 something else .... endlessly easy to do ;; in the other two terms in the cycle.

+1
source

Source: https://habr.com/ru/post/1491788/


All Articles