The IIF function

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
bds107
User
User
Posts: 10
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: 1132
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: 2183
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: 1132
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: 2183
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"))
ebs
Enthusiast
Enthusiast
Posts: 561
Joined: Fri Apr 25, 2003 11:08 pm

Re: The IIF function

Post 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"))
User avatar
Piero
Addict
Addict
Posts: 914
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: The IIF function

Post 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,"|") +")"
User avatar
Piero
Addict
Addict
Posts: 914
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: The IIF function

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