When we write C++ program, there may be situation when we need to execute a set of instruction several number of times. C++ provides various control structure that all for more complicated execution path.
Loop
According to dictionary definition loop is set of instructions that repeat themselves until the certain condition is fulfilled. It is also known as looping statement or loop. It is also known as iterative statement or repetitive structure.
Loops are basically used for two purposes:
  • ·       To execute a statement or number of statements for a specified number of time. For example, a user may display his name or email on screen for 5 times.
  • ·       To use a sequence of values. For example , a user may display a set of natural numbers from 0 to 50.

There are many types of Loops or iteration statement in C++.
  1. For loop
  2. While Loop
  3. Do-while Loop


For Loop definition in C++
Execute a sequence of statement multiple times until the certain condition is fulfilled.For loop is iteration statement. It is also called counter controlled loop. It is the most flexible loop. That is why, it is the most frequently used loop by the c++ programmers.
Syntax of For Loop
For (initialization statement; test expression; update expression)
{
//statement
}
Syntax 2
For(variable initialization; condition; variable increment )

Initialization statement: it specifies the starting value of counter variable. One or many variables can be initialized in this part. To initialize many variables each variable is separated by comma.
Condition or test expression: The condition is given as a relation expression. The statement is executed only if the given condition is true. If the condition is false, the statement is never executed.
Increment or decrement or Update expression: The part of loop specifies the charge in counter variable after each execution of loop. To change many variables, each variable must be separated by commas.
Statement: statement is the instruction that is executed when the condition is true. Statement are given in braces if they are two or more.
{
}

For loop with Example in borland C++


#include<iostream> //header file for cout and cin
#include<conio>
int main ()        //main function
{              
int a;
for(int a=0; a<=10; a++)        //for(For (initialization; test expression; update expression))
{

cout<<"\n"<<a;                    // statement or loop body
}
getch ();     //getcharacter
return 0;
}




Explanation

Working of ‘For’ loop in C++

The number of iteration depends on the initialization, condition and increment/decrement parts. The initialization part is executed only when control enters the loop. After the initialization, the given condition is evaluated. If it is true, the control enters the body of loop and executes all statement in it. Then the increment part is executed that changes the counter variables. The control again moves to condition part. This process is continues while the condition remain true. The loop is terminated when the condition becomes false.


2 Comments

Post a Comment

Previous Post Next Post