Page 1 of 1

The IIF function

Posted: Thu Jul 24, 2025 9:50 pm
by bds107
Hi everyone!
Back when animals still spoke, there was something called IIF. Unfortunately, it wasn't built into PureBASIC, or I'd have to cross my eyes.

Code: Select all

GLOBAL result.s, userInput.a
result.s = IIF(userInput MOD 2 = 0, "Even", "Odd")
DEBUG "The number is " + result
Hopefully...

Re: The IIF function

Posted: Thu Jul 24, 2025 10:36 pm
by Quin
This has been requested many times. I've concluded that it'll never happen.
PB isn't a bad language for pounding out a native executable that works across operating systems. But if you want anything even close to modern syntax, it's not it. I personally recommend Crystal for that.

Re: The IIF function

Posted: Thu Jul 24, 2025 10:54 pm
by AZJIO
PureAutoIt

Code: Select all

Macro IIf(expr, truepart, falsepart=__expr_null__.b)
	If expr
		truepart
	Else
		falsepart
	EndIf
EndMacro
bds107 wrote: Thu Jul 24, 2025 9:50 pm

Code: Select all

result.s = IIF(userInput MOD 2 = 0, "Even", "Odd")

Code: Select all

result.s = n & 1

Re: The IIF function

Posted: Thu Jul 24, 2025 11:01 pm
by Quin
AZJIO wrote: Thu Jul 24, 2025 10:54 pm PureAutoIt

Code: Select all

Macro IIf(expr, truepart, falsepart=__expr_null__.b)
	If expr
		truepart
	Else
		falsepart
	EndIf
EndMacro
Only works in super basic cases. I can't do this for example:

Code: Select all

MessageRequester("Current score is", Str(Points) + " " + Iif(Points = 1, "point", "points"))
That's why I'd like it to be a language feature, not a new function. But we know how Fred feels about those.

Re: The IIF function

Posted: Thu Jul 24, 2025 11:07 pm
by AZJIO

Code: Select all

Procedure.s IIF(expr, truepart.s, falsepart.s)
	If expr
		ProcedureReturn truepart
	Else
		ProcedureReturn falsepart
	EndIf
EndProcedure

Global result.s, userInput.a = 1
result.s = IIF(userInput & 1, "Odd", "Even")
Debug "The number is " + result

Global Points = 1
MessageRequester("Current score is", Str(Points) + " " + Iif(Bool(Points = 1), "point", "points"))

Re: The IIF function

Posted: Thu Aug 28, 2025 2:36 pm
by ebs
Only because I'm extremely lazy (apologies to AZJIO):

Code: Select all

Macro IIF(_expr, _truepart, _falsepart)
  IIFX(Bool(_expr), _truepart, _falsepart)
EndMacro

Procedure.s IIFX(expr, truepart.s, falsepart.s)
	If expr
		ProcedureReturn truepart
  Else
		ProcedureReturn falsepart
  EndIf
EndProcedure

Global result.s, userInput.a = 1
result.s = IIF(userInput & 1, "Odd", "Even")
Debug "The number is " + result

Global Points = 1
MessageRequester("Current score is", Str(Points) + " " + IIF(Points = 1, "point", "points"))

Re: The IIF function

Posted: Thu Aug 28, 2025 5:09 pm
by Piero
ebs wrote: Thu Aug 28, 2025 2:36 pmMacro IIF(_expr,
That's great, I didn't know/remember!

PS: IIFs$ :lol:

Code: Select all

Points = 10
Debug "Score: " + Str(Points) + " Point" +
   Mid("s",Bool(Points=1)+1) +" ("+ StringField("Even|Odd",Points&1+1,"|") +")"

Re: The IIF function

Posted: Thu Aug 28, 2025 9:34 pm
by Piero
Summarizing:

Code: Select all

Macro IIF(e,s)
   StringField(s,Bool(Not(e))+1,"|")
EndMacro

x=10
Debug ""+x+" Point"+ IIF(x=1, "|s")
Debug ""+x+" is "+ IIF(x&1, "Odd|Even")
Debug IIF(1=0 or 0=0, "True|Not True")