Home | | Computer Science 11th std | C++ for loop

Chapter: 11th Computer Science : Chapter 10 : Flow of Control

C++ for loop

The for loop is the easiest looping statement which allows code to be executed repeatedly.

for loop

 

The for loop is the easiest looping statement which allows code to be executed repeatedly. It contains three different statements (initialization, condition or test-expression and update expression(s)) separated by semicolons.

 

The general syntax is:

for (initialization(s); test-expression; update expression(s))

{

      Statement 1;

      Statement 2;

      ………….

}

Statement-x;

The initialization part is used to initialize variables or declare variable which are executed only once, then the control passes to test-expression. After evaluation of test-expression, if the result is false, the control transferred to statement-x. If the result is true, the body of the for loop is executed, next the control is transferred to update expression. After evaluation of update expression part, the control is transferred to the test-expression part. Next the steps 3 to 5 is repeated. The workflow of for loop and flow chart are shown below.


 

Illustration 10.11 C++ program to display numbers from 0 to 9 using for loop

#include <iostream>

using namespace std;

int main ()

{

int i;

for(i = 0; i< 10; i ++ )

      cout<< "value of i : " <<i<<endl;

return 0;

}

Output

value of i : 0

value of i : 1

value of i : 2

value of i : 3

value of i : 4

value of i : 5

value of i : 6

value of i : 7

value of i : 8

value of i : 9

 

The following lines describes the working of the above given for loop:


In the above program, first the variable i is initialized, next i is compared with 10, if i is less than ten, the value of i is incremented. In this way, the numbers 0 to 9 are displayed. Once i becomes 10, it is no longer < 10. So, the control comes out of the for loop.

 

Illustration 10.12 C++ program to sum the numbers from 1 to 10 using for loop

#include <iostream>

using namespace std;

int main ()

{

int i,sum=0;

for(i=1; i<=10;i++)

{

      sum=sum+i;

}

cout<<"The sum of 1 to 10 is "<<sum;

return 0;

}

Output

The sum of 1 to 10 is 55

 

Variations of for loop

The for is one of the most important looping statement in C++ because it allows a several variations. These variations increase the flexibility and applicability of for loop. These variations will be discussed below:

 

Multiple initialization and multiple update expressions

Multiple statements can be used in the initialization and update expressions of for loop. These multiple initialization and multiple update expressions are separated by commas. For example,


Output

The value of i is 0 The value of j is 10

The value of i is 1 The value of j is 9

The value of i is 2 The value of j is 8

The value of i is 3 The value of j is 7

The value of i is 4 The value of j is 6

In the above example, the initialization part contains two variables i and j and update expression contains i++ and j++. These two variables are separated by commas which is executed in sequential order i.e., during initialization firstly i=0 followed by j=10. Similarly, in update expression, firstly i++ is evaluated followed by j++ is evaluated.

 

Prefer prefix operator over postfix

Generally, the update expression contains increment/decrement operator (++ or --). In this part, always prefer prefix increment/decrement operator over postfix when to be used alone. The reason behind this is that when used alone, prefix operators are executed faster than postfix.

 

Optional expressions

Generally, the for loop contains three parts, i.e., initialization expressions, test expressions and update expressions. These three expressions are optional in a for loop.

 

Case 1

Illustration 10.13 (a) C++ program to sum the numbers from 1 to 10

#include <iostream>

using namespace std;

int main ()

{

int i, sum=0, n;

cout<<"\n Enter The value of n";

cin>>n;

i =1;

for ( ; i<=10;i++)

      {

      sum += i;

     }

cout<<"\n The sum of 1 to " <<n<<"is "<<sum;

return 0;

}

Output

Enter the value of n 5

The sum of 1 to 5 is 15

 

In the above example, the variable i is declared and sum is initialized at the time of variable declaration. The variable i is assigned to 0 before the for loop but still the semicolon is necessary before test expression. In a for loop, if the initialization expression is absent then the control is transferred to test expression/conditional part.

 

Case 2

Illustration 10.13 (b) C++ program to sum the numbers from 1 to 10

#include <iostream>

using namespace std;

int main ()

{

int i, sum=0, n;

cout<<"\n Enter The value of n";

cin>>n;

i =1;

for ( ; i<=10; )

{

      sum += i;

      ++i;

}

cout<<"\n The sum of 1 to " <<n<<"is "<<sum;

return 0;

}

Output

Enter the value of n 5

The sum of 1 to 5 is 15

 

In the above code, the update expression is not done, but a semicolon is necessary before the update expression.


In the above code, neither the initialization nor the update expression is done in the for loop.

If both or any one of expressions are absent then the control is transferred to conditional part.

 

Case 3

An infinite loop will be formed if a test-expression is absent in a for loop. For example,


Similarly, the following for loop also forms an infinite loop.


 

Empty loop

Empty loop means a loop has no statement in its body is called an empty loop. Following for loop is an empty loop:


In the above code, the for loop contains a null statement, it is an empty loop.

Similarly, the following for loop also forms an empty loop.


In the above code, the body of a for loop enclosed by braces is not executed at all because a semicolon is ended after the for loop.

 

Declaration of variable in a for loop

In C++, the variables can also be declared within a for loop. For instance,


A variable declared inside the block of main() can be accessed anywhere inside main() i.e., the scope of variable in main()

 


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
11th Computer Science : Chapter 10 : Flow of Control : C++ for loop |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.