Learn Loops in C++ with Code and Output

Want to learn loops in C++ the easy way? Loops are one of the best tricks in programming. They help you repeat tasks without writing the same code again and again. If you’re just starting with C++, loops will save you time and make your code cleaner. In this guide, we’ll break down each type of loop in C++ with beginner-friendly examples, real output, and simple words that are easy to understand in just a few minutes!

Learn Loops in C++ with Code and Output

Table of Contents

  • What Are Loops in C++?
  •  
  • Why Do We Use Loops in C++?
  •  
  • For Loop in C++
  •  
  • While Loop in C++
  •  
  • Do-While Loop in C++
  •  
  • Range-Based For Loop
  •  
  • Nested Loops in C++
  •  
  • Common Loop Mistakes
  •  
  • Infinite Loops in C++
  •  
  • Loop Control Statements
  •  
  • Best Loop for Your Task
  •  
  • Conclusion
  •  
  • FAQs

Key Points You’ll Learn

  • How to use each loop in C++ with simple code
  •  
  • What output does each loop give
  •  
  • Common mistakes beginners make
  •  
  • Which loop to use for which problem
  •  
  • Easy examples to practice

What Are Loops in C++?

Imagine brushing your teeth every morning. You don’t need to be told again because it has become a habit. In coding, loops do the same. They repeat actions again and again until you say "stop!"

Loops are used to repeat tasks in your program.
Instead of writing 10 lines to print numbers from 1 to 10, you can write just 3 lines using a loop.

In simple words:

  • Loops make your code repeat things.
  •  
  • They help you save time.
  •  
  • Your code becomes cleaner and shorter.
  •  
  • It’s great for automation and repetition.

Why Do We Use Loops in C++?

  • To automate repetition
  •  
  • To handle large sets of data
  •  
  • To make the code shorter and cleaner
  •  
  • To save time and reduce errors
  •  
  • To make the program easy to understand and debug
  •  

For example, what if you had to send 100 messages to users? Instead of writing 100 lines, you use a loop once.

1. For Loop in C++

🔹 When to Use a For Loop

Use a for loop when you know how many times you want to repeat something.
Like printing numbers from 1 to 5, or looping through the first 10 items in a list.

🔹 Syntax + Code Example

Copy Code

for(int i = 1; i <= 5; i++) {

    cout << i << endl;

}

Output:

1

2

3

4

5

What This Code Does:

  • int i = 1: Start from 1
  •  
  • i <= 5: Keep going while i is less than or equal to 5
  •  
  • i++: Add 1 after every loop
  •  
  • cout << i: Print the current number
  •  

This loop prints 1 to 5. Clean and simple!

2. While Loop in C++

🔹 When to Use a While Loop

Use a while loop when you don’t know how many times something will repeat.
It keeps going while a condition is true.

🔹 Syntax + Code Example

Copy Code

int i = 1;

while(i <= 3) {

    cout << "Hi!" << endl;

    i++;

}



🔹 Output:

Hi!

Hi!

Hi!

⚠︎Warning:

If you forget i++, the loop never ends. This is called an infinite loop.
So always update the variable inside the loop.

 3. Do-While Loop in C++

🔹 Key Difference

A do-while loop always runs at least once, even if the condition is false.

🔹 Code Example:

Copy Code

int i = 1;

do {

    cout << "This runs once!" << endl;

} while(i > 1);



🔹 Output:



This runs once!

 What This Code Does:

  • Runs the code first
  •  
  • Then checks if i > 1
  •  
  • Even though it's false, the message prints once
  •  

 do-while always runs once, no matter what.

Bonus: Range-Based For Loop (C++11 and Later)

🔹 What It Is:

range-based for loop is a cleaner way to go through arrays or vectors. It saves you from writing index values.

Code Example:

Copy Code

int nums[] = {10, 20, 30};

for(int n : nums) {

    cout << n << endl;

}

 Output:

10  

20  

30

Works great with arraysvectors, and collections.

Nested Loops in C++

🔹 What is Nesting?

