Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Pascal

Idiom #252 Conditional assignment

Assign to the variable x the string value "a" if calling the function condition returns true, or the value "b" otherwise.

uses math;
x := ifthen(condition, a, b);

This is just shorthand, the actual code translates to the same code as the other 4-line implementation.
if condition then
  x := a
else
  x := b;
x := (if condition() then "a" else "b");

New implementation...
< >
programming-idioms.org