0

Is it possible to write a one-line if then else statement in a language that supports short-circuiting? It doesn't have to be language specific, just pseudocode.

In case you didn't know, short-circuiting means that a language will evaluate exp1 || exp2 || exp3 || exp4... (|| is the or operator) by first executing exp 1, then if it returns true, execute exp 2, and so on... However, the moment exp n returns false, it does not continue evaluating exp n+1 and anything after that.

AMACB
  • 1,290
  • 2
  • 18
  • 26
  • It will work in C and C++ for sure. Have a look at this for a full list of languages: http://rosettacode.org/wiki/Short-circuit_evaluation#C – Jerry Jeremiah Oct 29 '15 at 22:35

3 Answers3

1

Let's say you want to express:

if p then f() else g()

Using only || and &&, both short circuiting. You can do that like this:

(p && ( f() || 1 )) || g()

To test it, a quick script:

$ perl -E '$p=1; ($p && ( f() || 1 )) || g(); sub f { say "f() called" } sub g { say "g() called" }'
f() called
$ perl -E '$p=0; ($p && ( f() || 1 )) || g(); sub f { say "f() called" } sub g { say "g() called" }'
g() called
$ 
asjo
  • 3,084
  • 2
  • 26
  • 20
0

In C, C++, C# and Java, you can do this (all 4 support short circuiting):

a = a>b ? a : b;

This would assign a the bigger of the two, either a or b.

This is called the ternary operator. First comes the condition to test a>b, then a question mark, then the true value (e.g. a), then a colon, then the false value (e.g. b).

Icemanind
  • 47,519
  • 50
  • 171
  • 296
  • I'd say you can't; you're using a different operator, aren't you here? And, BTW, where's the short circuitry? – watery Oct 29 '15 at 22:31
  • @watery - In C, C++, C# and Java, short circuiting automatically occurs. – Icemanind Oct 29 '15 at 22:31
  • I know, I meant I don't understand where the short circuitry could happen in your example. – watery Oct 29 '15 at 22:39
  • @watery I guess short circuiting for the ternary operator means that either the part after the ? gets executed or the part after the :, but not both. It is probably clearer if you think of an example like a>b ? f() : g() - either f() or g() gets called, but not both. – asjo Oct 29 '15 at 23:52
  • @asjo I still don't see the short circuitry there: to my understanding, that's how the ternary operator behaves, and that behaviour doesn't depend on short cirtuitry being available in the language. – watery Oct 30 '15 at 07:17
  • @watery short circuiting for "a() || b()" means that b() will ONLY be called if a() is false. If || didn't short circuit, then both a() and b() would be called, before evaluating the ||. I was just guessing that what was meant was something similar to that. But you're right it doesn't really make sense. – asjo Oct 30 '15 at 21:10
0

You can't by simply short circuitry. That's meant to shorten the number of conditions being evaluated by skipping all conditions after the first that would make the whole condition true or false (based on the operator).

E.g.:

boolean x = true || 1 == 2;

The || evalutation won't check the value of 1 == 2, since true will already turn the || true, no matter what follows.

In a similar way for &&:

boolean y = 2 < 3 && 5 > 6;

To expand a bit on that, I could write:

boolean y = 2 < 3 && 5 > func();

In either case, the second half of the condition won't be checked (and func() won't be invoked) because the first half gives false.

There's no way to do something else, only with this behaviour.

watery
  • 5,026
  • 9
  • 52
  • 92