简体   繁体   English

C-while循环挂在条件上

[英]C - while loop hanging on the condition

I'm a newbie learning code so this might be pretty simple but I can't understand what is happening. 我是一个新手,正在学习代码,所以这可能很简单,但是我不明白发生了什么。

When I try to run the following code it seems to hang on the while loop condition and doesn't execute any code after that. 当我尝试运行以下代码时,它似乎挂在while循环条件上,此后不执行任何代码。 Is the != operator not suited for this? !=运算符是否不适合此操作?

I also tried a do {} while and everything ran fine but as soon as I set the operator to 'E' , again the condition seems to hang there and doesn't get out of the loop. 我也尝试了一个do {} while并且一切运行良好,但是一旦我将运算符设置为'E' ,条件就好像挂在那儿了,并没有脱离循环。

#include <stdio.h>

int main(void) {
    float n, content;
    char operator;

    n = content = 0;
    operator = 'a';

    printf("Begin Calculations:\n");

    while (operator != 'E');
    {
        scanf("%f %c", &n, &operator);

        switch (operator) {
        case 's':
        case 'S':
            content = n;
            printf("= %f\n", content);
            break;

        case '+':
            content = content + n;
            printf("= %f\n", content);
            break;

        case '-':
            content = content - n;
            printf("= %f\n", content);
            break;

        case '*':
            content = content * n;
            printf("= %f\n", content);
            break;

        case '/':
            content = content / n;
            printf("= %f\n", content);
            break;

        case 'e':
        case 'E':
            printf("End of calculations\n");
            operator == 'E';
            break;

        default:
            printf("Invalid input\n");
            break;
        }
    }
    return 0;
}

Remove the semicolon at the end of the while statement: 在while语句的末尾删除分号:

while(operator != 'E')
{

The semicolon ends the body of the while statement, in other words it behaves as if you would write this: 分号结束while语句的主体,换句话说,它的行为就像您将编写以下代码:

while(operator != 'E')
{
}

causing an infinite loop. 造成无限循环。

  1. You need to remove the semi-colon at the end of while 您需要在一会结束while删除分号
  2. You need to take into account both cases 您需要考虑两种情况

So change 所以改变

while(operator != 'E');

to

while(operator != 'E' && operator != 'e')

You have a classic bug in your while loop: while循环中,您有一个经典的错误:

while (operator != 'E');
{

The ; ; after the condition is parsed as en empty statement, hence your while runs forever if operator is different from 'E' . 在条件被解析为空语句之后,因此如果operator不同于'E'那么while将永远运行。

Using the classic Kernighan and Ritchie brace style makes this kind of bug obvious and much less likely to occur: 使用经典的Kernighan和Ritchie大括号样式可以使这种错误显而易见,并且不太可能发生:

while (operator != 'E') {

Also note that you should exit your loop from the switch statement instead of testing for E in the while condition, this would allow for both e and E to be handled correctly (your statement operator == 'E'; is a no op, it should be written operator = 'E'; . Also check the return value of scanf to avoid looping endlessly at end of file. 还要注意,您应该从switch语句退出循环,而不是在while条件下测试E ,这将允许eE都得到正确处理(您的语句operator == 'E';是no op,它是应该写为operator = 'E'; scanf还要检查scanf的返回值,以避免在文件末尾无限循环。

Here is an improved version: 这是一个改进的版本:

#include <stdio.h>

int main(void) {
    float n, content;
    char operator;
    int active = 1;

    n = content = 0;

    printf("Begin Calculations:\n");

    while (active && scanf("%f %c", &n, &operator) == 2) {
        switch (operator) {
        case 's':
        case 'S':
            content = n;
            printf("= %f\n", content);
            break;

        case '+':
            content = content + n;
            printf("= %f\n", content);
            break;

        case '-':
            content = content - n;
            printf("= %f\n", content);
            break;

        case '*':
            content = content * n;
            printf("= %f\n", content);
            break;

        case '/':
            content = content / n;
            printf("= %f\n", content);
            break;

        case 'e':
        case 'E':
            printf("End of calculations\n");
            active = 0;
            break;

        default:
            printf("Invalid input\n");
            break;
        }
    }
    return 0;
}
while(operator != 'E');

The correct syntax is while (...) { ... } without semicolon or do { ... } while (...); 正确的语法是while (...) { ... }不带分号,或者do { ... } while (...); with. 用。

printf("End of calculations\n");
operator == 'E';

The assignment operator is a single = . 赋值运算符是一个= Change == into = and it should be fine. ==更改为= ,应该没问题。

您需要从while循环中删除分号,因为分号意味着语句结束,因此您的程序不会进一步执行

couple of things. 几件事。 1- operator is a reserve word, you should change the variable name 2- (semicolon ;) after while() 1-运算符是保留字,您应该在while()之后更改变量名称2-(分号;)。

Check update code: 检查更新代码:

        #include <stdio.h>

        int main(void)
        {
          float n, content;
          char operator1;

          n = content = 0;
          operator1 = 'a';

          printf("Begin Calculations:\n");

          while(operator1 != 'E')
          {
            scanf("%c", &operator1);

            switch(operator1)
            {
              case 's':
              case 'S':
            content = n;
            printf("= %f\n", content);
            break;

              case '+':
            content = content + n;
            printf("= %f\n", content);
            break;

              case '-':
            content = content - n;
            printf("= %f\n", content);
            break;

              case '*':
            content = content * n;
            printf("= %f\n", content);
            break;

              case '/':
            content = content / n;
            printf("= %f\n", content);
            break;

              case 'e':
              case 'E':
            printf("End of calculations\n");
            operator1 = 'E';
            break;

              default:
            printf("Invalid input\n");
            break;
            }
          }
          return 0;
        }

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

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