A loop repeats a block of code until a condition is met
A loop is a programming instruction that runs the same code over and over without you having to type it multiple times. Instead of writing the same line ten times, you write it once and tell the computer to repeat it ten times. The loop keeps running until you tell it to stop — usually by reaching a number you set in advance, or when a condition becomes true or false.
Loops save time and make code shorter and easier to read. They also reduce mistakes because you are not copying and pasting the same instruction over and over. If you need to change what happens in the loop, you change it in one place, and the change applies to every repetition.
Key Takeaways
- A loop runs the same block of code multiple times without requiring you to write it out repeatedly.
- The three most common loop types are the for loop (runs a set number of times), the while loop (runs until a condition becomes false), and the do-while loop (runs at least once, then checks the condition).
- Loops use a counter or condition to decide when to stop, preventing infinite loops that would freeze your program.
- Nested loops (loops inside loops) let you repeat actions across multiple layers, like printing a grid or table.
The for loop: repeating a set number of times
A for loop runs a block of code a specific number of times. You tell it where to start, where to stop, and how much to increase the counter each time. This is the most common loop type because you usually know in advance how many times you need to repeat something.
For example, if you want to print the numbers 1 through 10, a for loop does this in three lines instead of ten separate print commands. You set the counter to start at 1, stop when it reaches 11, and increase by 1 each time. The loop prints the counter, increases it, and repeats until the counter hits 11, then stops.
The for loop is predictable — it will always run the exact number of times you specify. This makes it safe and straightforward to understand when you read the code later.
The while loop: repeating until a condition changes
A while loop keeps running as long as a condition is true. The moment the condition becomes false, the loop stops. You do not need to know in advance how many times it will run — only what condition will make it stop.
For example, a while loop might keep asking a user for a password until they type the correct one. The loop says "keep asking while the password is wrong." Once the user types the right password, the condition becomes false and the loop exits. The number of repetitions depends on how many times the user guesses wrong.
While loops are useful when the stopping point depends on something that changes during the loop, like user input or data from a file. However, you must make sure the condition will eventually become false, or the loop will run forever.
The do-while loop: checking the condition after running
A do-while loop is similar to a while loop, but it checks the condition after running the code, not before. This means the code inside always runs at least once, even if the condition is false from the start.
This is useful when you want to ask a user a question, get their answer, and then decide whether to ask again. The question gets asked once no matter what, and then the loop checks whether to repeat. For example, a menu system might show options, let the user pick one, and then ask "do you want to see the menu again?" If yes, the loop repeats; if no, it stops.
Nested loops: loops inside loops
A nested loop is a loop inside another loop. The inner loop completes all its repetitions for each single repetition of the outer loop. This creates a pattern of repeated repetitions, useful for working with grids, tables, or multi-dimensional data.
For example, if you want to print a 3-by-3 grid of asterisks, you use an outer loop to handle rows and an inner loop to handle columns. The outer loop runs 3 times (once per row). Each time the outer loop runs, the inner loop runs 3 times (once per column). This produces 9 asterisks arranged in a square without writing out all 9 positions by hand.
Nested loops can become complex quickly, so they are usually limited to two or three levels deep. If you nest too many loops, the code becomes hard to read and the program may run slowly.
Break and continue: controlling loop flow
Two keywords let you change what happens inside a loop: break and continue. The break statement exits the loop when ready, even if the condition says it should keep running. The continue statement skips the rest of the current repetition and jumps to the next one.
Break is useful when you find what you are looking for and do not need to keep searching. For example, if you are looping through a list of names looking for "Alice," you can break as soon as you find her instead of checking every remaining name. Continue is useful when you want to skip certain items. For example, if you are looping through numbers and want to skip even numbers, you use continue to jump to the next number without processing the current one.
Common mistakes to avoid
The most dangerous mistake is creating an infinite loop — a loop that never stops. This happens when the condition never becomes false or the counter never reaches its limit. Your program will freeze and stop responding. To avoid this, always make sure the loop has a clear way to exit, and test your code with small numbers first.
Another common mistake is off-by-one errors, where the loop runs one too many times or one too few. This often happens with for loops when you confuse whether the stop number is included or excluded. For example, if you want to loop from 1 to 10, you might accidentally set the loop to stop at 9 or 11. Test your loops with print statements to see exactly how many times they run.
A third mistake is forgetting to update the counter in a while loop. If the counter never changes, the condition never becomes false and you get an infinite loop. Always make sure something inside the loop moves you closer to the stopping condition.
Frequently Asked Questions
What is the difference between a for loop and a while loop?
A for loop runs a set number of times that you know in advance. A while loop runs until a condition becomes false, and you may not know in advance how many times that will be. Use a for loop when you know the exact count; use a while loop when the count depends on something that changes during the loop.
Can a loop run zero times?
A for loop can run zero times if the starting value is already past the stopping value. A while loop runs zero times if the condition is false from the start. A do-while loop always runs at least once because it checks the condition after the first run.
What happens if I forget to update the counter in a while loop?
The counter stays the same, the condition never changes, and the loop runs forever. Your program will freeze. Always make sure something inside the loop moves you closer to making the condition false.
Why would I use a nested loop instead of just one loop?
Nested loops let you repeat actions across multiple dimensions or categories. For example, one loop handles rows and another handles columns, creating a grid. Without nesting, you would need much longer and more complicated code to achieve the same result.
Is there a loop type that runs backwards?
Yes. A for loop can count backwards by setting the counter to start high and decrease instead of increase. For example, you can start at 10 and decrease by 1 each time until you reach 0. The logic is the same as counting forward; only the direction changes.