【问题标题】:WHILE loop shows a different output than FOR loopWHILE 循环显示与 FOR 循环不同的输出
【发布时间】:2015-12-02 19:33:03
【问题描述】:

据我所知for 循环和while 循环之间的区别如下:

为:

首先进行初始化,然后检查条件表达式,如果结果为TRUE,则仅执行语句部分,此循环一直持续到条件表达式结果FALSE

同时:

首先检查条件表达式,如果结果TRUE然后语句部分被执行,否则不执行,这个循环一直持续到条件表达式结果FALSE


今天我写了一个算法来检查 string 是否有重复,如果有,只打印那些不重复的:

#include<stdio.h>
#include<string.h>

int main(void){
    const char *str = "Mississippi";
    char tmp[15] = {0};
    size_t i=0,j=0,k=1;
    int found=0;

    tmp[0] = str[0];

    printf("Before = %s\n",str);
    while(str[i] != '\0'){
        for(j=0;tmp[j] != '\0';j++){
            if(tmp[j] == str[i]){
                found++;
            }
        }

        if(found==0){
            tmp[k]=str[i];
            k++;
        }

        found=0;
        i++;
    }

    tmp[strlen(tmp)] = '\0';
    printf("After  = %s\n",tmp);
    return 0;
}

输出:

Before = Mississippi
After  = Misp

现在看看如果替换 for 循环会发生什么:

for(j=0;tmp[j] != '\0';j++){
    if(tmp[j] == str[i]){
        found++;
    }
}

使用 `while 循环:

while(tmp[j] != '\0'){
    if(tmp[j] == str[i]){
        found++;
    }
    j++;
}

我明白了:

#include<stdio.h>
#include<string.h>

int main(void){
    const char *str = "Mississippi";
    char tmp[15] = {0};
    size_t i=0,j=0,k=1;
    int found=0;

    tmp[0] = str[0];

    printf("Before = %s\n",str);
    while(str[i] != '\0'){
        while(tmp[j] != '\0'){
            if(tmp[j] == str[i]){
                found++;
            }
            j++;
        }

        if(found==0){
            tmp[k]=str[i];
            k++;
        }

        found=0;
        i++;
    }

    tmp[strlen(tmp)] = '\0';
    printf("After  = %s\n",tmp);
    return 0;
}

但输出却不如预期:

Before = Mississippi
After  = Misp

但是:

Before = Mississippi
After  = Misisipi

为什么会这样?

【问题讨论】:

  • TLDR;请提供一个简短而甜蜜的MCVE 来说明问题。不要发布有效的代码。
  • @WeatherVane 抱歉,删除了示例中的部分。
  • 反对票 请解释一下,为什么?有没有机会看看谁投了我的票?
  • 将我标记为我的评论在上面-但这不是我的反对意见。
  • 我真的需要知道,这个问题有什么问题。这不对,这里发生了什么。

标签: c for-loop while-loop


【解决方案1】:

缺少的是 for 循环在第一次进入时将 j 初始化为 0。

虽然j 在声明时被初始化为0,但while 循环在另一个while 循环中被调用,因此它不会每次都重新初始化。

添加初始化:

    j=0;
    while(tmp[j] != '\0'){
        if(tmp[j] == str[i]){
            found++;
        }
        j++;
    }

你会得到:

Before = Mississippi
After  = Misp

【讨论】:

    【解决方案2】:

    因为while循环不会在最外层的while循环的每次迭代中初始化j=0

    【讨论】:

      【解决方案3】:

      请注意,while 循环不会将 j 初始化为 0

      去掉j的初始化为0并添加

      j = 0;
      

      在嵌套的while 循环前面。

      【讨论】:

        【解决方案4】:

        您永远不会使用 while 循环将 j 重置为 0。

        这一点:

        for(j=0;
        

        【讨论】:

          【解决方案5】:

          for loop

          for ( init-statement(optional) ; condition(optional) ;
                iteration_expression(optional) )
            statement
          

          上面的语法产生的代码相当于:

          {
            init_statement 
            while ( condition ) { 
              statement 
              iteration_expression ; 
            }
          }
          

          除了

          1. 由 init-statement 声明的名称(如果 init-statement 是一个声明)和由 condition 声明的名称(如果 condition 是一个 声明)在同一范围内(这也是 声明)。
          2. 语句中的continue会执行iteration_expression
          3. 空条件相当于while(true)

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2018-04-20
            • 1970-01-01
            • 1970-01-01
            • 2014-03-23
            • 2011-05-11
            • 2015-09-18
            • 2019-07-14
            相关资源
            最近更新 更多