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

Idiom #131 Successive conditions

Execute f1 if condition c1 is true, or else f2 if condition c2 is true, or else f3 if condition c3 is true.
Don't evaluate a condition when a previous condition was true.

if (c1) {
  f1();
} else if (c2) {
  f2();
} else if (c3) {
  f3();
}
switch (true) {
  case c1:
    f1();
    break;
  case c2:
    f2();
    break;
  case c3:
    f3();
    break;
}
c1 ? f1 : c2 ? f2 : f3
if      (c1) f1;
else if (c2) f2;
else if (c3) f3;
cond do
  c1 -> f1
  c2 -> f2
  c3 -> f3
end
switch {
case c1:
	f1()
case c2:
	f2()
case c3:
	f3()
}
if c1 then f1
else if c2 then f2
else f3
if (c1) {
   f1();
} else if (c2) {
   f2();
} else if (c3) { 
   f3();
}
if ($c1) {
  f1();
} else if ($c2) {
  f2();
} else if ($c3) {
  f3();
}
if c1 then f1 else if c2 then f2 else if c3 then f3;
if ($c1)    { $f1 }
elsif ($c2) { $f2 }
elsif ($c3) { $f3 }
f1 if c1 else f2 if c2 else f3 if c3 else None
case
  when c1
    f1
  when c2
    f2
  when c3
    f3
end
if c1 { f1() } else if c2 { f2() } else if c3 { f3() }
match true {
    _ if c1 => f1(),
    _ if c2 => f2(),
    _ if c3 => f3(),
    _ => unreachable!(),
}
if (c1) {
  f1()
} else if (c2) {
  f2()
} else if (c3) {
  f3()
}
true match {
  case _ if c1 => f1()
  case _ if c2 => f2()
  case _ if c3 => f3()
}
(cond (c1 (f1))
      (c2 (f2))
      (c3 (f3)))

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

programming-idioms.org