Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Question about Loops. — Gideros Forum

Question about Loops.

tytadastytadas Member
edited November 2016 in General questions
I do realize this question might not be the best here, but I need to understand loops better. So I'll try to risk and ask the question.

I know loops and I do pretty much understand what they are made for and what do they do. But I'm a bit Lost.

While loop syntax is:
while(condition)
do
statement
end
For loop conditions is pretty much the same, but it has init, max/min value, increment, everything else is the same.
But repeat..until loop is a bit different.
repeat
statement
while


My point here is, that I can't get out by myself the information.
Why does 2 loops has the "do" line and repeat..until loop does not?
What's the function or should I say purpose of the "do" line in the loop? What does it stand for? What does "do" line represents?

Thank you.

Comments

  • antixantix Member
    edited November 2016
    Here's an example...
    local counter = 0
     
    while counter ~= 100 do -- Loop 100 times
      counter = counter + 1
    end
     
    print(counter) -- will print "100"
     
    counter = 0
    repeat
      counter = counter + 1
    until counter == 200 -- repeat the loop until this condition is met
     
    print(counter)
    Does that make it easier to grasp?

    Likes: tytadas

    +1 -1 (+1 / -0 )Share on Facebook
  • @antix Yes, I do understand it. But my point of the question was about "do" line. Maybe it's not that important to know everything about the syntax of all the loops.
    Thank you for answering
  • n1cken1cke Maintainer
    Accepted Answer
    > Why does 2 loops has the "do" line and repeat..until loop does not?
    'do' is just a keyword to separate condition from statements in loops. Without this separator it will be harder to read them, especially when condition and statements are on the same line.
    'while - do - end' loop checks it's condition first and if it is equal to false or nil then statements will be skipped even at first loop iteration. But there are few situations when you need to execute loop statements at least once, no matter of condition. Of course you can copy-paste statements to a place above your loop but such code will be harder to maintain: if you will change statements in your loop then you will need to copy-paste them again. That's why 'repeat - until' loop is useful here: it first executes it's statements and only then checks a condition. And 'repeat-until' doesn't use 'do' keyword because it's easier to differentiate it from 'while-do' loop.
    +1 -1 (+4 / -0 )Share on Facebook
  • tytadastytadas Member
    edited November 2016
    Hoora! I was expenting to get the answer from you! Thanks:))

    Likes: n1cke

    +1 -1 (+1 / -0 )Share on Facebook
  • 'do' will end the condition in the tokeniser. It's there so that you can write:
    n=100
    while n=0 do n=n+1 end
    just like you could write:
    n=100
    repeat n=n-1 until n=0
    (don't forget though that repeat will execute the loop at least once even if the condition isn't met, but the while may not if the condition isn't met straight away)

    Likes: tytadas

    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
    +1 -1 (+1 / -0 )Share on Facebook
  • I also understand and use both while and repeat, and I have also felt it is an inconsistent syntax. I really want an end in there in the repeat loop, but if course that would lead to more confusion.

    Likes: tytadas

    My Gideros games: www.totebo.com
    +1 -1 (+1 / -0 )Share on Facebook
  • Yeaa I do believe that you can't survive without using loops while coding or can you?
Sign In or Register to comment.