Hi Charles,
I am trying to use the functionality of a ternary operator in Oxygenbasic. This is the result I accomplished so far:
#include "$/inc/console.inc"
function iif(bool testVal, sys ifTrue, ifFalse) as sys
if testVal then return ifTrue
return ifFalse
end function
function iif(bool testVal, float ifTrue, ifFalse) as float
if testVal then return ifTrue
return ifFalse
end function
function iif(bool testVal, double ifTrue, ifFalse) as double
if testVal then return ifTrue
return ifFalse
end function
function iif(bool testVal, string ifTrue, ifFalse) as string
if testVal then return ifTrue
return ifFalse
end function
'Test
print "" iif((14==13), (14 * 15), "False") & cr '( == )
'-----------------------
int A=5, B=6, C
c = iif(A > B, 100, 200)
print c & cr
'-----------------------
int a=1, b=2
string c
printl "a = 1 b = 2"
c = iif((a==b), "TRUE", "FALSE") '( == )
printl "iif((a==b), TRUE, FALSE) returned " c
c = iif(a <> b, "TRUE", "FALSE")
printl "iif(a <> b, TRUE, FALSE) returned " c
c = iif(a < b, "TRUE", "FALSE")
printl "iif(a < b, TRUE, FALSE) returned " c
c = iif(a > b, "TRUE", "FALSE")
printl "iif(a > b, TRUE, FALSE) returned " c
'-----------------------
string buf1, buf2, str1
buf1 = "ABCDEFGHI"
buf2 = "ABCDEFGHI"
printl cr "buf1 = ABCDEFGHI buf2 = ABCDEFGHI"
str1 = iif((buf1==buf2), "TRUE", "FALSE") '( == )
if str1 = "TRUE" then
printl "buf1 is equal buf2"
elseif str1 = "FALSE" then
printl "buf1 is not equal buf2"
end if
'-----------------------
double hour = 12.30
string greeting = "Good" + iif((hour > 17), " evening.", " day.") '( > )
printl cr greeting
'-----------------------
printl "Enter ... " : waitkey
When using the iif function it seems to be necessary to use braces for the condition if literal numbers are used and it is necessary to use braces and == for equality check.
Although my examples work, perhaps it is possible to use a more general method? In OxygenBasic\tests\Constructs I found iff.o2bas but it's iff function did not work for my tests.
Roland
.