nested loop is when one loop runs inside another.
This is helpful when you’re working with patternstables, or 2D arrays.

Example: Pattern Print

Copy Code

for(int i = 1; i <= 3; i++) {

    for(int j = 1; j <= 2; j++) {

        cout << "* ";

    }

    cout << endl;

}

Output:

* * 

* * 

* *

What This Code Does:

  • Outer loop (i) runs 3 times (for rows)
  •  
  • Inner loop (j) runs 2 times each time (for columns)
  •  
  • Prints * * in every row
  •  

Nested loops help you create patterns and work with tables easily.

✖ Common Loop Mistakes

Here are some mistakes beginners make with loops:

  • ⟳ Forgetting to update variables (causes infinite loops)
  •  
  • Using = instead of == in conditions

  • Example: if(i = 3) is wrong, use if(i == 3)
  •  
  •  Wrong condition (loop never runs or runs forever)
  •  
  • 🔒 Not using {} for multiple lines in a loop
  •  

Pro Tip:

Always check your condition and update the steps. And use braces {} even if there's only one line because it is safer and helps avoid mistakes.

⚠︎ Infinite Loops in C++

🔹 What is an Infinite Loop?

An infinite loop never ends unless you break it.
It happens when the loop condition never becomes false.

Example:

Copy Code

while(true) {

    cout << "Looping forever!";

}

⚠︎ This loop will keep running and printing the line forever.

Testing Tip:

When trying loops, always add a stop condition or use a counter to break the loop after some time.

Loop Control Statements in C++

Loop control lets you change the flow inside loops using special keywords.

Break Statement

break stops the loop immediately.

Copy Code

for(int i = 1; i <= 5; i++) {

    if(i == 3) break;

    cout << i << endl;

}

Output:

1  

2

✔  Loop stopped when i == 3.

🔹 Continue Statement

continue skips the current step and goes to the next.

Copy Code

for(int i = 1; i <= 5; i++) {

    if(i == 3) continue;

    cout << i << endl;

}

Output:

1  

2  

4  

5

✔  Skipped printing 3, moved to the next number.

⚠︎ Quick Tip:

Use break and continue only when needed. They are powerful, but too many of them can make code harder to debug.

Best Loop for Your Task

Here’s a quick table to help you pick the right loop for the job:

SituationUse This Loop
Count known timesfor
Repeat until condition truewhile
Must run at least oncedo-while
Loop through arrays or listsrange-based for
Work with patterns or tablesnested loops

The more you practice, the better you understand loops.

Snippet for Google (SEO Snippet)

Loops in C++ help repeat actions. Use for, while, or do-while loops to avoid writing the same line again and again. Each loop type fits a specific problem. With just a few lines of code, loops make C++ faster, smarter, and more fun!

Conclusion

Now you know how to learn loops in C++, starting from simple repeats to advanced nested patterns. Practice is the most important step in mastering them. Run each example in your compiler and observe how the output changes when you make small adjustments. Try changing the numbers, the conditions, or the output messages.

You’ll begin to see how each part of the loop works. Even a tiny change in the code can teach you something new. Keep experimenting and don’t be afraid to make mistakes. Every step helps you get better. You are now ready to build cool and smart C++ programs with confidence and creativity.

FAQs

1. What is the best loop for beginners?
Start with the for loop because it is simple to understand and easy to control.

2. What happens if I forget to increase a variable in a loop?
You might create an infinite loop because the condition will never become false, and the loop will keep running without stopping.

3. What’s the difference between while and do-while?

  • while checking the condition before running.
  •  
  • do-while runs once first, then checks the condition.
  •  

4. Can loops be used with arrays?
Yes! Range-based for loops are great for arrays and vectors.

5. How many times does a loop run?
It depends on the condition. Try printing values to see the count.

Want to master C++ faster?
Practice each loop with different numbers and conditions.
Build small programs: like printing tables, showing even numbers, or making patterns.
Keep testing, keep learning!

Placed Students

Our Clients

Partners

...

Uncodemy Learning Platform

Uncodemy Free Premium Features

Popular Courses