Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using the tenary operator in C with 3 numbers
#11
Well, until we get a short-circuiting ternary operator in QB64-PE. Big Grin  

Here is my attempt, just for fun.

My version brazenly exploits how QB64 uses the C++ compiler. This correctly eliminates the part that doesn't require evaluation.

Cheers!


Save as iif.h
Code: (Select All)
#pragma once

#define qb64_iif(qb64_iif_expression, qb64_iif_true_part, qb64_iif_false_part) ((qb64_iif_expression) ? (qb64_iif_true_part) : (qb64_iif_false_part))

Code: (Select All)
OPTION _EXPLICIT

DECLARE LIBRARY "iif"
    FUNCTION IIf_Byte%% ALIAS "qb64_iif" (BYVAL expression AS _OFFSET, BYVAL truePart AS _BYTE, BYVAL falsePart AS _BYTE)
    FUNCTION IIf_UByte~%% ALIAS "qb64_iif" (BYVAL expression AS _OFFSET, BYVAL truePart AS _UNSIGNED _BYTE, BYVAL falsePart AS _UNSIGNED _BYTE)
    FUNCTION IIf_Integer% ALIAS "qb64_iif" (BYVAL expression AS _OFFSET, BYVAL truePart AS INTEGER, BYVAL falsePart AS INTEGER)
    FUNCTION IIf_Uinteger~% ALIAS "qb64_iif" (BYVAL expression AS _OFFSET, BYVAL truePart AS _UNSIGNED INTEGER, BYVAL falsePart AS _UNSIGNED INTEGER)
    FUNCTION IIf_Long& ALIAS "qb64_iif" (BYVAL expression AS _OFFSET, BYVAL truePart AS LONG, BYVAL falsePart AS LONG)
    FUNCTION IIf_ULong~& ALIAS "qb64_iif" (BYVAL expression AS _OFFSET, BYVAL truePart AS _UNSIGNED LONG, BYVAL falsePart AS _UNSIGNED LONG)
    FUNCTION IIf_Integer64&& ALIAS "qb64_iif" (BYVAL expression AS _OFFSET, BYVAL truePart AS _INTEGER64, BYVAL falsePart AS _INTEGER64)
    FUNCTION IIf_UInteger64~&& ALIAS "qb64_iif" (BYVAL expression AS _OFFSET, BYVAL truePart AS _UNSIGNED _INTEGER64, BYVAL falsePart AS _UNSIGNED _INTEGER64)
    FUNCTION IIf_Single! ALIAS "qb64_iif" (BYVAL expression AS _OFFSET, BYVAL truePart AS SINGLE, BYVAL falsePart AS SINGLE)
    FUNCTION IIf_Double# ALIAS "qb64_iif" (BYVAL expression AS _OFFSET, BYVAL truePart AS DOUBLE, BYVAL falsePart AS DOUBLE)
END DECLARE

DIM i AS LONG

PRINT IIf_Long(i > 10, foo, bar) ' foo should not evaluate

i = 100

PRINT IIf_Long(i > 10, foo, bar) ' bar should not evaluate

END

FUNCTION foo&
    PRINT "Foo called!"
    foo = 512
END FUNCTION

FUNCTION bar&
    PRINT "Bar called!"
    bar = 128
END FUNCTION


[Image: Screenshot-2024-09-12-184743.png]
Reply
#12
(08-08-2024, 08:53 PM)Pete Wrote: I look at it as shorthand...

C=(A>B)?A:B; 

meaning:

If A > B Then C = A Else C = B

Pete

Yeah @a740g if that is the Ternary then it's the more familiar (to me at least)
IFF as in IFF(expression, ReturnThisTrueValue, ElseReturnThisValue)
ServeBeverage$= IFF$(age>=18, "Beer," "Juice")

and maybe not so popular in QB64 land because you have to Type the function returns.
b = b + ...
Reply
#13
(09-12-2024, 02:17 PM)bplus Wrote: and maybe not so popular in QB64 land because you have to Type the function returns.
it would be cool to have _WORD$ in QB64, maybe one that works on numbers too
Reply
#14
(09-12-2024, 02:37 PM)vince Wrote:
(09-12-2024, 02:17 PM)bplus Wrote: and maybe not so popular in QB64 land because you have to Type the function returns.
it would be cool to have _WORD$ in QB64, maybe one that works on numbers too

You have that from me a long while ago, possibly posted on old forum in toolbox.

I called it Item$ and had some other tools to go along with it, all string$ type.
I posted it at another forum this year I think.

Luke put up fast versions for numbers at the old forum as well.

Update: it's here too!
https://qb64phoenix.com/forum/showthread...9#pid22809
b = b + ...
Reply
#15
@bplus
Quote: Luke put up fast versions for numbers at the old forum as well.
do you have the link for Luke's code ?
Reply
#16
Reply #94 I think this is a copy of Luke's work and my own follows in next post?
https://qb64forum.alephc.xyz/index.php?t...#msg129084

The main use of this stuff is using Strings for Arrays in UDTs.
b = b + ...
Reply
#17
thanks bplus Big Grin
Reply




Users browsing this thread: 6 Guest(s)