Re: ternary if or iif
Posted: Sat Nov 14, 2009 10:12 pm
And anyone can be a hippyluis wrote:...make simple (or complicated) programs '60 style...
Anyone can be happy.

http://www.purebasic.com
https://www.purebasic.fr/english/
And anyone can be a hippyluis wrote:...make simple (or complicated) programs '60 style...
Anyone can be happy.
I asked for it !Foz wrote:And anyone can be a hippyluis wrote:...make simple (or complicated) programs '60 style...
Anyone can be happy.
Nahh they will never be happy because they want to stop everyone else getting these nice newer features because "it's not BASIC" or because it's C/C++/C# syntaxluis wrote:Anyone can be happy.
Well, maybe, maybe not (who cares?).Kaeru Gaman wrote:this is C syntax.
if you like it that much, you can program in C (++/#)
there are tons of compilers out there for C
Code: Select all
Macro iif(clause,condTrue,condFalse)
If Bool(clause) : condTrue : Else : condfalse : EndIf
EndMacro
Procedure foo(x,y)
Debug Str(x) + Str(y)
EndProcedure
x=4:y=2
iif(x > 5 And y > 0, Debug "true", foo(x,y))
Code: Select all
Procedure$ IIFs(expression, IsTrue$, IsFalse$)
If expression
ProcedureReturn IsTrue$
Else
ProcedureReturn IsFalse$
EndIf
EndProcedure
Procedure.i IIF(expression.i, IsTrue.i, IsFalse.i)
If expression
ProcedureReturn IsTrue
Else
ProcedureReturn IsFalse
EndIf
EndProcedure
Procedure.i DoIt(a.i=1, b.i=2, x$=#NULL$)
Debug a
Debug b
Debug x$
EndProcedure
DoIt(1, 2, IIFs(Bool("1" > "2"), "TRUE", "FALSE"))
DoIt(1, IIF(Bool("1" > "2"), 1, -999), "Using b")
Code: Select all
reset->tcp.AckNum =
(tcp_header->Syn?
htonl(ntohl(tcp_header->SeqNum) + 1):
htonl(ntohl(tcp_header->SeqNum) + payload_len));
Code: Select all
Procedure _IIF_Integer(Expression, IsTrue, IsFalse)
If Expression
ProcedureReturn IsTrue
EndIf
ProcedureReturn IsFalse
EndProcedure
Macro IIFi(Expression, IsTrue, IsFalse)
_IIF_Integer(Bool(Expression),IsTrue,IsFalse)
EndMacro
Result=IIFi(3>2,1,0)
Debug Result
How many million/billion of this ternary if statements do you execute per second?Thunder93 wrote:The fact that there's no native support, we suffer ~2x lowered speeds. Working with certain types of projects especially networking, this is a big deal. That being the case, doing bulky and time-consuming If : elseIf : else expressions seems like a requirement.
Code: Select all
reset->tcp.AckNum = (tcp_header->Syn? htonl(ntohl(tcp_header->SeqNum) + 1): htonl(ntohl(tcp_header->SeqNum) + payload_len));
Code: Select all
If Bool(*tcp_header\Syn) : *reset\tcp\AckNum = htonl(ntohl(*tcp_header\SeqNum) + 1)
Else : *reset\tcp\AckNum = htonl(ntohl(*tcp_header\SeqNum) + payload_len)
EndIf
Code: Select all
Macro IIF(_target_, _expression_, _true_, _false_)
If Bool(_expression_) : _target_ = _true_ : Else : _target_ = _false_ : EndIf
EndMacro
Macro IIFcmd(_cmd_, _expression_, _true_, _false_)
If Bool(_expression_) : _cmd_ _true_ : Else : _cmd_ _false_ : EndIf
EndMacro
Macro NONE : : EndMacro
IIF(Result, 3 > 2, 1, 0) : Debug Result
IIF(a$, x = 0, "True", "False") : Debug a$
IIF( d.d, ; result/target
1 = 2, ; test expression
1.5, ; true
0.5 ) ; false
Debug d
IIFcmd( Debug, 1 = 1, "true", "false" )
IIFcmd( var$=, 1 > 1, "true", "false" ) : Debug var$
Procedure Func(text.s)
Debug text
EndProcedure
IIFcmd( NONE,
5 < 6,
Func("5 < 6"),
Func("5 >= 6") )
CompilerIf Defined(Thunder93, #PB_Constant)
IIFcmd( *reset\tcp\AckNum =,
*tcp_header\Syn,
htonl(ntohl(*tcp_header\SeqNum) + 1),
htonl(ntohl(*tcp_header\SeqNum) + payload_len) )
CompilerEndIf