HACKER Q&A
📣 sargstuff

Discussions/examples of logical &&/|| vs. if..then/case?


Discussions/examples of logical &&/|| vs. if..then/case?


  👤 tacostakohashi Accepted Answer ✓
Perl went to town on having lots of logical operators and blurring (rather arbitrary) line between "flow control" and "logical operators", e.g.:

open( my $fh_file, "<", $file ) or die "Could not open file '$file' $!";

do_thing() unless predicate();

do_thing() if predicate();

do_thing() until predicate();

... and lots more.


👤 al2o3cr
What do you mean by "versus" here? The short-circuit operators behave like this pseudocode:

    expr1 || expr2

    if expr1 == false
      expr2
    else
      true
In languages with macros, they may even be _implemented_ like that.