The IIF function

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
bds107
New User
New User
Posts: 7
Joined: Fri Jul 28, 2023 10:35 am
Location: Bruges (Belgium)

The IIF function

Post 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...
Quin
Addict
Addict
Posts: 1122
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: The IIF function

Post 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.
AZJIO
Addict
Addict
Posts: 2141
Joined: Sun May 14, 2017 1:48 am

Re: The IIF function

Post 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
Quin
Addict
Addict
Posts: 1122
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: The IIF function

Post 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.
AZJIO
Addict
Addict
Posts: 2141
Joined: Sun May 14, 2017 1:48 am

Re: The IIF function

Post 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"))
Post Reply