문제

I have seen a kind of "poetic" code in some code base. Though it looks straight forward, just want to confirm, if it's in right direction.
In a very simple form:

bool foo ();  
bool bar ();  

int main () {
  foo() or bar();  // <--- line
}

Is the code at highlighted line as good as below snippet?

if(foo() == false)
  bar();

I am aware of , operator where all the statements are invoked, but unsure about or (equivalent to ||) operator.
Testing in g++, it gives expected output.

도움이 되었습니까?

해결책

Yes, The or operator is the text equivalent of ||.

So what you said is correct.

다른 팁

If the 1st condition is true the other one isn't evaluated anymore (in ||'s case) and therefore it's as you said:

if(foo() == false)
  bar();

The same thing applies to && when if the 1st condition is false, the second isn't evaluated anymore.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top