简体   繁体   English

为什么我的循环不会变得无限

[英]Why is my loop not going 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 loop is not going to infinity if the user enters in a number for n . 我想知道为什么如果用户输入n的数字,循环不会变为无穷大。 Lets say that the user puts in 40 for n ; 让我们说用户输入40为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? 难道i不会总是n-1 ,所以39和n是40,那么当n变为39时i变成38,依此类推 - 那么这不会成为无限循环吗?

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

Lets draw a (really short) table for n = 40 : 让我们为n = 40绘制一个(非常短的)表格:

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

Thus, we'll exit the loop after the 1 st 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. 我猜你很困惑,因为你认为i在每次迭代中都会更新,但这就是重点 - 它没有 ,它的值是固定的,只有n在变化。

This loop only runs once. 此循环仅运行一次。 Consider: 考虑:

 for(int i=n-1;i<n;n--)
  • With n == 40 , on the first iteration, i = 39 . n == 40 ,在第一次迭代中, i = 39
  • The condition i < n is true ( 39 < 40 == true ), so we go in to the loop for the first time. 条件i < n为真( 39 < 40 == true ),所以我们第一次进入循环。
  • At the end of the first loop, n gets decremented to 39 在第一个循环结束时, n减少到39
  • The condition i < n is false ( 39 < 39 == false ), so we don't get a second time through the loop. 条件i < n是假的( 39 < 39 == false ),所以我们没有第二次通过循环。

Now, what happens if we make n increase instead of decrease? 现在,如果我们做了什么情况n增加,而不是减少了? 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). 最终, n将达到可以存储在整数INT_MAX (在limits.h定义,在我的系统中定义为2,147,483,647)。
  • Making an integer larger than INT_MAX causes integer overflow . 使整数大于INT_MAX会导致整数溢出
  • 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. 但是,在大多数系统上,该值可能会转换为INT_MIN或-2,147,483,648。

  • If this happens, i < n will be false, and your loop will terminate. 如果发生这种情况, i < n将为false,并且您的循环将终止。

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. 这两个循环的优点是它们是编写无限循环的常用方法,因此它们很容易被其他程序员读取。

The reason is that i is never decremented, so it does only 1 loop: 原因是i永远不会减少,所以它只做一个循环:

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

In order to decrement also i you should write: 为了减少i也应该写:

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

n will underflow somewhen because int is signed and has a value range of -2147483648 to 2147483647 for example (x86). 由于int是有符号的并且其值范围为-2147483648到2147483647(例如(x86)),因此n将下溢。 Somewhen n will get more positive than i. 有些人会比我更积极。

Edit: The loop has at most 1 iteration. 编辑:循环最多有1次迭代。

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). 编辑2:如果n的值为-2147483648,则循环将没有迭代,因为-2147483648 - 1将使值为正(两个补码整数运算)。 But this could never the case because the pre condition is that n may not be negative. 但这种情况永远不会发生,因为前提条件是n可能不是负数。

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. 例如,如果用户输入10,则第一次迭代i为9。 By the 2nd iteration n is decremented by 1 and i is still 9. 到第二次迭代时,n减1,i仍然是9。

Your for loop: for循环:

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

Translates into the following while loop: 转换为以下while循环:

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

The first clause of the for statement performs initialization. for语句的第一个子句执行初始化。 It is not repeated at each iteration, but only once. 它不会在每次迭代时重复,而只会重复一次。 Thus, i never changes value, and so the loop ends after a single iteration. 因此, i永远不会改变值,因此循环在单次迭代后结束。

This happens because you are decrementing n . 发生这种情况是因为你正在递减n At the second iteration i < n is false, you are exiting from the loop. 在第二次迭代中, i < n为假,您正在退出循环。

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)

your condition is wrong in the for loop..in your loop i is not changing only n is changing try this 你的条件在for循环中是错误的..在你的循环中我不改变只有n正在改变试试这个

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

you written perfect for loop... for(int i=n-1;i 你写的完美的循环... 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. 在循环中的另外两个条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM