For loop in c++
C++ For loop
In the topic of for loop in c++, for loop is very important in coding because it is easy to write in the programs compared to there available loop in the CPP programming.
In the computer language, loops are using for the execute the block of code again and again until the programmer wants to execute the block of code.
Sometimes we need to display something for different times in the program. we use loops in the program to print such things for our requirements.
There are three types of loops used in the CPP language.
- for loop
- do-while loop
- while loop
Here , we will discuss the for loop in the detail. Following is the syntax of the for loop.
What is syntax of the for loop in the cpp?
syntax :
(Initialization, terminated condition, increment or decrement in counter)
Execution process :
First of all initialization part execute then go to terminated condition, if condition true then code in the loop execute, if condition false then loop code is not executed.
The very important thing in this for loop is that if condition true, piece of block executed after that counter increment or decrement, not go to condition. After counter then check the condition again.
Examples:
#include<iostream>
using namespace std;
int main()
{
int x;
for(x=1;x<=5;x++)
{
cout<<"hello user";//display hello user in the every iteration
cout<<endl;//next line
}
return 0;
}
If any problem in cout then visit cout
In this code, counter of the loop does not increase from the five numbers. Five iterations will be executed in every iteration print hello user .
output:
Example:
The program to sum of the first 5 numbers?
#include<iostream>
using namespace std;
int main()
{
int x,num;
num=0;
for(x=1;x<=5;x++)
{
num+=x;//the value of x add in the num
}
cout<<num;//print num
cout<<endl;
return 0;
}
output:
15
In the above example, sum the number with help of for loop. Firstly, initialize the number (num=0), then apply for loop by controlling the counter of the loop.5 is the terminated condition of the loop.
The body of the loop add the counter(x) into the num variable after the loop terminates display the value of the num.
Example3
#include<iostream>
using namespace std;
int main()
{
int x,num;
for(x=10;x>0;x--) //decrement loop
{
cout<<" the value of counter "<<x;//display x variable for every iteration
cout<<endl;
}
return 0;
}
Output
the value of counter 10
the value of counter 9
the value of counter 8
the value of counter 7
the value of counter 6
the value of counter 5
the value of counter 4
the value of counter 3
the value of counter 2
the value of counter 1
In the above example, decrement loop use for printing the number in reverse order from 10 to 1.
Firstly, initialize the variable by 10 and start the counter of the for loop by 10.Decremnt in every iteration we can use decrement as increment, and display the counter value.
we can also use decremental instead of incremental its totally depend on our logic building.
Please write the comment,if you find anything incorrect,any problem in the above topic or you want to share more information about the above topic.
Comments
Post a Comment