Python relies heavily on indentation

# The code below prints 0-4, sees that the next for line is not indented, doesn't read it
for x in range(5):
    print(x)

# there are other methods of breaking a python loop
for x in range(6):
    print(x)
 if x == 5:
         # break the loop
         break
Answer from Marker on Stack Overflow
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › javascript fundamentals
Loops: while and for
The answer: 1. ... Every loop iteration decreases i by 1. The check while(i) stops the loop when i = 0.
🌐
LaunchCode
education.launchcode.org › intro-to-professional-web-dev › chapters › loops › for-statement-breakdown.html
9.4. Breaking Down the for Statement — Introduction to Professional Web Development in JavaScript documentation
It is critical that the loop condition eventually becomes false. A loop for which the condition is never false is known as an infinite loop, because it never stops iterating.
Discussions

How does python know when to stop the for loop? - Stack Overflow
1st time programming in python How does python know when to stop the for loop? Like the for does not have a matching end correct? so how does it know what is part of the loop and what is not? Apol... More on stackoverflow.com
🌐 stackoverflow.com
Why does my for loop stop iterating after one time?
Because you keep redeclaring your array inside the loop. More on reddit.com
🌐 r/AskProgramming
7
2
June 26, 2021
For Loop stops after first iteration
Hi New here 🙂. I am trying to write a function to find the Smallest Common Multiple between a range of 2 numbers in an array. The multiple has to be divisible by all numbers in the range. I approached the problem as follows: step 1: Create a new array and push all the numbers in range between ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
11
0
October 12, 2022
When to use a for loop and when to use a while loop?
Short answer: Use what make sense. I am not sure what language you are speaking, but if it's English then follow how you phrase what you want to do in your head. If in your head you say, "let's go from 0 to the end" use for( int i =0; i < arr.lenght; i++) {..} If you say in your head, "Let's do it while we can" use while( rd.MoveNext() ) {..} More on reddit.com
🌐 r/csharp
43
26
August 1, 2022
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Loops_and_iteration
Loops and iteration - JavaScript - MDN Web Docs - Mozilla
When you use continue without a label, it terminates the current iteration of the innermost enclosing while, do-while, or for statement and continues execution of the loop with the next iteration. In contrast to the break statement, continue does not terminate the execution of the loop entirely.
🌐
Python
wiki.python.org › moin › ForLoop
ForLoop - Python Wiki
Contrast the for statement with ... each iteration or to repeat a block of code forever. For example: For loop from 0 to 2, therefore running 3 times. for x in range(0, 3): print("We're on time %d" % (x)) While loop from 1 to infinity, therefore running forever. x = 1 while True: print("To infinity and beyond! We're getting close, on %d now!" % (x)) x += 1 · When running the above example, you can stop the program ...
🌐
LearnPython.com
learnpython.com › blog › end-loop-python
How to End Loops in Python | LearnPython.com
December 16, 2021 - If you want to iterate over some ... element of the iterator. The loop, or the iteration, is finished once we return the last element from the iterator. Calling next() after that raises the StopIteration exceptio...
🌐
CodeSignal
codesignal.com › learn › courses › exploring-iterations-and-loops-in-python › lessons › mastering-loop-controls-break-and-continue-in-python
Mastering Loop Controls: Else, Break and Continue in Python
We leveraged the not in operation to check for any items that hadn't been packed. Specifically, not in is used here to determine if the current item from our packing_list is absent in the packed_items list. As soon as we encountered an item missing from packed_items, the break statement stopped ...
🌐
IBM
ibm.com › home › page title › page title › for loop
What is a for loop in python? | IBM
November 21, 2025 - Use a for loop when you know in ... is evaluated dynamically at run time. StopIteration is an exception in Python that is raised to signal that an iterator has no more items to provide....
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › loops-in-python
Loops in Python - GeeksforGeeks
Loops are used to execute a block of code repeatedly until a condition is met or all items in a sequence are processed. The main types are For loops (iterating over sequences) and While loops (executing code based on a condition).
Published: 3 weeks ago
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-check-end-of-for-loop-in-python
How to Check End of For Loop in Python - GeeksforGeeks
July 23, 2025 - The simplest way to check when a for loop has ended is by using the else clause. The else block after a for loop runs only when the loop has finished all iterations without being interrupted by a break statement.
🌐
freeCodeCamp
forum.freecodecamp.org › curriculum help
For Loop stops after first iteration - Curriculum Help - The freeCodeCamp Forum
October 12, 2022 - Hi New here 🙂. I am trying to write a function to find the Smallest Common Multiple between a range of 2 numbers in an array. The multiple has to be divisible by all numbers in the range. I approached the problem as follows: step 1: Create a new array and push all the numbers in range between the 2 numbers in the original array to this new array. step 2: Define an upper bound for the multiples. step 3: Loop through the multiples of the largest number, capping at the u...
🌐
Windows 10 Forums
tenforums.com › general-support › 209456-loop-stop-itself-desired-iteration.html
FOR loop : stop itself at desired iteration Solved - Windows 10 Forums
In most programming languages, a for loop typically has a fixed number of iterations based on a specified range or collection of elements. However, if you want to stop a for loop at a specific iteration based on a condition, you can use a combination of break and conditional statements.
🌐
Reddit
reddit.com › r/csharp › when to use a for loop and when to use a while loop?
r/csharp on Reddit: When to use a for loop and when to use a while loop?
August 1, 2022 -

