For loop in C++

category: 

The second looping construct is the for loop which is the useful form of the loop. In some cases it is necessary to execute a statement for a fixed number of times. In for loop, the number of iteration is specified and at each iteration, the loop control variable is assigned one of a sequence of values.

The general form of for-loop is:

For (expression1;expression2;expression3)

         Statement1;

For loop flow chart

 

The expression1 represents the initial value, constant or variable whose value is taken as the beginning value,the first time the loop is run.The expression 2 represent the Boolean expression and expression3  represents the update of the expression1 that the amount it is to be increased or decreased after each pass through the loop.Other wise it will remain in the same state.In other words,an infinite loop.The example of an infinite loop is shown next.It is not recommended to do so.

For(i=0;;i++)

{

Body of the loop

}

This is an infinite loop because there is no condition.There is no way that it will ever return false.

For(i=0;i<10;)

{

Body for  loop

}

The same is true for this example. The value if alcv (i) is not update. A few steps involved in this type of strucrure that how it is executed after step by step. First the initialization of expression1 is executed if it exists. The test is performed after the initialization of a lcv .if the test codition evaluates to true,the body of the loop is executed.Otherwise, control transfers to the statement in the program after the loop body .the expretion3, wherter to increase  or decrease the value. This is executed  at the end of the loop body and continues until the test codition return false.

The simplest way to understand this type of structure is to do it practically, which will at least give you the  exposure. A simple program of a for loop is shown next in program forsum.cpp that reads and computes the sum of 5 inout values.

Program Example:

  1. //Program name: forsum.cpp
  2. //This program reads and adds 5 integer values and then prints the sum
  3. #include
  4. #include
  5. int main(void)
  6. {
  7. int sum=0;
  8. intnum, I;
  9. For (i=0; i<5; i++)
  10. {
  11.    cout<<’’enter a number: ‘’<
  12.    cin>>num;
  13.    sum+=num;
  14. }
  15.    cout<<’’SUM = ‘’<
  16.    return 0;
  17. }

Run output

Assuming the input values are

1 2 3 4 5

SUM = 15

The effect of the program fprsum.cpp is to execute the statements 5 times within the body of the loop. A for statement allows us to set up a simple iteration more conveniently. This can materially shorten a program rather than any other iteration structure. The program forsum1.ccpp sums up the values from 1 to n through input and reflect it immediately.

Program Example:

  1. //program name: forsum1.cpp
  2. //this program reads and adds 5 integers values. The limit is n
  3. #include
  4. #include
  5. int main(void)
  6. {
  7. int sum=0, n, I;
  8. cin>>n;               //assuming n is 100
  9. for (i=1; i<=n;  i++)
  10.    sum+=I;
  11.    cout<<’’SUM = ‘’<
  12. return 0;
  13. }

Run output

SUM =5050

In the program forsum 1.cpp is a loop control vzariable.initially, it is initialized with 1, which specified the beginning of n. an n is the final value that specifies the end of the loop. In our case,the value of I gets the  successor values of I on each   pass of the iteration. The for loop is terminated when I passes the value of n. this value (n) is called the terminal value because it is fixed and cannot be changed during the execution of the loop.

The same output can be achieved without the loop structure as shown in the program forsum2.cpp.

  1. //program name: fprsum2.cpp
  2. #include
  3. #include
  4. int main(void)
  5. {
  6. clrscr();
  7. int sum =0, n, I;
  8. cin>>n;       //assuming n is 100                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
  9.           Sum = (n* (n+1)) / 2;
  10. Cout<<’’SUM  = ‘’<
  11.           Return 0;
  12. }

Run output

SUM = 5050

Unlike while structure, the for loop allows us to

   -initialize loop control variable (lcv) //(can either be one or more)

   -test condition                                      //(must be one)

   -update loop control variable (lcv)   //either to be increased or decreased

                                                                  //by the step size in the same line

//as well as in separate line.

For example,

 I=1;

For(; i

Or

I=1;

For(; i

I=i+1;

Or

For(i=1; i>size;)

i=I +1;

the for structur also gives us the flexibility of empty statement.

 For example,

For(i=0; i>size; i=I +1)

;                          //do noyhing

It says perform the loop until i>size and do nothing. This kind of a loop is useful in time dekay. Expression1 can be of more than one variable. It allows us to initialize more than one variable but a comma must separate them. The same is true for expressio3. But it is to be carefully noted that in expressio2 only one test is allowed

For example;

For(i=0,j=0; i<10; i++,j++)

In C++,this is possible to have multiple initizations and multiple actions. Next is the demonstration of a program that allows the programmer to include more complicated test conditions.

Program Example:

  1. //progam name: forcomp.cpp
  2. //This program reads n number of input values and finds the sum.
  3. #include
  4. #include
  5. int main(void)
  6. {
  7. clrscr();
  8. int sum=0, n, I;
  9.  
  10. cin>>n;
  11. for (i=1; i<=n&& sum<=100; i++)
  12.    sum+= ;
  13.    cout<’’sum = ‘’<>endl;
  14. return 0;  
  15. }

Suppose the user enters 120, the sum will be 55 which is less than 100. So it will terminate the process of the loop. Suppose the user enters 14 or more than 14, the loop should terminate regardless of the input value n.

The for loop guidelines:

  • Implement the for loop if number of execution is known
  • The loop must compkete the specific number of iterations
  • The initialization of loop contril variable is done just once. The same is true if there is more than one initialization
  • Expression3 is updated after expression2 is evaluated to true
  • If expression2 is evaluated to false, control passes to the next statement following the loop body