Page 1 of 2

Tip: IIf function

Posted: Fri Feb 09, 2007 2:00 pm
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")

Posted: Fri Feb 09, 2007 2:07 pm
by hellhound66
Removed.

Posted: Fri Feb 09, 2007 2:14 pm
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

Posted: Fri Feb 09, 2007 6:28 pm
by netmaestro
Nice clean implementation, PB. 8)

Posted: Fri Feb 09, 2007 6:48 pm
by dell_jockey
Nice indeed. A suggestion: you could extend the Wikipedia page with your PB implementation.... Would give you and PB some airtime... :)

Posted: Fri Feb 09, 2007 7:02 pm
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

Posted: Sat Feb 10, 2007 2:27 am
by SFSxOI
Very Nice! Thank You :)

Posted: Sat Feb 10, 2007 12:32 pm
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

Posted: Sat Feb 10, 2007 12:36 pm
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.

Posted: Sun Feb 11, 2007 4:42 am
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?

Posted: Tue Sep 18, 2007 5:03 pm
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

Posted: Tue Sep 18, 2007 10:43 pm
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

Posted: Wed Sep 19, 2007 2:18 pm
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... :?

Posted: Wed Sep 19, 2007 2:45 pm
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.

Posted: Thu Sep 20, 2007 1:55 pm
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.