while循环有两个参数吗? [英] Does while loop have two Arguments?

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

问题描述

我的女士给了我一个要解决的问题。预测以下代码的输出。

  #include  
int main()
{
int i = 0,j = 0;
printf(Output is:);
while(i <5,j <10)//疑问:接受2个参数时如何?以及它是如何工作的?
{
i ++;
j ++;
}
printf(%d,%d \ n,i,j);
}

我认为这是一个语法错误。但是,当我试图运行时,它给了我输出。

 输出为:10,10 

但是如何?任何人都可以解释?



但是,如果我删除第一个printf语句 printf(Output is:); 并且运行它,我的防病毒给我一个警报,检测到 Trojan
但它是如何变成木马

解决方案

逗号运算符是一个二元运算符,它评估其第一个操作数并放弃结果,然后评估第二个操作数并返回此值。



所以在您的情况下,

 首先它会将i和j增加到5并丢弃。 
其次它将迭代我和我到10,并为您提供结果为10,10。



<你可以使用下面的代码进行确认:

pre $ while(i <5,j <10)//疑问:如何接受2个参数?以及它是如何工作的?
{
i ++;
j + = 2;
}


My ma'am gave me one question to solve. To predict the output of the following code.

#include <stdio.h>
int main()
{
    int i = 0, j = 0;
    printf("Output is : ");
    while (i < 5, j < 10)    // Doubt: how does while accept 2 arguments?? and how it works??
    {
        i++;
        j++;
    }
    printf("%d, %d\n", i, j);
}

I thought it was a syntax error. But when I tried to run, it gave me output.

Output is : 10, 10

But How? Can anyone explain?

But if I remove the first printf statement printf("Output is : "); and run it, my antivirus give me a alert that a Trojan is detected. But how it becomes a Trojan?

解决方案

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

so in your case,

First it will increment i and j upto 5 and discard.
Second it will iterate i and i upto 10 and provide you the result as 10, 10.

you can confirm by using the following code,

while (i < 5, j < 10)    // Doubt: how does while accept 2 arguments?? and how it works??
{
    i++;
    j+ = 2;
}

这篇关于while循环有两个参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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