How to use ternary IF

Asked

Viewed 2,921 times

2

How would the following situation be using if ternary?

if StrToInt(Edit1.Text) < 30 then
      Edit2.Text := '30'
   else if StrToInt(Edit1.Text) in [30..50] then
      Edit2.Text := '40'
   else
      Edit2.Text := '50'
  • 1

    The latest versions received the ternary operator or you refer to the gambiarra of the IfThen?

  • Ifthen, for knowledge only.

2 answers

3


v = StrToInt(Edit1.Text);

Edit2.Text = IfThen(v < 30, '30', IfThen(v in [30..50], '40', '50'));

2

In programming languages, the expression :?, is known to be a Ternary Operator, several languages adopt it, but even in its latest Seattle 10 version, Delphi nay adopts.

But from Delphi7, there is the option to use the IfThen:

IfThen([Expressão boleana], [Se verdadeiro], [Se falso])

But you can’t do that:

y:= IfThen(x <> 0, 1/x, 0);

Being necessary to use the traditional way:

if x <> 0 then y := 1/x else y := 0;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.