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 of for loop in C++ Programming Language

for (Initialization; Condition; Increment)

{

//Code to be executed.

}


Basic Working Of For Loop in C++ Programming Language

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.

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





In the first programming example we 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.

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




In the second programming example we will simply take two inputs the first one is the TableNumber and the second one is the 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 the for loop we will simply use TableLength number to specify the condition upto specific table length. 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 executed the for loop program in C++ programming language.










Comments

Popular posts from this blog

How to run c project in visual studio community 2022

How To Write Hello World Program In C++ Programming Language

if and if else statement in C++ programming language