Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which condition is true in While?

Tags:

c

c99

Sorry if this is too simple. I want to know which of the conditionals is happening exactly. Is there a way more able to capture this without repeating them inside the block with if structures? I am using C language.

while ( l < 0 || l > 2 || c < 0 || c > 2 )
like image 978
Artur Rodrigues Aguiar Avatar asked Jan 28 '23 04:01

Artur Rodrigues Aguiar


1 Answers

You could use comma expressions, i.e. something like (expr1,expr2), which are always evaluated left to right with a sequence point at each ,; So you may rely on that expr1 is evaluated before expr2, whereas the latter serves as the comma expression's result then.

With that, the following should work, and x will bee in the range of 0..3, depending on which condition got true:

int x;
while ( (x=0,l < 0) || (++x,l > 2) || (++x,c < 0) || (++x,c > 2) )
like image 68
Stephan Lechner Avatar answered Feb 07 '23 17:02

Stephan Lechner