1
main()
{
    int i=0,j=0;
    while(i<5,j<10)
    {
        i++;
        j++;
    }
    printf("%d,%d,",i,j);
}

output: 10,10

int main(){
    int x=2,y=2;
    while(x<=5,y<=3)
         printf("%d %d ",++x, ++y);
    return 0;        
}

output: 3 3 4 4

In the first code how can output is coming as 10,10? can anyone explain,but the output for the second code is 3344, Are both running on different logic?

Badda
  • 1,329
  • 2
  • 15
  • 40
Maths Maniac
  • 83
  • 1
  • 7

7 Answers7

5

What happens is that you are using the comma operator within the condition of the while.

comma operator works evaluating all operands but throwing the result away except for the last one.

Your while is equivalent to the following:

 while(j<10)
{
    i++;
    j++;
}

Take a look at this answer: What does the comma operator , do?

Davide Spataro
  • 7,319
  • 1
  • 24
  • 36
4

The problem is that you are using the comma operator in your while loop instead of the logical and. The expression i<5,j<10 means: Perform statement i<5 but throw away the result and then perform statement j<10 which will be the result of the whole expression. So in the end the first part isn't used as a check in your while condition. The reason why it's different in your second case is because of the ordering of your statements.

What your probably meant was:

while(i<5 && j<10)
     // do your stuff

Now both conditions have to be true.

Sebastian Stern
  • 632
  • 4
  • 15
1

You are coding in c++ not python-perl, this:

while(x<=5,y<=3)

is equivalent to while (y <= 3)

please use the boolean operators like && or ||

while(x<=5 && y<=3)
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
1

In this snippet i<5,j<10 you are using the built-in comma operator. What you need to use is logical and: && or and, like this:

while (i < 5 and j < 10)

The built-in comma operator evaluates all of the operands and throws all but the last away. The reason the second code works is because the limit of y is lower than that of x and the condition on y is the stop condition in the while-loop. Thus the loop is correctly stopped by the condition on y, since while (x <= 5, y <= 3) is equivalent to while (y <= 3).

Jonas
  • 6,915
  • 8
  • 35
  • 53
1

In C, expressions separated by comma , are evaluated left to right (atleast on gcc) and the value of whole expression e:

e = e1, e2, .. , en;

is same as en. Thus all the expression e1, ... , en-1 are discarded. Thus they only have uses in producing side-effects or macros. Thus here i<5, j<10 is essentially nothing but j<10 and same in second case. Its just while(y<=3) because x<=5, y<=3 always evaluates to y<=3.

Abhishek Kumar
  • 729
  • 6
  • 20
1

People are suggesting him to use && operator, but he wants an explanation for why he is getting 10 10 from Example 1 and 3 3 4 4 from Example 2. Yes both logic is different. in Example 1 the while loop iterates from 0 to 9 and at 10th value it quits(Hope you understood how comma-operator works from all the answers). That's why it prints 10 10 for i and j. and in Example 2 it starts from 2 one pre-increment and the value gets 3 condition is true. it loops one more time and at j=4 it fails and 3 3 4 4 values get printed. Hope it helps :)

Vishwajeet Vishu
  • 492
  • 3
  • 16
0

Change your while loop to :

while(i<5 && j<10)
{
    i++;
    j++;
}

as you want both conditions to be true. Right now you are using the comma operator, which evaluates all the operands but only keeps the last result.

Marievi
  • 4,951
  • 1
  • 16
  • 33