Tip: IIf function

Share your advanced PureBasic knowledge/code with the community.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Tip: IIf function

Post by PB »

Code: Select all

Macro IIf (expr,truepart,falsepart)
  If expr : truepart : Else : falsepart : EndIf
EndMacro

a=Random(1) : Debug a
b=Random(1) : Debug b
IIf(a=b, Debug "same", Debug "different")
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
hellhound66
Enthusiast
Enthusiast
Posts: 119
Joined: Tue Feb 21, 2006 12:37 pm

Post by hellhound66 »

Removed.
Last edited by hellhound66 on Wed Mar 19, 2008 11:44 pm, edited 1 time in total.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

It's a shorter way to do a test and return either a true or false result.
Truepart and falsepart are not meant to be a set of commands (like in
your example of For/Next), but just a single result. Some docs for it:

http://en.wikipedia.org/wiki/IIf

and

http://msdn.microsoft.com/library/defau ... fctIIF.asp
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Nice clean implementation, PB. 8)
BERESHEIT
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Post by dell_jockey »

Nice indeed. A suggestion: you could extend the Wikipedia page with your PB implementation.... Would give you and PB some airtime... :)
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

Using in this way?

Code: Select all

Macro IIf (expr,truepart,falsepart)
  If expr : truepart : Else : falsepart : EndIf
EndMacro

a=Random(1) : Debug a
b=Random(1) : Debug b
IIf(a=b, x=1, x=0)
Debug x
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

Very Nice! Thank You :)
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

Nice.

It was already submitted by helpy one year ago.
he was very close to make it work but nobody replies.

http://www.purebasic.fr/english/viewtopic.php?t=19291
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> Nice but already submitted by helpy one year ago

I didn't see those, but yeah, they don't work. He was very close, as you said.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

The most useful form of the IIf command imho would be:

Code: Select all

  Result$ = IIf ( (height > 190), "Tall", "Average" )
but I don't believe it's doable in straight PureBasic code (no boolean expressions). Can anyone make it work, perhaps with an asm approach? Or is it easier than it looks to me?
BERESHEIT
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Post by Little John »

How about this:

Code: Select all

Macro IIf (condition, target, trueVal, falseVal)
   If condition
      target = trueVal
   Else
      target = falseVal
   EndIf
EndMacro

;-- Demo
IIf(3=4, msg$, "same", "not equal")
Debug msg$
Regards, Little John
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Post by #NULL »

like this i do it for integers:

Code: Select all

; if2.pbi
; by #Null

Procedure if2proc(condition, A.l, B.l)
  If condition  
    ProcedureReturn A
  Else
    ProcedureReturn B
  EndIf
EndProcedure

Macro if2(if2_condition, if2_A, if2_B)
  if2proc( (0 Or (if2_condition)), if2_A, if2_B )
EndMacro



;Debug if2( 2>5, 123, 321)

like that for strings

Code: Select all

; strIf.pbi
; by #Null

; an ?-operator for stings. works like..
; expression ? string1 : string2

Macro StrIf( _StrIf_condition_, _StrIf_string_IF_, _StrIf_string_ELSE_="")
  _StrIf_proc( 0 Or (_StrIf_condition_), _StrIf_string_IF_, _StrIf_string_ELSE_ )
EndMacro


Procedure.s _StrIf_proc( condition.l, stringIF.s, stringELSE.s)
  If condition
    ProcedureReturn stringIF
  Else
    ProcedureReturn stringELSE
  EndIf
EndProcedure




; Debug "---- EXAMPLE - StrIf.pbi"
; For i=0 To 10
;   r=Random(20)-10
;   Debug StrIf(r>0,"+") + Str(r)
; Next
; Debug "----"
; For i=0 To 10
;   r=Random(3)
;   Debug StrIf(Not r,"no", Str(r))  +  StrIf(r>1," seats", " seat")
; Next
; Debug "----"

..i didn't find an ultimate solution yet
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post by pdwyer »

I don't understand the point of a one line macro in this case,

Can't you just type what's in the macro instead of the macro? Whats the benefit?

Perhaps because I can't see what everyone else sees is why I never use macros... :?
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
Edwin Knoppert
Addict
Addict
Posts: 1073
Joined: Fri Apr 25, 2003 11:13 pm
Location: Netherlands
Contact:

Post by Edwin Knoppert »

In c# it works as should be:

int a = 1;
String T = a == 1 ? "1234" : "nope";

This means you can use inline stuff instead of using if's like:

if( Function1( a == 1 ? "1234" : "nope" ) )
{


}

Where Function1's param is a string in this case.
User avatar
Dreamland Fantasy
Enthusiast
Enthusiast
Posts: 335
Joined: Fri Jun 11, 2004 9:35 pm
Location: Glasgow, UK
Contact:

Post by Dreamland Fantasy »

pdwyer wrote:I don't understand the point of a one line macro in this case,

Can't you just type what's in the macro instead of the macro? Whats the benefit?

Perhaps because I can't see what everyone else sees is why I never use macros... :?
Macros execute faster than procedures since the code is substituted where the calls to the macro are during compilation. This is one reason why people use them. Downside is that it can increase the size of the compiled code compared with using procedures.

The other reason is that it can save a lot of typing (and hence less chance of mistakes creeping into your code).

Here is an example of a macro from an image processing/converter program I wrote:

Code: Select all

Macro Normalize(Red, Green, Blue) 
  If Red < 0 
    Red = 0 
  ElseIf Red > 255 
    Red = 255 
  EndIf 
  If Green < 0 
    Green = 0 
  ElseIf Green > 255 
    Green = 255 
  EndIf 
  If Blue < 0 
    Blue = 0 
  ElseIf Blue > 255 
    Blue = 255 
  EndIf 
EndMacro
Rather than write this out the long way every time I had to access it in various procedures all I do type is NormalizeRGB(RedValue, GreenValue, BlueValue). Also, because I have it as a macro instead of a procedure the execution time is a lot faster (imagine calling this as a procedure for every single pixel in a large image! :shock:).

But, like you, I can't really see the point in the IIf() example unless it is to ease the conversion of some code from another programming environment.

Kind regards,

Francis.
Last edited by Dreamland Fantasy on Thu Sep 20, 2007 3:19 pm, edited 1 time in total.
Post Reply