### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: IIF()  (Read 10804 times)

#### Michael

• New member
• Posts: 7
##### IIF()
« on: June 11, 2009, 09:39:54 am »
Is there an IIF() method someone can help me find?  Example from a C dialec I use:

Code: [Select]
`// ------------------------------// C-ish syntax// you can see that that our function returns a string// we are testing to see if name__ is set, if it is we will return it, if it isnt we will return "(unset name)"nomask string query_name() {    return name__ ? name__ : "(unset name)";}`

Code: [Select]
`// ------------------------------// here is a more complicated example// here its being used in-line to pre-pend a space to the arg variable, but only if it existedif(body__->do_command(query_verb() + (arg ? " " + arg : "")))// C-ish syntax, example of calling methodsb = ((x = 100)? x : z_func() );`
b will equal x if its 100, other wise it will equal whatever is returned by z_func()

Is there anything like this in Pascal?  I need the function to be able to call methods also.

#### theo

• Global Moderator
• Hero Member
• Posts: 1891
##### Re: IIF()
« Reply #1 on: June 11, 2009, 10:22:03 am »
b will equal x if its 100, other wise it will equal whatever is returned by z_func()

Yes, just code it!

if x=100 then b:=x else b:=z_func();

#### Leledumbo

• Hero Member
• Posts: 8242
• Programming + Glam Metal + Tae Kwon Do = Me
##### Re: IIF()
« Reply #2 on: June 11, 2009, 11:18:26 am »
Pascal doesn't have conditional expression, just code it using conditional statement as theo has posted.

#### Nicola Gorlandi

• Jr. Member
• Posts: 80
##### Re: IIF()
« Reply #3 on: June 11, 2009, 01:12:01 pm »

function IIF(Condition: Boolean; TrueResult, FalseResult: variant):Variant;
begin
if Condition then
Result := TrueResult
else
Result := FalseResult;
end;

#### duncanparsons

• Jr. Member
• Posts: 83
##### Re: IIF()
« Reply #4 on: June 11, 2009, 01:38:30 pm »

function IIF(Condition: Boolean; TrueResult, FalseResult: variant):Variant;
begin
if Condition then
Result := TrueResult
else
Result := FalseResult;
end;

That's a fair comment, but even overlooking the use of variants, it requires more calculations to be made. Whether or not the condition is true or false, both possible return values must be evaluated before they are passed to the function.

This means you couldn't do for instance
Code: [Select]
`ID := iif(assigned(Obj), Obj.ID, 0;` if there was anychance that Obj could be null, since Obj.ID would HAVE to be evaluated before the function call. Using std conditional execution avaoids that, and cuts down on the extra evaluation overhead.

The c conditional semantic is a shorthand and a compiler would produce the same code as if it was 'if (x) {y} else {z}'

HTH
DSP

#### Leledumbo

• Hero Member
• Posts: 8242
• Programming + Glam Metal + Tae Kwon Do = Me
##### Re: IIF()
« Reply #5 on: June 12, 2009, 10:22:45 am »
There are IfThen functions in Math and StrUtils unit, but it will be slower as AFAIK they're not inlined.