I’m relatively new to coding and for certain projects I always wonder whether I should use a for loop or a while loop. It seems like the results often don’t differ much. Could someone explain when to use which and what the differences are? Or does it really ‘not matter’?

🌐
Codefinity
codefinity.com › courses › v2 › a5c23211-8dc2-4c4a-9a83-07a51e843ab6 › cc84bb48-f37c-4b78-9d06-9102f2fd10f3 › d40d791e-3036-445c-bf0d-e245178ef46c
Learn Iteration with for Loop | Loops
Then, within the loop, we initialize our variable i. Initially, i is set to 1, and the variable i represents the number we will add with each iteration. This continues until we add the number 100. After that, the loop stops.
🌐
CS UIC
cs.uic.edu › ~jbell › CourseNotes › C_Programming › Looping.html
C Programming Course Notes - Looping Constructs
If you know ( or can calculate ) how many iterations you need, then use a counter-controlled ( for ) loop. Otherwise, if it is important that the loop complete at least once before checking for the stopping condition, or if it is not possible or meaningful to check the stopping condition before the loop has executed at least once, then use a do-while loop.
🌐
SideFX
sidefx.com › forum › topic › 89010
For loop stop conditions | Forums | SideFX
February 25, 2023 - From what I thought I understood about the stop condition in for loops. If the stop condition is set to 1 on iteration 3, it will finish the process and output anything from the 3rd iteration and stop processing at the start of iteration 4.
🌐
freeCodeCamp
freecodecamp.org › news › javascript-loops-explained-for-loop-for
JavaScript Loops Explained: For Loop, While Loop, Do...while Loop, and More
February 15, 2020 - Loops are used in JavaScript to perform repeated tasks based on a condition. Conditions typically return true or false. A loop will continue running until the defined condition returns false. for Loop Syntax for (initialization; condition; ...
🌐
Reddit
reddit.com › r/javahelp › how do you know when to stop in a loop?
r/javahelp on Reddit: How do you know when to stop in a loop?
March 21, 2023 -

I have a problem assigned in which it asks for the variable z for the given values of variables a, b and c.

The code is as follows:

mult = 0; 

while (a < 10) {  
     mult = b * a; 
     if (mult > c) {  
        break;  
    }  
    a = a + 1; 
} 
z = a; 

a = 4, b = 5, c = 20

So I understood that z = 5 after going through it once so I input this answer and got it right. However, in its explanation it tells me that there's a second iteration where mult is assigned 25 (5*5), then the if branch executes and breaks the loop. Had I not been told this I would have had no idea I had to go through this loop a 2nd time and just assumed it went through it once.

So my question is how do you know how many iterations a loop goes through? I got the answer right but I did not go through it a second time, I just went through it once. So do I have to continue running through the loop over and over until the if statement executes? What if there were no break statement?? I am so confused on this iterations bit. Thank you in advance!!

Top answer
1 of 4
2
The while loop has a "loop condition" (ie a < 10) and will loop until this condition evaluates to false (it may loop forever if the condition never evaluates to false). In addition you may have a "guard" (ie if(mult > c) break) that can force the loop to end if the guard condition evaluates to true. The loop continues until any of these conditions are meet. You may try to unfold the loop by writing on pice of paper the instructions that are evaluated, one by one, and the changes to the different variables for some given values.
2 of 4
2
You looked at the whole from a wrong point of view. You looked at it from an outstander's perspective instead of playing the computer and mentally executing the program (which you should actually have done). As an outstander, it is easy to see that z has to be 5 to fulfill the breaking condition, but the program doesn't know that. A computer only sees programs statement by statement from top to bottom, one statement at a time. So, the first thing it sees is that: a = 4 b = 5 c = 20 mult = 0 Then, the loop starts: while (a < 10) -> while (4 < 10) -> condition is fulfilled, let's enter the loop - 1st iteration mult = b * a -> mult = 4 * 5 -> mult = 20 if (mult > c) -> if (20 > 20) -> not fulfilled since 20 is not larger than 20, so, the following "break" is skipped a = a + 1 -> a = 4 + 1 -> a = 5 back to the start of the loop while (a < 10) -> still fulfilled, let's enter the loop - 2nd iteration mult = b * a -> mult = 5 * 5 -> mult = 25 if (mult > c) -> if (25 > 20) > condition is fulfilled break -> now, and only now, the break gets executed and breaks out of the loop z = a -> z = 5
🌐
Real Python
realpython.com › python-while-loop
Python while Loops: Repeating Tasks Conditionally – Real Python
July 10, 2026 - At the same time, the expression returns the user input so that it can be compared to the sentinel value "stop". Using a while loop with the built-in next() function may be a great way to fine-control the iteration process when working with iterators and iterables. To give you an idea of how this works, you’ll rewrite a for loop using a while loop instead.