For Loop in C++
We just discussed about while
loop in C++ in the previous article. Now we're going to discuss for
loops in C++.
Syntax
How does this work?
- Initialization is where we decide where to start counting. We set a variable like 'count' and give it a starting value, for example, 'count = 1.'
- Boolean Expression is like deciding when to stop counting. We set a condition, like 'as long as count is less than or equal to 10.'
- Update tells us how much to count up after each step. We increase our 'count,' for instance, 'count++' to add 1 every time.
- Inside the curly braces '{ }', we write the actual steps or actions we want to happen during each iteration (loop cycle).
This might look a bit confusing. No worries! We've got you. Let's understand this with an example.
Explanation:
Quiz
How many times will a 'for loop' with the following initialization, condition, and update expressions run?
Some Cool Examples
Using a for loop to print numbers from a to b (a and b are given by the user as inputs).
Explanation:
Here's how the code works:
1. We prompt the user to input 'a' and 'b' and read the values using `cin`.
2. We display a message indicating the range of numbers we're going to print.
3. We use a 'for' loop to iterate from 'a' to 'b'. The loop's initialization sets 'i' to the value of 'a', and it continues as long as 'i' is less than or equal to 'b'.
4. Inside the loop, we print the current value of 'i' followed by a space.
5. The loop runs through all the numbers in the range from 'a' to 'b'.
6. The program terminates, and the numbers from 'a' to 'b' are displayed.
Follow up : print in reverse from b to a from the previous problem.
Solution code
Factorial Calculation
Explanation
We check if 'n' is negative. If it is, we inform the user that factorial is not defined for negative numbers.
If 'n' is non-negative, we proceed to calculate the factorial using a 'for' loop. The loop iterates from 1 to 'n', and in each iteration, it multiplies the 'factorial' by the current value of 'i'.
After the 'for' loop, we display the result, which is the factorial of 'n'.
Counting digits in a number
Explanation
Here's how the 'for' loop works in this program:
We didn't initialize anything in the 'for' loop's initialization part, but we are using the existing variables.
- We start by checking if the 'number' is greater than 0. If it is, we enter the loop.
- After each iteration, we update 'number' by dividing it by 10. This removes the last digit from 'number,' bringing us closer to zero.
- We also increment the 'count' by 1 in each iteration inside the 'for' loop. This keeps track of how many digits we've counted so far.
- The loop continues until 'number' becomes 0, which means we have counted all the digits.
We use the 'for' loop without a traditional initialization because we are utilizing the existing values. The update part, dividing 'number' by 10, is crucial for counting each digit. When 'number' reaches 0, the loop terminates, and we display the count of digits.
Introducing Nested for Loops
By now, you're familiar with the power of 'for' loops, which are like magic wands in the world of coding. They help us repeat actions. But here's where things get even more exciting: nested 'for' loops!
Think of nested 'for' loops as loops within loops, just like a set of Russian nesting dolls where each doll contains another. 🪆🪆🪆
Let's understand this with an example,
Output
Explanation:
Pattern Printing Problems
Print a Right-Angled Triangle
The output should look like :
let's say the input n = 6
Hence, you're basically required to ask user n as input and print n rows having the above pattern.
Explanation:
Reverse Right-Angled Triangle
The output should look like :
let's say the input n = 6
Hence, you're basically required to ask user n as input and print n rows having the above pattern.
Explanation:
Half Diamond
The output should look like :
let's say the input n = 5
So how do we do this?
Hint : this task is a combination of the other two tasks that we just did before this.
Explanation:
Hopefully, you enjoyed and learned a lot through this post.
Let's move to our final quiz problem.
Quiz
What will be the output of the following C++ program?
Join Our Course

Offer ends in:
Days
Hours
Mins
Secs