Loop
A loop is a control structure that allows a program to repeat an action multiple times.
Loops are useful when we want to perform the same instruction repeatedly, such as:
- counting numbers
- processing data
- displaying repeated output
Without loops, we would have to write the same code over and over again.
Why Loops Are Important
Loops help make programs:
- more efficient
- shorter and cleaner
- easier to maintain
Instead of repeating the same syntax many times, we only need to define:
- what should be repeated
- when the repetition should stop
How Loops Improve Efficiency
Using loops reduces:
- duplicated code
- execution time
- human error
In general:
- fewer instructions → faster execution
- clearer logic → easier debugging
How Loops Work (Conceptually)
Every loop usually has:
- a starting point
- a condition that controls repetition
- an action that is repeated
- a way to stop
As long as the condition is true, the loop continues running.
Types of Loops
There are several kinds of loops in programming. In this section, we will focus on two common ones:
-
For Loop Used when the number of repetitions is known in advance.
-
While Loop Used when repetition depends on a condition that is checked during execution.
Each type has its own use case and strengths.
Example (Concept Only)
Instead of writing:
print message
print message
print message
We can use a loop to repeat the instruction automatically.
The exact syntax depends on the programming language, which will be explained in the next pages.
What’s Next?
This section is divided into smaller parts:
- For Loop — repetition with a clear counter
- While Loop — repetition based on a condition
Take time to understand the concept of looping before moving on to syntax.
Summary
- A loop repeats instructions automatically
- Loops reduce repetitive code
- They improve efficiency and readability
- Loops are a core part of control flow in programming
Understanding loops is essential before learning more advanced topics.