C++ Program to Explain Loops Concept
Learn C++ step by step.
Explore blog under C++ - Loops
Jan 23, 2026
## Chapter 6: Loops
Many times in programming, we
have to perform the same task repeatedly.
For example:
- Counting from 1 to 100
- Printing the marks of all students
- Repeating a task 10 times
If we write a separate line of code each time,
the code will become:
- Very long
- Boring
- And error-prone
The solution to this problem is a Loop.
### What is a Loop? (Simple Language)
Meaning of Loop:
As long as the condition is true,
keep running the same code repeatedly.
There are mainly three types of loops in C++:
1. for loop
2. while loop
3. do-while loop
Let's start with the most common loop: the for loop
### For Loop
When we know in advance:
- How many times the loop needs to run
Then the for loop is the best choice.
**Example: Printing numbers from 1 to 5**
C++
```
#include <iostream>
using namespace std;
int main() {
// Print numbers from 1 to 5
for (int i = 1; i <= 5; i++) {
cout << i << " ";
}
return 0;
}
```
Output:
C++
```
1 2 3 4 5
```
Now let's understand the for loop line by line
**Structure of a for loop**
C++
```
for (initialization; condition; update) {
// loop body
}
```
It has three parts:
1. Initialization – Starting point
C++
```
int i = 1;
```
- Runs once before the loop starts
- Here, a counter named 'i' is created
- Its initial value = 1
1. Condition – Checking
C++
```
i <= 5;
```
- Checked before each iteration of the loop
- If the condition is true → the loop will run
- If false → the loop stops
1. Update – Changing
C++
```
i++;
```
- Runs after each iteration
- Increments the value of i by 1
**How does the entire flow work?**
1. 1. i = 1 (once)
2. i <= 5 → true
3. Body executes → 1 print
4. i++ → Now i = 2
5. Condition checked again
6. This process repeats
7. When i = 6
→ Condition false
→ Loop stops
**Beginner Tips**
- The loop variable (i) is often
named i, j, k
- The code inside { }
runs repeatedly
- Make sure the loop doesn't become infinite
Remember in one line:
If you know how many times to run beforehand → for loop
### While loop:
When we don't know beforehand
how many times the loop will run,
the while loop is the best option.
In simple words:
As long as the condition is true,
the loop will continue to run.
**When to use a while loop?**
- When taking input from the user
- When the condition depends on an event
- When the number of iterations is not fixed
**Example: Checking a password**
C++
```
#include <iostream>
using namespace std;
int main() {
int password;
cout << "Enter password: ";
cin >> password;
while (password != 1234) {
cout << "Incorrect password! Try again: ";
cin >> password;
}
cout << "Access granted!";
return 0;
}
```
Now let's understand it line by line
**while (password != 1234)**
C++
```
while (password != 1234)
```
This means:
- As long as the password is not equal to 1234
- The loop will continue to run
As soon as the correct password is entered:
- Condition becomes false
- Loop stops
**What happens inside the loop?**
C++
```
cout << "Incorrect password! Try again: ";
cin >> password;
```
- A message is displayed to the user
- Input is taken again
- The condition is checked again with the new value
**How does the entire flow work?**
1. 1. The user enters the password.
2. The condition is checked.
3. If incorrect → it will go inside the loop.
4. It will ask for the password again.
5. If correct, the loop ends.
**For vs While**
| For Loop | While Loop |
| :--- | :--- |
| Number of iterations is known | Number of iterations is unknown |
| Counter-based | Condition-based |
| Fixed repetition | Dynamic repetition |
**Beginner Tips**
Before a while loop:
- It's important to initialize the variable.
- The condition must change inside the loop.
- Otherwise, it will become an infinite loop.
Real-Life Examples
- Password verification
- Login system
- User input validation
- Game loops
### Do-While Loop:
The do-while loop is similar to the while loop,
but there is a very big difference.
This loop runs at least once,
whether the condition is true or false.
**When to use a do-while loop?**
When you want:
- The user to see the options first
- Then the condition to be checked
- And the program to execute at least once
For example:
- Menu-based programs
- User choice systems
- Displaying options in games
**Example: Menu Program**
C++
```
#include <iostream>
using namespace std;
int main() {
int option;
do {
cout << "Menu:n";
cout << "1. Playn";
cout << "2. Settingsn";
cout << "3. Exitn";
cout << "Choose your option: ";
cin >> option;
} while (option != 3);
return 0;
}
```
Now let's understand it in simple terms:
**do { ... }**
C++
```
do {
// code
}
```
- First, this block runs.
- The menu is displayed.
- Input is taken from the user.
This part runs without checking the condition.
**while (option != 3);**
C++
```
while (option != 3);
```
- Now the condition is checked.
- If the option is not 3
→ the loop will run again.
- If the option is 3
→ the loop ends.
**How does the complete flow work?** - Menu will be displayed
- User will enter an option
- Condition will be checked
- If option is not 3 → show menu again
- If option is 3 → exit
**while vs do-while**
**Beginner Tips**
- Don't forget to put a semicolon (;) after the do-while
loop.
- This is the best loop for menu programs.
- The condition always comes with the while statement.
Real-Life Examples
- ATM menu
- Game menu
- Mobile app options
- System settings
### Loop Control Statements:
Sometimes we need to:
- stop the loop midway
- or skip one iteration and continue
For this purpose, C++ provides:
break and continue
### break statement
(Immediately stops the loop)
break means:
Exit the loop immediately
regardless of whether the loop has finished or not.
**Example: Using break**
C++
```
#include <iostream>
using namespace std;
int main() {
for (int num = 1; num <= 10; num++) {
if (num == 5) {
break; // As soon as num becomes 5, the loop stops
}
cout << num << " ";
}
return 0;
}
```
Output:
C++
```
1 2 3 4
```
What happened inside?
1. num = 1 → print
2. num = 2 → print
3. num = 3 → print
4. num = 4 → print
5. num = 5 → break encountered
6. The loop ends immediately
Therefore, 5 is not printed.
### continue statement
(Skips only one iteration)
continue means:
Skip this iteration,
go to the next iteration
The loop doesn't stop,
only one step is skipped.
**Example: Using continue**
C++
```
#include <iostream>
using namespace std;
int main() {
for (int value = 1; value <= 5; value++) {
if (value == 3) {
continue; // It will skip 3
}
cout << value << " ";
}
return 0;
}
```
Output:
```
1 2 4 5
```
What happened inside?
1. value = 1 → print
2. value = 2 → print
3. value = 3 → continue
- The print part is skipped
1. value = 4 → print
2. value = 5 → print
The loop continued,
only 3 was skipped.
**break vs continue**
| Feature | `break` | `continue` |
| :--- | :--- | :--- |
| Stops the loop | yes | no |
| Skips one iteration | no | yes |
| Exits | Immediately | No |
Remember it in a real-life way:
- break → Leaving the entire class
- continue → Skipping one period
How to remember it in one line:
To end the loop → break
To skip one iteration → continue
## Conclusion
In this article, we explored the core concepts of Explore blog under C++ - Loops. Understanding these fundamentals is crucial for any developer looking to master this topic.
## Frequently Asked Questions (FAQs)
**Q: What is Explore blog under C++ - Loops?**
A: Explore blog under C++ - Loops is a fundamental concept in this programming language/topic that allows developers to perform specific tasks efficiently.
**Q: Why is Explore blog under C++ - Loops important?**
A: It helps in organizing code, improving performance, and implementing complex logic in a structured way.
**Q: How to get started with Explore blog under C++ - Loops?**
A: You can start by practicing the basic syntax and examples provided in this tutorial.
**Q: Are there any prerequisites for Explore blog under C++ - Loops?**
A: Basic knowledge of programming logic and syntax is recommended.
**Q: Can Explore blog under C++ - Loops be used in real-world projects?**
A: Yes, it is widely used in enterprise-level applications and software development.
**Q: Where can I find more examples of Explore blog under C++ - Loops?**
A: You can check our blog section for more advanced tutorials and use cases.
**Q: Is Explore blog under C++ - Loops suitable for beginners?**
A: Yes, our guide is designed to be beginner-friendly with clear explanations.
**Q: How does Explore blog under C++ - Loops improve code quality?**
A: By providing a standardized way to handle logic, it makes code more readable and maintainable.
**Q: What are common mistakes when using Explore blog under C++ - Loops?**
A: Common mistakes include incorrect syntax usage and not following best practices, which we've covered here.
**Q: Does this tutorial cover advanced Explore blog under C++ - Loops?**
A: This article covers the essentials; stay tuned for our advanced series on this topic.