简体   繁体   English

c while循环在条件之前停止

[英]c while loop stop before condition

I have one exercise that I have to calculate fibonacci numbers until 100 and then print them. 我有一个练习,我必须计算斐波那契数直到100,然后再打印出来。

I have made this code: 我做了这段代码:

    #include <stdio.h>
    #include <stdlib.h>

    int main()
    {

        int i=2,fibonacci = 0, fParcialone = 1, fParcialtwo = 0;
        printf("The %2dst Fibonacci number is %3d\n", i-1, fibonacci+1);
        while (fibonacci <= 100){

            fibonacci = fParcialone+fParcialtwo;
            printf("The %2dst Fibonacci number is %3d\n", i, fibonacci);
            fParcialtwo = fParcialone;
            fParcialone = fibonacci;
            i++;
        }
        return 0;
    }

As you can see, it prints the 12th number and this number is greater than 100. I understand why it is doing that. 如您所见,它将打印第12个数字,并且该数字大于100。我知道它为什么这样做。

One alternative is to make this: 一种替代方法是:

    #include <stdio.h>
    #include <stdlib.h>

    int main()
    {

        int i=2,fibonacci = 0, fParcialone = 1, fParcialtwo = 0;
        printf("The %2dst Fibonacci number is %3d\n", i-1, fibonacci+1);
        while (fibonacci <= 100){

            fibonacci = fParcialone+fParcialtwo;
            if (fibonacci > 100){
                return 0;
            }
            printf("The %2dst Fibonacci number is %3d\n", i, fibonacci);
            fParcialtwo = fParcialone;
            fParcialone = fibonacci;
            i++;
        }
        return 0;
    }

And now it works but now, In every loop it as to make two comparisons and not just one and I believe that this way it uses to much "processor time" (in this example its to little but on a bigger scale it probably will make difference). 现在它可以工作了,但是现在,在每个循环中它要进行两次比较,而不仅仅是一次比较,我相信这样一来,它会占用大量的“处理器时间”(在此示例中,它花费的时间很少,但可能会更大)区别)。

Is there a better way to do this? 有一个更好的方法吗?

favolas 最爱

I think that you can use a do ... while(cond). 我认为您可以使用do ... while(cond)。 It should looks like : 它应该看起来像:

#include <stdio.h>
#include <stdlib.h>

int main()
{

    int i=2,fibonacci = 0, fParcialone = 1, fParcialtwo = 0;
    printf("The %2dst Fibonacci number is %3d\n", i-1, fibonacci+1);
    fibonacci = fParcialone+fParcialtwo;
    do {
        printf("The %2dst Fibonacci number is %3d\n", i, fibonacci);
        fParcialtwo = fParcialone;
        fParcialone = fibonacci;
        i++;
    } while ((fibonacci = fParcialone+fParcialtwo) <= 100);
    return 0;
}

The processing time of one simple if-statement is quite negligible. 一个简单的if语句的处理时间可以忽略不计。

If you insist on making the code better looking one option is simply replacing the while condition with: 如果您坚持让代码看起来更好,那么一个选择就是简单地将while条件替换为:

while (fParcialone + fParcialtwo <= 100)

or change the loop to a do-while loop. 或将循环更改为do-while循环。

The simplest solution is: 最简单的解决方案是:

    ...
    while (1) {

        fibonacci = fParcialone+fParcialtwo;
        if (fibonacci > 100){
            return 0;
        }
        printf("The %2dst Fibonacci number is %3d\n", i, fibonacci);
        fParcialtwo = fParcialone;
        fParcialone = fibonacci;
        i++;
    }
    ...

you can calculate the first number outside of loop ,and each time calculate it in the end of the loop. 您可以计算循环外的第一个数字,每次都在循环末尾进行计算。 like this: 像这样:

    #include <stdio.h>
    #include <stdlib.h>

    int main()
    {

        int i=2,fibonacci = 0, fParcialone = 1, fParcialtwo = 0;
        printf("The %2dst Fibonacci number is %3d\n", i-1, fibonacci+1);

        fibonacci = fParcialone+fParcialtwo;
        while (fibonacci <= 100){

            printf("The %2dst Fibonacci number is %3d\n", i, fibonacci);
            fParcialtwo = fParcialone;
            fParcialone = fibonacci;
            i++;
            fibonacci = fParcialone+fParcialtwo;
        }
        return 0;
    }

