Java If to Blitzmax

BlitzMax Forums/BlitzMax Programming/Java If to Blitzmax

FBEpyon(Posted 2013) [#1]
java code:
x2 = x1 + SCREEN_WIDTH + (x1 == 0 ? 0 : TILE_SIZE);

Hello,

What is the best way to convert this to Blitzmax..? This code has an embedded if/then..

Thanks,

FBEpyon


ziggy(Posted 2013) [#2]
There are not this inmediate if operators in BM
If X1=0 then
   x2 = x1 + SCREEN_WIDTH 
Else
   x2 = x1 + SCREEN_WIDTH + TILE_SIZE
Endif


Maybe eaiser:
x2 = x1 + SCREEN_WIDTH 
if x1<>0 then x2 += TILE_SIZE



FBEpyon(Posted 2013) [#3]
Thank you..

I know it was easy, but I'm not having a very good week lol

Thanks Again!!


Kryzon(Posted 2013) [#4]
If you don't care about readability, you can multiply each result of that ternary operation by the equivalent boolean operation, which in BlitzMax results in 0 or 1.

x2 = x1 + SCREEN_WIDTH + (x1 == 0 ? 0 : TILE_SIZE);

Becomes...

x2 = x1 + SCREEN_WIDTH + ( 0 * ( x1 = 0 ) ) + ( TILE_SIZE * ( x1 <> 0 ) )

(You can remove the first boolean operation since it will always evaluate to 'zero'. I just added it for completeness.)


Matt Merkulov(Posted 2013) [#5]
x2 = x1 + SCREEN_WIDTH + IfThenElse(x1 = 0, 0, TILE_SIZE)

Function IfThenElse( Condition, IfTrue, IfFalse )
If Condition Then Return IfTrue Else Return IfFalse
End Function