A loop repeats a block of code until a condition is met
A loop is a programming structure that runs the same set of instructions over and over without you having to write them out each time. Instead of writing the same line of code ten times, you write it once and tell the program to repeat it ten times. The loop stops when a condition you set becomes true — for example, when a counter reaches a certain number, or when there are no more items to process.
Loops save time and make code shorter and easier to read. They also reduce mistakes, because you are not copying and pasting the same logic multiple times. If you need to change what happens inside the loop, you change it in one place instead of ten.
Key Takeaways
- A loop runs the same code block repeatedly until a specific condition stops it, letting you avoid writing the same instruction multiple times.
- The 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 foreach loop (runs once for each item in a list).
- Every loop needs a way to stop, called an exit condition, or it will run forever and freeze your program.
- Loops are used for tasks like processing lists of data, counting, searching, and repeating actions until a goal is reached.
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. The loop keeps track of the counter and stops when it reaches the limit you set.
For example, if you want to print the numbers 1 through 10, you write a for loop that starts at 1, stops at 10, and increases by 1 each time. The loop prints the number, increases the counter, checks if the counter is still 10 or less, and repeats. When the counter reaches 11, the condition is false, and the loop stops.
For loops are useful when you know exactly how many times you need to repeat something. They are also the standard way to go through a list or array one item at a time.
The while loop: repeating until a condition changes
A while loop keeps running as long as a condition is true. You do not set a fixed number of repetitions. Instead, you set a condition, and the loop checks that condition before each run. If the condition is true, the loop runs. If it becomes false, the loop stops.
For example, you might write a while loop that says "while the user has not entered the correct password, keep asking them to try again." The loop runs, asks for the password, checks if it is correct, and if it is not, the condition is still true, so the loop runs again. Once the user enters the right password, the condition becomes false, and the loop stops.
While loops are useful when you do not know in advance how many times you need to repeat something. They are also common in programs that wait for user input or respond to changing data.
The foreach loop: processing each item in a list
A foreach loop (also called a for-each loop) runs once for each item in a collection — such as a list, array, or set of names. You do not have to count the items or manage a counter yourself. The loop automatically moves to the next item after each run and stops when there are no more items left.
For example, if you have a list of five names and want to print each one, a foreach loop goes through the list, picks the first name, prints it, moves to the second name, prints it, and continues until all five names are printed. You write the print instruction once, and the loop handles the repetition.
Foreach loops are cleaner and safer than for loops when you are working with lists, because you do not have to worry about counting wrong or going past the end of the list.
Exit conditions: how loops know when to stop
Every loop must have an exit condition — a rule that tells the loop when to stop. Without one, the loop runs forever, and your program freezes or crashes. The exit condition is usually a comparison: "if the counter is greater than 10, stop" or "if the user entered the correct answer, stop."
In a for loop, the exit condition is built in: the loop stops when the counter reaches the limit. In a while loop, you write the condition yourself, and you are responsible for making sure something inside the loop changes that condition. For example, if you are asking a user for a password, the loop must actually check the password and set a variable to true when it is correct, or the loop will ask forever.
A common mistake is forgetting to change the condition inside the loop. If you write a while loop that says "while x is less than 10, do something," but you never increase x inside the loop, then x stays the same forever, and the loop never stops.
Nested loops: loops inside loops
You can put a loop inside another loop, called a nested loop. The inner loop runs completely for each single run of the outer loop. Nested loops are useful for working with two-dimensional data, like a grid or a table with rows and columns.
For example, if you want to print a 3-by-3 grid of asterisks, you use an outer loop to go through each row and an inner loop to print three asterisks in each row. The outer loop runs 3 times. Each time it runs, the inner loop runs 3 times. So the total number of asterisks printed is 3 times 3, or 9.
Nested loops can slow down a program if they run many times, because the total number of operations multiplies. A loop that runs 100 times inside a loop that runs 100 times runs 10,000 times total.
Common uses for loops in real programs
Loops appear in almost every program because they handle repetitive work. A web browser uses loops to load images on a page one at a time. A spreadsheet uses loops to calculate a formula for every cell in a column. A search engine uses loops to check each result against your search terms.
Loops also handle user input: a login screen loops until the user enters the correct password, a game loop runs the game logic over and over until the player quits, and a data processing program loops through a file line by line to extract information.
Without loops, programmers would have to write the same code hundreds or thousands of times, and programs would be enormous and impossible to maintain.
Frequently Asked Questions
What happens if a loop never stops?
The program freezes or crashes. This is called an infinite loop. It usually happens when the exit condition is never reached — for example, a while loop that checks if x is less than 10, but x is never increased inside the loop. Most programming environments let you stop a frozen program by pressing Ctrl+C or closing the window.
Can you use a for loop and a while loop to do the same thing?
Yes. A for loop that runs 10 times can be rewritten as a while loop that runs while a counter is less than 10. The for loop is cleaner when you know the exact number of repetitions, and the while loop is cleaner when the number depends on a condition that changes during the loop.
Why would you use a while loop instead of a for loop?
Use a while loop when you do not know how many times you need to repeat something. For example, if you are reading lines from a file until you reach the end, or asking a user for input until they enter something valid, a while loop is more natural because the number of repetitions depends on the data, not a fixed count.
What is the difference between a foreach loop and a for loop?
A for loop uses a counter to access items by position, so you write code like "for i from 0 to 9, do something with item at position i." A foreach loop automatically picks each item from a list without needing a counter, so you write "for each item in the list, do something with that item." Foreach is simpler and safer for lists.