My C is a couple of decades rusty so I won't attempt valid code, but to solve this problem you basically need to break out of the loop in the middle to remove the test. 我的C语言已经生锈了几十年,所以我不会尝试使用有效的代码,但是要解决此问题,您基本上需要在中间退出循环以删除测试。

while(true)
{
   calculate
   if (fib <= 100)
   {
       output
   }
   else
   {
       break; // Exit from the loop
   }
}

However - in truth, the extra comparison just isn't an issue in terms of performance. 但是-实际上,额外的比较就性能而言不是问题。 Even at scale the hit should be vanishingly small in comparison with other operations so really you have far more important things to worry about in the general case. 与其他操作相比,即使是大规模操作,命中率也应消失得很小,因此在一般情况下,您确实要担心得多的重要事情。 (There will always be specific cases where that might not be true.) (在某些特定情况下,可能并非如此。)

Sure there is. 当然可以。

When you run into something like this, if you feel that you need to put a condition inside the loop like that, it can probably go in the conditional check that happens in the while statement and whatever follows after your inner conditional is what should be at the very start of the while loop. 当您遇到这样的情况时,如果您觉得需要在这样的循环中放置一个条件,则它可能可以进入在while语句中发生的条件检查,而内部条件之后的所有内容应为while循环的开始。 Then just rearrange everything as needed (like on a circle) and add initialization statements as needed before the loop to get it started. 然后,只需根据需要重新排列所有内容(如画一个圆圈),并在循环开始之前根据需要添加初始化语句即可开始。

int main()
{

    int i=2,fibonacci = 0, fParcialone = 1, fParcialtwo = 0;
    printf("The %2dst Fibonacci number is %3d\n", i-1, fibonacci+1);
    //initialize
    fibonacci = fParcialone+fParcialtwo;
    while (fibonacci <= 100){
        printf("The %2dst Fibonacci number is %3d\n", i, fibonacci);
        fParcialtwo = fParcialone;
        fParcialone = fibonacci;
        fibonacci = fParcialone+fParcialtwo;
        i++;
    }
    return 0;
}

If you want to keep most of the structure of your code, try : 如果您想保留大部分代码结构,请尝试:

#include <stdio.h>
#include <stdlib.h>

int main()
{

    int i=1,fibonacci = 1, fParcialone = 1, fParcialtwo = 0;
    while (fibonacci <= 100){
        printf("The %2dst Fibonacci number is %3d\n", i, fibonacci);
        fibonacci = fParcialone+fParcialtwo;
        fParcialtwo = fParcialone;
        fParcialone = fibonacci;
        i++;
    }
    return 0;
}

We already know the first Fibonacci number is 1 so you don't even need to calculate it before the condition statement. 我们已经知道第一个斐波那契数是1,因此您甚至不需要在条件语句之前进行计算。 I know it is different code than yours, was just having some fun, but hopefully it gets the idea across. 我知道它与您的代码不同,只是在获得一些乐趣,但希望它能使您的想法得到理解。

#include <stdio.h>
#include <stdlib.h>

int Fibonacci(int n)
{
    if(n <= 1) return n;  
    return Fibonacci(n-2) + Fibonacci(n-1);
}

int main()
{
    int i = 1, fibonacci = 1;
    do 
    {
        printf("The %2dst Fibonacci number is %3d\n", i, fibonacci);
        fibonacci = Fibonacci(++i);
    } while (fibonacci <= 100);

    return 0;
};

The key is calculating and checking the 12th Fibonacci number in this case, before printing it to the screen. 在这种情况下,关键是计算并检查第12个斐波那契数,然后再将其打印到屏幕上。 Either way, one if-check won't have any noticeable processing time and it is negligible. 无论哪种方式,一个if-check都不会有任何明显的处理时间,并且可以忽略不计。

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

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