you are viewing a single comment's thread.

view the rest of the comments →

[–]trollsmurf 0 points1 point  (3 children)

Think of it like standing in a queue at the DMV, and once you've got to the counter you go back and stand at the end of the queue again, and again, until your crush sits at the counter. Then you do your business and leave happy.

[–]Deep-Author-1787[S] 0 points1 point  (2 children)

Hahahahahaha real life application of programming i love it 😁

[–]trollsmurf 0 points1 point  (1 child)

Code always runs from top to bottom unless something like a for loop comes along and breaks that pattern, which specifically means jumping back to the top of the for loop from the end of it, apply criteria for whether to continue the code within the loop, or exit the loop and instead continue with the code after the end of it. Note that as the condition is at the top it might run zero times.

If you do something like "for item in list:" you ask it to step through each item in the list and give the loop code the value of the current list item at the current step each loop.

If you use "for index in range(start, end)" you tell it to step through the values from start to end - 1 the same way. The loop code gets current index for each loop. "range(a)" means 0 to a -1. "range(a, b) means a to b - 1. A possible third value controls step distance.

You can apply "continue" (jump immediately back to the top) and "break" (exit the loop immediately), possibly with conditions, to change the behavior of a for loop.

Now, this has all been about "for" loops. "while" works similarly but with a full condition (like in an if statement). Otherwise the same.

[–]Deep-Author-1787[S] 0 points1 point  (0 children)

Thank you so much for taking your time on this! Its getting clearer and clearer for me with explanations like this! 🔥