在while循环的条件指定值 [英] Assigning value in while loop condition

查看:133
本文介绍了在while循环的条件指定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发现这块code对维基百科。

I found this piece of code on Wikipedia.

#include <stdio.h>

int main(void)
{
  int c;

  while (c = getchar(), c != EOF && c != 'x')
  {
    switch (c)
      {
      case '\n':
      case '\r':
        printf ("Newline\n");
        break;
      default:
        printf ("%c",c);
      }
  }
  return 0;
}

我很好奇作为条件while循环前pression:

I'm curious about expression used as condition for while loop:

while (c = getchar(), c != EOF && c != 'x')

这是很明显的它做什么,但我从来没有见过这种结构。这是具体的,而循环?如果不是,如何解析器/编译器确定哪些逗号分隔的前pression的一侧的同时循环返回布尔值?

It's quite obvious what it does, but I've never seen this construction before. Is this specific to while loop? If not, how does parser/compiler determine which side of comma-separated expression returns boolean value for while loop?

推荐答案

借助逗号操作符是一个二元运算符该评估其第一个操作数和丢弃的结果,然后计算第二个操作数,并返回该值。

The comma operator is a binary operator that evaluates its first operand and discards the result, it then evaluates the second operand and returns this value.

这也是一个顺序点,这意味着所有的副作用会先下一计算在code的一部分被执行。

It is also a "sequence point", which means that all side effects will be calculated before the next part of the code is executed.

这篇关于在while循环的条件指定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