This program demonstrates how if statements can be used to control a rectangle that moves across the screen.
CreateImageColor ( 1, 255, 255, 255, 255 ) CreateSprite ( 1, 1 ) SetSpriteSize ( 1, 5, 5 )
direction as integer = 0 x as float = 0.0 y as float = 50.0 speed as float = 10.0 angle as float = 0.0
do SetSpritePosition ( 1, x, y )
speed = ( x + 1 ) / 30 y = y + sin ( angle ) / 2.0 angle = angle + speed + 4
if direction = 0 print ( "moving right" )
x = x + speed
if x > 100 then direction = 1 elseif direction = 1 print ( "moving left" )
x = x - speed
if x < 0 then direction = 0 endif
sync ( ) loop
Understanding everything that is going on here isn't totally necessary right now, this example is being used as a more interesting way of looking at if statements instead of just viewing more text on screen. Essentially what is happening is that a small rectangle is created and its movement is controlled by the variable direction. An if statement is used to check the value of direction. When the value is 0 the rectangle is moved across to the right. Inside this is another if statement, that checks whether the rectangle's position has moved to the far right and if so changes the direction to 1. When direction is 1 the rectangle will move to the left. Again this if statement has another if statement within it, checking if the position of the rectangle is over to the far left, in which case it resets the direction to 0. The rectangle will move across to the right, then back to the left, back to the right, then to the left again, continually repeating this process as determined by the if statements being used.