News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

I have a strange problem with an "OR" operator

Started by cybermind, December 10, 2013, 08:09:16 AM

Previous topic - Next topic

cybermind

I have this piece of code:

PlayBASIC Code: [Select]
`check if player is chosing a destination
if action_activated = 1 and player_world_map_y <> world_map_marker_y and action_single_press = 0 or action_activated = 1 and player_world_map_x <> world_map_marker_x and action_single_press = 0
action_single_press = 1
world_map_destination_x = world_map_marker_x
world_map_destination_y = world_map_marker_y
player_world_map_move_timer = timer()
endif



Only the right operand is able become true. If I remove the OR operator and the right operand after the OR operator, then the left operand is able to become true. If I switch the two operands it is still only the right one that is able to become true. Does anybody know what I am doing wrong?

kevin

 I'm assuming you want either side of the OR operator to evaluate separately.  To do so, you'd need to bracket the parts of  expression accordingly.    This is because expressions parse left to right and  AND / OR operators have the same precedence.   So the order in which they appear is they order they'll be evaluated out of the expression.

So an expression like   A and B or C  - The AND would be performed first, then the OR between the result of the AND and the C variable.    

Where as  A or B and C - The OR is performed first and then the AND.


 

PlayBASIC Code: [Select]
`check if player is chosing a destination
if ( action_activated = 1 and player_world_map_y <> world_map_marker_y and action_single_press = 0) or (action_activated = 1 and player_world_map_x <> world_map_marker_x and action_single_press = 0)
action_single_press = 1
world_map_destination_x = world_map_marker_x
world_map_destination_y = world_map_marker_y
player_world_map_move_timer = timer()
endif







cybermind

Okay :-) Thank you very much! I am used to DarkBASIC, it seems to be a bit different from PLAYBASIC, in DarkBASIC the brackets are not needed as far as I Know. The brackets is a good thing if you ask me, they give me a better control of how the expression is checked :-)

kevin


While AND / OR are often referred to a logical Boolean operators in the PB world, they're actually bitwise operators in PB.   But it's not uncommon behavior in languages that support both for the Boolean And operator to have precedence over OR.