vijay kumar
vijay kumar

Reputation: 23

while loop check for two values seperated by comma

I have come across some code where it looks like this.

while(1, N) 

where N can an integer ranging from 0 - 100.

Can somebody tell how can we use while loop like that.

Upvotes: 0

Views: 83

Answers (2)

ouah
ouah

Reputation: 145899

while(1, N)

is equivalent to

while(N)

as the comma operator yields the value of the right operand. So using the first form is useless.

If you want to write a loop that is run from 1 to N (including), you can use a for loop:

for (int i = 1; i <= N; i++)

Upvotes: 2

Sourav Ghosh
Sourav Ghosh

Reputation: 134356

As per the C11 standard document, chapter 6.5.17, comma operator,

The left operand of a comma operator is evaluated as a void expression; there is a sequence point between its evaluation and that of the right operand. Then the right operand is evaluated; the result has its type and value.

So, essentially,

while(1, N)

is same as

 while(N)

Upvotes: 3

Related Questions