C++ for loop

Page Contents:

For Loop in C++
Nested for Loop in C++

For Loop in C++

For loop in C++
For loop in C++

A for loop provides a step-by-step recipe for performing repeated actions. Let’s take a more detailed look at how it’s set up. The usual parts of a for loop handle these steps:

  1. Setting a value initially
  2. Performing a test to see whether the loop should continue
  3. Executing the loop actions
  4. Updating values used for the test

The C++ loop design position these elements so that you can spot them at a glance. The initialization, test, and update actions constitute a three-part control section enclosed in parentheses. Each part is an expression, and semicolons separate the expression from each other. The statement following the control section is called the body of the loop, and it is executed as long as the test expression remains true:

for(initialization;condition;increment/decrement/updation)
{
-
-
statement;
-
-
}

C++ syntax counts a complete for a statement as a single statement, even though it can incorporate one or more statements in the body portion.

Note* In the C++ standard, the scope of the control variable declared in the initialization section of a for structure differs from the scope in older C++ compilers.In pre-standard compilers, the scope of the control variable does not terminate at the end of the block defining the body of the for structure; rather, the scope terminates at the end of the block that encloses the for standard-complaint compilers.If you are working with pre-standard compilers and you want to be sure your code will work with standard-compliant compilers, there are two defensive programming strategies that can be used to prevent this problem: either declare control variables with different names in every for structure, or if prefer to use the same name for the control variable in several for structures, define the control variable before the first for structure

The for repetition structure handles all the details of counter-controlled repetition. To illustrate the power of for,

The three expressions in the for structure header are optional. If the loop Continuation Condition is omitted, C++ assumes that the loop-continuation condition is true, thus creating an infinite loop. One might leave the initialization expression if the control variable is initialized earlier in the program. One might omit the increment expression if the increment is calculated by statements in the body of the for or if no increment is needed. `The increment expression in the for structure acts as a stand-alone statement at the end of the body of for.

Why for loop is called entry control loop?

The for loop is an entry-control loop. this means the test expression is evaluated before each loop cycle. The loop never executes the loop body when the test expression is false

1.Example for loop in C++ increment of n natural numbers

#include<iostream> 
using namespace std;
  int main()
{
  for(int i=1;i<=10;i++)
{
  cout<<i <<'\n'; 
 }
return 0;
 } 

OUTPUT:

1
2
3
4
5
6
7
8
9
10

2.Displaying the infinite loop using for statement

#include<iostream> 
using namespace std;
int main ()
{
 for (;;)
{
 cout<<"Infinitive For Loop";
  }
return 0;
  } 

OUTPUT:

Infinitive For Loop
Infinitive For Loop
Infinitive For Loop
Infinitive For Loop
Infinitive For Loop
............
............
............
............
............
............And so on

Nested for Loop in C++

  • A for loop inside another for loop is called as nested for loop
  • When a particular operation has two references, we require nested for loop. For example, if we want to keep a reference of row number and column number, then we can use a nested for loop
#include<iostream>
using namespace std;
void main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=2;j++)
{
cout<<"Hello";
}
cout<<"Hi"<<'\n';
}
return 0;
}

Translate »