0
if(expression1 or expression 2)
{
do something
}

If 'expression1' returns true, does the compiler starts to execute 'do something' or it evaluates the second expression too?

user30771
  • 109
  • 6

1 Answers1

2

No, expression2 will not be evaluated if expression1 is true.

This is because or is short-circutted in perl: once the result of the entire expression is known, evaluation stops. Evaluation occurs from left to right.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Thank you for answering so fast! :) – user30771 Feb 24 '16 at 14:36
  • A 2 minute wait is an eternity on some tags ;-) It's true also in C, C++, and Java. The only language I can think of that **doesn't** do this is VBA. – Bathsheba Feb 24 '16 at 14:37
  • 1
    This is true in many languages, and is often used as a feature. `./configure && make && make test && make install` for example :) – Sobrique Feb 24 '16 at 14:40
  • Or in Perl: `open(...) or die $!;` – ikegami Feb 24 '16 at 15:17
  • 1
    @Bathsheba, Apparently, C++ **doesn't** short-circuit when the operators are overloaded ([ref](http://stackoverflow.com/a/628554/589924)). – ikegami Feb 24 '16 at 15:18
  • 1
    @ikegami: you're absolutely right. The reason is simple: the overloaded function takes two parameters and both parameters are evaluated before the function body is reached. I was asked this at interview once. – Bathsheba Feb 24 '16 at 15:21