ternary operator equivalent

Started by James C. Fuller, June 16, 2024, 10:45:51 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

James C. Fuller

Charles,
  Does O2 have a ternary operator equivalent?
int a = 10, b = 20, c;

c = (a < b) ? a : b;

printf("%d", c);

James



Charles Pegge

Hi James,

It can be done with macro functions (the C ternary syntax proved to be too messy)

macro iif int(r,  e,a,b)
========================
if e
  r=a
else
  r=b
endif
end macro
'
int a = 10, b = 20

print iif(a < b , a , b)

Frank Brübach

Good morning,

Charles you example doesn't Work Here Got an Error message in
Print iif(a<b,a,b) ' Not found [a

Here's another example

' o2-tenary-frank c to oxygen
uses console

''
'' int a=10, b=20,c;
'' if(a<b){c=a}
'' else{c=b}
'' printf("%d",c)


Dim a As Integer = 10
Dim b As Integer = 20
Dim c As Integer

If a < b Then
    c = a
Else
    c = b
End If

Print c 'result 10

wait

Charles Pegge

It works here. I also tested it with a console:

uses console

macro iif int(r,e,a,b)
========================
if e
  r=a
else
  r=b
endif
end macro
'
int a = 10, b = 20

print iif(a < b , a , b) cr
wait

Frank Brübach

Hi Charles your Code is working Well..  Made an Update of oxygen Basic dont know what was the Problem Here