Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does while loop have two Arguments?

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?

like image 988
Shreyash S Sarnayak Avatar asked Nov 25 '14 18:11

Shreyash S Sarnayak


People also ask

Can while loop have 2 conditions?

Using multiple conditions As seen on line 4 the while loop has two conditions, one using the AND operator and the other using the OR operator. Note: The AND condition must be fulfilled for the loop to run. However, if either of the conditions on the OR side of the operator returns true , the loop will run.

What are the 3 arguments of the for loop?

With three arguments, the sequence starts at the first value, ends before the second argument and increments or decrements by the third value.

Can you have two conditions in a while loop Java?

We can have multiple conditions with multiple variables inside the java while loop. In the below example, we have 2 variables a and i initialized with values 0.

Can you run 2 while loops in Python?

Like other programming languages, Python also uses a loop but instead of using a range of different loops it is restricted to only two loops “While loop” and “for loop”. While loops are executed based on whether the conditional statement is true or false.


1 Answers

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;
}
like image 95
Sridhar DD Avatar answered Oct 20 '22 07:10

Sridhar DD