# How to use for loop in C++

Loop in Programming is the process of repeating a specific block of code while a given condition is true. Generally codes are executed sequentially but there is a situation when you need to execute a block of code several number of times. A loop statement allows us to execute a statement or group of statements multiple times.

There are four types of Loop provided in C++, these are:

For Loop is a Counter Controlled Loop, which means that it execute the set statements if the number of iteration is known. This Loop can be used to solve any kind of problem where you now how many number of iteration should the loop iterate. It can't be used to solve such kind of problem like

The below picture shows the general structure of the Loop.

There are four types of Loop provided in C++, these are:

**For Loop**- While Loop
- Do While Loop
- Nested Loop

For Loop is a Counter Controlled Loop, which means that it execute the set statements if the number of iteration is known. This Loop can be used to solve any kind of problem where you now how many number of iteration should the loop iterate. It can't be used to solve such kind of problem like

**Sentinel**, only while loop can solve this kind of problem. What is**Sentinel**after all? Just read the following post where we discuss about the**while loop**.The below picture shows the general structure of the Loop.

In this structure there are four parts which are:

**Start**. Is the starting point of your loop where initial value is initialized.**Condition**. Is the Boolean expression which has two results, either true or false.**Statement**. Is the lists of statement that will be executed once the condition return to be true.**End**. Is the end your loop, and this termination will be executed if and only if the condition return false.

**For Loop Syntax.**

**for ( initial value; condition; step value )****{****Statement;****}****In the syntax of**

**for loop**there is the following meaning of each word.**for**. Is the keyword that is used for looping (repetition).**initial value**. Is the starting point of where you loop should start. Always are the number value that define the starting point of repetition.**condition**. Is the Boolean expression that is either**true**or**false**. If the it is**true**, it will execute the statements below the loop but if it is**false**, it will terminate and exit.**step value**. Is the number of steps (interval) from the initial value. I call it step value because it may increase( + ) or decrease ( - ) it depend to the nature of the problem that you solve.**Statement**. This can be more than one statement that you want to be executed if and only if the condition has**true**result.

From the description of the syntax, lets have one example that will elaborate the concept of this kind of loop.

**Example**: Write a program that will display even numbers from 0 to 100 inclusive. Using for loop

To solve this kind of question you have to as yourself about the following.

- What is initial value?
- What is final value?
- And how about step value?
- What is my output after all?

When you trying to ask these questions by yourself, the mind is getting wider to get the solution. Now let us answer those questions as following.

**What is initial value?**Because, the question say that, I have to display even numbers from 0 to 100 inclusive, here my initial value will be 0 and also my final value will be 100. So I have to write a loop that will repeat 100 times.**And how about step value?**My step value can be increment ( + ) or decrement ( - ) it depend to the nature of the question, but from my question say that I have to display even numbers from 0 to 100 inclusive. If this is the case, I have to know that, when the number start from 0 to 100 they increase or decrease? The answer is "they increase", so if they increase, they increase by what interval? The answer is "I don't know". So if you don't know just take interval of 1, such that they will start from 0 1 2 3 4 5 ……**What is my output after all?**My output should be an even numbers, so from the series of my numbers (0 1 2 3 4 5 …..) how can I get an even numbers (matter of simple calculation) just take**number%2 == 0 ⇢ even number.**

From the answer of my questions now the last part is to implement my answers in reality. The implementation will be:

**#include <iostream>**

using namespace std;

using namespace std;

**int main()**

{

for (int number = 0; number <= 100; number++)

{

if (number%2 == 0){

cout<<number<<" ";

}

}

}

{

for (int number = 0; number <= 100; number++)

{

if (number%2 == 0){

cout<<number<<" ";

}

}

}

I just one to explain only one thing from the peace of code because the rest ones have been discussed in previous posts (click here to learn an if statement)

**(number%2 == 0)**

**.**This condition has the following three parts.

**number%2****.**This mean that take the**number**divide by**2**, what value will remain? Here we use the remainder/modulus operator (**%**) which it takes the remain value after division process. So if the number is 4 the remainder will be 0, if the number is 5 remainder will be 1.**== .**This is the relation operator. Click here learn get concept about these operators.**0****.**This is value that is compared with the remain value by using comparison operator ( == ).

By this condition, the

**cout**statement will be executed if and only if the result of condition will be true, and the condition to be true it must have value of zero from left side (**number%2**)
and right side (

**0**). From left side, only even number will return zeros, and that is the only thing that gives us the result of all even numbers from our series of (0 1 2 3 4 5 ……..)
The output for this program will be

We are done...!!!

_{}

^{}

I hope now you understand how to use

**For Loop**in C++. Feel free to ask any question if you didn't understand then I can reply as faster as I can._{}^{}_{}

^{}

_{}

^{}

_{}

^{}

Please comment and share with your friends. Don't miss the following post where we will continue to learn different things and application of new concepts.

_{}

^{}

Thank you for reading, be blessed....!!!

_{}

^{}

## No comments