Interactive PowerBasic Forum

IT-Consultant: Charles Pegge => OxygenBasic => Topic started by: James C. Fuller on June 16, 2024, 10:45:51 PM

Title: ternary operator equivalent
Post by: James C. Fuller on June 16, 2024, 10:45:51 PM
Charles,
  Does O2 have a ternary operator equivalent?
int a = 10, b = 20, c;

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

printf("%d", c);

James


Title: Re: ternary operator equivalent
Post by: Charles Pegge on June 17, 2024, 02:59:20 AM
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)
Title: Re: ternary operator equivalent
Post by: Frank BrĂ¼bach on June 17, 2024, 07:25:43 AM
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
Title: Re: ternary operator equivalent
Post by: Charles Pegge on June 17, 2024, 08:43:06 AM
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
Title: Re: ternary operator equivalent
Post by: Frank BrĂ¼bach on June 17, 2024, 12:13:03 PM
Hi Charles your Code is working Well..  Made an Update of oxygen Basic dont know what was the Problem Here