QB64 Phoenix Edition
Using the tenary operator in C with 3 numbers - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3)
+---- Forum: Utilities (https://qb64phoenix.com/forum/forumdisplay.php?fid=8)
+---- Thread: Using the tenary operator in C with 3 numbers (/showthread.php?tid=2916)

Pages: 1 2


RE: Using the tenary operator in C with 3 numbers - a740g - 09-12-2024

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]


RE: Using the tenary operator in C with 3 numbers - bplus - 09-12-2024

(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.


RE: Using the tenary operator in C with 3 numbers - vince - 09-12-2024

(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


RE: Using the tenary operator in C with 3 numbers - bplus - 09-12-2024

(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.php?tid=2414&pid=22809#pid22809


RE: Using the tenary operator in C with 3 numbers - Jack - 09-12-2024

@bplus
Quote: Luke put up fast versions for numbers at the old forum as well.
do you have the link for Luke's code ?


RE: Using the tenary operator in C with 3 numbers - bplus - 09-12-2024

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?topic=1511.msg129084#msg129084

The main use of this stuff is using Strings for Arrays in UDTs.


RE: Using the tenary operator in C with 3 numbers - Jack - 09-13-2024

thanks bplus Big Grin