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.
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
Likes: tytadas
Thank you for answering
'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.
Likes: tytadas, SinisterSoft, talis, pie
Likes: n1cke
Likes: tytadas
https://deluxepixel.com
Likes: tytadas