Frage

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.

War es hilfreich?

Lösung

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

So what you said is correct.

Andere Tipps

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top