【问题标题】:How does the while loop work here?while 循环在这里是如何工作的?
【发布时间】:2015-06-06 05:20:14
【问题描述】:

虽然我对C有基本的了解,但我遇到了以下代码。我仍然了解以下代码的逻辑。

  1. 循环如何终止以及循环何时终止——在什么条件下?

  2. 最后counter变为零,但是为什么循环没有继续执行-1,-2而是终止了?

代码:

#include <stdio.h>

void func(void);
static int count = 5;
int main()
{
    while (count--)
    {
        func();
    }
    return 0;
}

void func(void)
{
    static int i = 5;
    i++;
    printf("i is %d and count is %d\n", i, count);
}

输出:

i is 6 and count is 4
i is 7 and count is 3
i is 8 and count is 2
i is 9 and count is 1
i is 10 and count is 0

【问题讨论】:

  • while(0) 将评估为假。这将打破 while 循环

标签: c termination


【解决方案1】:

while 循环的条件 count-- 在计算表达式之前当 count 的值为 1 时计算为 1。表达式求值的副作用是count 的值减少了1,即它的值变为0

这就是你看到的原因

i is 10 and count is 0

作为输出。

下一次循环条件求值时,表达式的值为0count的值设置为-1。由于表达式的值为0,循环执行停止。

【讨论】:

    【解决方案2】:

    这里循环在 count 变为 0 时终止,因为在 C 中,0 在 Java(布尔类型)中的行为类似于 false,因此条件为 false。循环退出。

    当计数=

    0 — 循环条件为假

    非 0 — 循环条件为真

    【讨论】:

    • 你需要非常小心。条件是while (count--),所以在count1开始的迭代中,条件的计算结果为真(因为1不是0),但变量count递减为0和循环体中的值为0
    • 是的,因为它的后期递减,首先取值然后递减完成
    【解决方案3】:

    while(term)

    当 term(condition/variable/function) 的计算结果为 0 时,while 循环终止。

    count-- 返回 count 的值,然后递减。

    所以当 count--return zero 循环终止时。当 count 的值为 0 时。 另见评论。

    C 取 0 为假,其余为真,因此当条件评估为假 (0) 时,while 循环将中断。

    【讨论】:

    • 其实循环在count变为零后进行最后一次迭代,因为while (count--)求值count然后递减,所以有一个循环count1在它递减到零之前,但循环继续进行一次迭代,因为递减后表达式的值为1,其计算结果为真,因此仍然满足循环条件。
    【解决方案4】:

    count-- 是post-decrement。因此,count 的值不会在while 条件下立即递减。之后会递减(在评估while 条件之后)。因此,当count = 1count-- 完成时,while 会继续运行。但是在输入whilecount becomes 0之后,因此,下一次条件评估到达时,while中断。

    简单来说,count is equal to 5 并且您希望它运行 5 次迭代(在简单流程下),因此它运行 4 to 0。如果你想换一种方式,你可以这样做,试试这个,自己看看

    while( 0 != count )
    {
       func() ;
       count-- ;
    }
    

    【讨论】:

      【解决方案5】:

      在 C 和 C++ 中,如果使用该表达式代替条件,则任何结果为零 (0) 的表达式都将被评估为 false。看——

      if(0){
        //never reached; never executed
      }  
      

      类似地,表达式中的任何非零都将被评估为true -

      if(5){
         //always reached and executed
      }  
      

      这个事实适用于while-loop中的条件 -

      int i = 5;
      while(i){
        //do something
      
        i--;
      }
      

      【讨论】:

        【解决方案6】:

        问题1:循环如何终止,循环在什么条件下终止。

        ans: while 循环以 count = 5 开始。循环条件满足,因为 count 非零。但立即计数值由于 count--,减一并变为 count = 4。所以输出是

        i 是 6,count 是 4

        直到 count =1,它继续。Count =1 也满足循环条件。但立即计数——发生,现在计数=0。所以输出是

        i 为 10,计数为 0

        所以在下一个检查循环条件不满足(Count = 0)并且循环终止。

        问题2:最后计数器变为零,但是为什么循环不继续存在到-1,-2而是终止了?

        ans: 当循环条件变为零时,它会创建一个 FALSE 条件。所以它会在那里终止。

        注意:条件块中只有零值会终止循环。如果它小于零,则不会。

        检查一下。

        void func(void);
        static int count = -5;
        main()
        {
            while (count++)
            {
                func();
            }
            return 0;
        }
        
        void func(void)
        {
            static int i = 5;
            i++;
            printf("i is %d and count is %d\n", i, count);
        }
        

        创建输出为

        i is 6 and count is -4
        i is 7 and count is -3
        i is 8 and count is -2
        i is 9 and count is -1
        i is 10 and count is 0
        

        【讨论】:

          【解决方案7】:

          while 表达式是 boolean; 整数表达式count--因此被隐式转换为布尔值,这样任何非零值都是true,零是false

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-12-27
            • 2016-09-27
            • 2012-08-20
            • 1970-01-01
            • 2012-05-17
            • 2016-10-27
            • 2018-07-22
            • 2011-07-18
            相关资源
            最近更新 更多