The for loop doesn't work for me?

-- Shaders
-- Use this function to perform your initial setup
function setup()
xPosition = 100
yPosition = 80
function loop()
 or xPosition > 112 do
            xPosition = xPosition - 12
            end          
end
-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(39, 46, 50, 255)
    sprite("Planet Cute:Gem Green", xPosition, yPosition)
    if xPosition < 500 then
        xPosition = xPosition + 6
        end
    if xPosition > 500 then
        xPosition = xPosition - 12
        loop()
        end
    -- This sets the line thickness
    strokeWidth(5)
    -- Do your drawing here
    end

Hey guys! Typing this from an iPad, hoping this has tags for code. I am trying a for loop to make sure that xPosition gets minused from, even when it strays away from the if values until it hits 100 again. It refuses to compile, with weird errors. Help?

@Treshure: The forums appear to be using a form of MarkDown. You can delimit code block with three tilde (~) characters on a line by themselves. (You should still be able to edit your post to fix that, if you like.)

Regarding your actual question, I don’t think you want a for loop, but a while loop. Something like:

    while xPosition > 112 do
      xPosition = xPosition - 12
    end

I think you can also solve that without a loop:

    xPosition = xPosition - math.ceil((xPosition - 100)/12)*12

(untested)

@Treshure I added the three(~) at the front and end for you.

Thank you so much for the response! I ran the code, and all is good but the gem slingshots left instead of going progressively. Any help?

draw() is running at 60 fps. So any changes to xPosition happen 60 times in a second. This would slingshot your object off the screen.

Why doesn’t that effect the first motion going right, then?

@Treshure: Your loop function will not be interrupted for drawing. So once xPosition gets over 500, loop will make it become about 100 without intervening draw. Instead of loop, consider introducing a variable step that switches between +6 and -12 when you hit the ends. Then xPosition is always updated as xPosition = xPosition+step.