And anyone can be a hippyluis wrote:...make simple (or complicated) programs '60 style...
Anyone can be happy.
ternary if or iif
Re: ternary if or iif
Re: ternary if or iif
I asked for it !Foz wrote:And anyone can be a hippyluis wrote:...make simple (or complicated) programs '60 style...
Anyone can be happy.
"Have you tried turning it off and on again ?"
Re: ternary if or iif
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.

Re: ternary if or iif
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
I'm not going to choose a language based solely on wether it uses ";" or ":" to separate commands or supports the use of "&&" or "||"
I'm not a pureist (no pun intended) who cares where these ideas come from?
The great thing about BASIC (any version) is that it isnt a *proud* language, it's standards were ignored/forgotten long ago - the point is, does "&&" and "||" add anything of value to the language or not?
If this syntax is useful, use it. If not, forget it.
Anyway, these are not my decisions to make (Fred / Freak?) - all I'm saying is I would like this syntax in PB, cos I think its useful...
Ta - N
Re: ternary if or iif
I know I'm posting in old topic, but I'm posting on this and avoid duplicate topics in 'Feature Requests and Wishlists' forum.
I don't know how I've missed this discussions way back in 2009.
Before ever programming, I use to-do lot of scripting with different languages. It has been so long now but waayyy back in my teens .. I use to-do lot of scripting and the ternary conditional / inline IF (IIF) was a wonder to use. I personally found this to make things much easier and also keeps code looking optimized and pretty.
Idle, has offered code which is all nice and dandy. Like idle said though, without it being natively supported, you have ~2x speed penalty. Working with different types of projects, .. like networking projects. Speed is of top-most importance.
When I bought PureBasic, waayyy back in 2005. Then shortly later I was looking to use ternary conditional / inline IF (IIF). However, I couldn't spot such a feature and at the time I've summed it up to not being familiar with PureBasic commandset and simply need more time to become familiar and locate this command.
I'm more familiar with PureBasic now.. I don't believe that this support exists. LOL
Unfortunately all this time has passed, and very old requests been made, and still no inclusion. This support doesn't seem like it'll ever catch the interest of those that matters.
New days..., might bring surprises!
I don't know how I've missed this discussions way back in 2009.
Before ever programming, I use to-do lot of scripting with different languages. It has been so long now but waayyy back in my teens .. I use to-do lot of scripting and the ternary conditional / inline IF (IIF) was a wonder to use. I personally found this to make things much easier and also keeps code looking optimized and pretty.
Idle, has offered code which is all nice and dandy. Like idle said though, without it being natively supported, you have ~2x speed penalty. Working with different types of projects, .. like networking projects. Speed is of top-most importance.
When I bought PureBasic, waayyy back in 2005. Then shortly later I was looking to use ternary conditional / inline IF (IIF). However, I couldn't spot such a feature and at the time I've summed it up to not being familiar with PureBasic commandset and simply need more time to become familiar and locate this command.
I'm more familiar with PureBasic now.. I don't believe that this support exists. LOL
Unfortunately all this time has passed, and very old requests been made, and still no inclusion. This support doesn't seem like it'll ever catch the interest of those that matters.
New days..., might bring surprises!
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
Re: ternary if or iif
Funny reading the old thread and now I wonder what all the fuss was about and why the solution was so protracted.
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))
Windows 11, Manjaro, Raspberry Pi OS


Re: ternary if or iif
that isn't quite the same. IIF() you produced, can't exactly be in-line? Like to set a returned value into variable.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
Re: ternary if or iif
Yeah, IIF()'s have to be Procedures and type specific for inline...
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")The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Re: ternary if or iif
Nice stuff. Some type of PB option exists to the counterpart.
I'm still looking for more convenience and freedom. 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.
I'm still looking for more convenience and freedom. 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));ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
Re: ternary if or iif
Unless I'm missing something, you can already do this with the advent of Bool():
The only downside is that we still need a separate function for each type, as we don't have anything like templates or generics.
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
Last edited by Mistrel on Mon Nov 24, 2014 5:10 am, edited 2 times in total.
Re: ternary if or iif
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)
EndIfto highly optimizing C++ compilers you are probably little bit slower with PB anyway.
Both languages have to test the expression at runtime, if it isn't a constant expression that could be optimized away.
I would include the result in the macro to make things easier to write. Of course it's not "Inline If" anymore, nonetheless it makes things easier to write:
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) )
CompilerEndIfRe: ternary if or iif
Thank you.., you guys! For both information and example codes.
I do like the idea of using Macros.
Like you've said, this makes things easier to write.
I do like the idea of using Macros.
Like you've said, this makes things easier to write.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley


