【问题标题】:Why does it not work to use multiple "if"s but it does work to use "if"s and "else if"s inside a while loop?为什么使用多个“if”不起作用,但在while循环中使用“if”和“else if”却起作用?
【发布时间】:2018-07-09 18:52:59
【问题描述】:

这是我没有使用 else if 的代码:

#include <stdio.h>

main()
{
    long s = 0, t = 0, n = 0;
    int c;
    while ((c = getchar()) != EOF)
        if (c == ' ')
            ++s;
        if (c == '\t')
            ++t;
        if (c == '\n')
            ++n;
    printf("spaces: %d tabulations: %d newlines: %d", s, t, n);
}

这是使用else if的代码:

#include <stdio.h>

main()
{
    long s = 0, t = 0, n = 0;
    int c;
    while ((c = getchar()) != EOF)
        if (c == ' ')
            ++s;
        else if (c == '\t')
            ++t;
        else if (c == '\n')
            ++n;
    printf("spaces: %d tabulations: %d newlines: %d", s, t, n);
}

出于某种原因,不使用 else if 不起作用。是什么原因?我知道使用 if 会一个一个地完成,而使用 else if 会在第一个正确的语句处停止。这在性能上有差异。无论如何,在这个特定(如果不是其他)while 循环中不使用 else if 似乎不起作用。

谢谢。

【问题讨论】:

  • C 不是 Python。缩进并不意味着所有这些 if 都是同一块的一部分。
  • 整件事都没有{ }
  • 第一个示例“有效”,因为第一个“if”语句仅由 while 包裹。第二个例子完全被 while 包裹起来。我会说第二个例子有效。首先没有。
  • 你可能想读这个,不确定它是否是正确的重复stackoverflow.com/questions/11734604/…
  • 谢谢大家。这完全表明忘记一个弯曲的括号是多么重要。这意味着我可以在没有曲括号的情况下使用“if”和“else if”,对吗? PS我是C新手,很抱歉这么愚蠢的帖子。

标签: c if-statement while-loop getchar


【解决方案1】:

因为你忘记了while 周围的大括号,所以它只循环第一个if 语句,然后退出循环并计算另外两个if 语句。

另外,你为什么不使用switch 语句?

while ((c = getchar()) != EOF) {
    switch(c) {
       case ' ':
          ++s;
          break; 
       case  '\t':
          ++t;
          break;
       case '\n':
          ++n;
          break;
    }
}
printf("spaces: %d tabulations: %d newlines: %d", s, t, n);

【讨论】:

    【解决方案2】:

    来自standard(只选择了语法规则的相关部分)

       iteration-statement: while ( expression ) statement
    
       statement: selection-statement
    
       selection-statement:
                 if ( expression ) statement
                 if ( expression ) statement else statement
    

    您正在编写一个迭代语句 - 它由 while(expression) 和一个语句组成。在您的情况下,该语句是选择语句 - 现在检查它。除非您使用 else ifelse,否则它不是一个单一的语句 - 而是多个语句,除了 while 语句中的一个之外,其余的都在它之外。

    你的代码基本上就是这个意思

      while ((c = getchar()) != EOF){ <--- the selection statement
        if (c == ' ')
            ++s;
      }<-- 
      if (c == '\t')
            ++t;
      if (c == '\n')
            ++n;
    

    在块中加上括号和缩进会让你避免这种错误。

    【讨论】:

      【解决方案3】:

      正确缩进,您的第一个程序如下所示:

      #include <stdio.h>
      
      main()
      {
          long s = 0, t = 0, n = 0;
          int c;
          while ((c = getchar()) != EOF)
              if (c == ' ')
                  ++s;
          if (c == '\t')
              ++t;
          if (c == '\n')
              ++n;
          printf("spaces: %d tabulations: %d newlines: %d", s, t, n);
      }
      

      while 循环的主体是一条语句。

      if ... else if ... else if ... else 都形成了一个大声明。通过将条件分成几个语句(ififif),您已经将除第一个之外的所有条件移出 while 循环。

      为避免此问题,请始终使用复合语句(即块:{ ... })作为 whileif 语句的主体。

      顺便说一句,main() 自 1999 年以来一直不是有效的 C。它应该是 int main(void)

      【讨论】:

      • 谢谢,非常感谢。此外,我正在从 Brian Kernighan 的原著中学习 C,它主要用于没有返回数据类型,就像 1980 年代一样。我使用 Visual Studio 编译它,它没有给出任何错误。不过,感谢您的更正。
      猜你喜欢
      • 2021-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多