For Loop In C++ Programming Language

 For Loop In C++ Programming Language

In Programming languages looping statements are used with the help of these looping statements we can repeat some block of code according to our requirement of the program. In C++ programming language we also use these looping statements, these looping statements are also referred as controlled structure.

In this blog post we are going to cover for loop statement which is used to execute a block of code repeatedly for a specific number of times. The for loop is "entry controlled" or "pre-tested" loop which means that the condition is checked before the code inside the loop ever runs. We use for loop when we know exactly how many times the loop will going to run.



Basic syntax and working of for loop in C++ programming language


Syntax of for loop 

for (initialization; condition; increment)
{
	// code to be executed here.
}


Working of for loop in C++ programming language

Now let us look at the basic syntax of for loop in C++ programming language. Starting with a for key word followed by opening and closing round brackets and with in these round brackets we can see three expressions.

1) initialization

2) condition

3) increment

As by now we know that loops are used to repeat a block of code. And if the numbers of iterations is fixed it is preferred to use the for loop than while or do-while loops. The for loop consists of three main parts, first part is the initialization, second part is condition, and the third part is the increment i.e, for (Initialization; Condition; Increment)

In the initialization part we simply declare and initialize a variable like int i = 1, in the condition part we simply make some condition that might be true or false like i <= 10, and the last part of the for loop is the increment part which is i++. In the last part we can either increment or decrement the value of i variable.

And after that we have opening and closing curly brackets and with in these brackets we can write the piece of code or the logic for the for loop. Just keep one thing in mind that when ever we are sure that how many times we want to repeat a piece of code then we should use for loop instead of while or do while loop.

Simple Programming Examples of for loop in C++ programming Language


Programming Example 1
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
	for (int i = 1; i <= 10; i++)
	{
		cout << i << endl;
	}
	_getch();
}


Explanation of coding example 1

In the first programming example we have created a for loop and declared and initialized the value of variable i like this int i = 1. After that we  simply write a condition and specifies that this loop will run untill value of i is less or equal to 10 like this i <= 10. And at last part of the for loop we simply increment the value by adding 1 like this i++ which is equal to i = i + 1.
for (int i = 1; i <= 10; i++)
After that we have opening and closing curly brackets with in which we have simply printed the values from 1 to 10 using the value of i variable with cout statement.

 cout << i << endl;

This program will simply print numbers from 1 to 10 in console screen.


Programming Example 2
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
	int TableNumber, TableLength;
	cout << "Enter Table Number = ";
	cin >> TableNumber;
	cout << "Enter Table Length = ";
	cin >> TableLength;
	for (int i = 1; i <= TableLength; i++)
	{
		cout << TableNumber << " * " << i << " = " << TableNumber * i << endl;
	}
	_getch();
}


Explanation of coding example 2

In the second programming example we will simply take two inputs the first one is the TableNumber and the second one is the TableLength. 
int TableNumber, TableLength;
cout << "Enter Table Number = ";
cin >> TableNumber;
cout << "Enter Table Length = ";
cin >> TableLength;

This program will simply prints the table of any given number and upto some specific range of numbers like from 1 to 10 or from 1 to 20, etc.

And with in the for loop we will simply use TableLength number to specify the range of numbers upto specific table length. 
for (int i = 1; i <= TableLength; i++)
If we enter TableNumber = 2 and TableLength = 10 then this program will print the table of number 2 from 1 to 10 i.e,

2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
.
.
.
2 * 10 = 20

Congratulations! like always we have successfully implemented the for loop in C++ programming language.

Comments

Popular posts from this blog

How to Download and Install Microsoft visual studio community 2022 for free

Hello World Program In C Programming Language

if and if-else statement in C programming language