Jan 31, 2016
FOR LOOPFOR LOOP
WHILE LOOPWHILE LOOP
DO-WHILE LOOPDO-WHILE LOOP
THE FOR LOOPTHE FOR LOOPThe for loop is the easiest to The for loop is the easiest to understand of the C++ loops. All understand of the C++ loops. All its loop control elements are its loop control elements are gathered in one place (on the top gathered in one place (on the top of the loop), while in the other of the loop), while in the other loop construction of C++,they loop construction of C++,they (top control elements) are (top control elements) are scattered about the program. scattered about the program. The general form (syntax) of the The general form (syntax) of the for loop statement isfor loop statement is
for (initialization for (initialization expression(s); test expression(s); test expression;expression; update update
expression(s) )expression(s) )
body- of –the- loop;body- of –the- loop;The following examples The following examples illustrates the use of illustrates the use of for for
statementstatement
Program using loop to print Program using loop to print numbers from 1 to10numbers from 1 to10
#include<iostream.h>#include<iostream.h>
int main()int main()
{{
int i; //declaring variable iint i; //declaring variable i
for (i=1;i<=10;i++)for (i=1;i<=10;i++)
cout<<i;cout<<i;
return 0;return 0;
}}
OutputOutput
1234567891012345678910
The following lines The following lines explain the working of explain the working of the above given the above given for for looploop
cout<<i;cout<<i;
Body of loopBody of loop
Initialization Initialization expressionexpression
for (i=1;for (i=1;
Test Test
expressionexpression
i<=10;i<=10;
Update Update expressionexpression
i++)i++)
1.1. Firstly initialization expression is executed .i.e i=1 which Firstly initialization expression is executed .i.e i=1 which gives the first value 1 to variable i.gives the first value 1 to variable i.
2.2. Then the test expression is evaluated. i.e. , i<=10 which Then the test expression is evaluated. i.e. , i<=10 which results into true i.e.1 .results into true i.e.1 .
3.3. Since the test expression is true , the body of loop i.e. Since the test expression is true , the body of loop i.e.
cout<<i is executed which prints the current value of i .cout<<i is executed which prints the current value of i .
4.4. After executing the loop body the update expression i.e. i+After executing the loop body the update expression i.e. i++ is executed which increments the value of i.+ is executed which increments the value of i.
5.5. After the update expression is executed the test expression After the update expression is executed the test expression is again evaluated. If it is true the sequence is repeated is again evaluated. If it is true the sequence is repeated from step no. 3,otherwise the loop terminatesfrom step no. 3,otherwise the loop terminates
Following figure outlines the working of a Following figure outlines the working of a ForFor loop: loop:
Initialization expression
Test Expression
Body of loop
Update expression
falseexit
true
The while LoopThe while Loop
The second loop available in The second loop available in C++ is the While loop. The C++ is the While loop. The while loop is an entry-while loop is an entry-controlled loop . The syntax controlled loop . The syntax of a while loop isof a while loop is
While (expression)While (expression)
loop-bodyloop-body
Program to calculate the Program to calculate the factorial of an integerfactorial of an integer
#include<iostream.h>int main(){Unsigned long i, num, fact=1; cout<<“Enter Integer:”;cin>>num; //num gets its 1st value.while(num){fact=fact*num ;--num;}Cout” the factorial of”<<i<<“is”<<fact<<“\n”;getch()}
Output of the above program.Output of the above program.
Enter Integer :10Enter Integer :10
The factorial of 10 is 3628800The factorial of 10 is 3628800
The do-while loopThe do-while loopUnlike the for and while loops, the do-while is an exit Unlike the for and while loops, the do-while is an exit
–controlled loop i.e., it evaluates its test expression –controlled loop i.e., it evaluates its test expression at the bottom of the loop after executing its loop-at the bottom of the loop after executing its loop-body statements. This means that a do-while loop body statements. This means that a do-while loop always executes at least once.always executes at least once.
In the other two loops for and while, the test -In the other two loops for and while, the test -expression is evaluated at the beginning of the loop expression is evaluated at the beginning of the loop i.e., before the executing the loop –body. If the test i.e., before the executing the loop –body. If the test -expressions evaluates to false the first time -expressions evaluates to false the first time itself,the loop is never executed .But in some itself,the loop is never executed .But in some situation , it is wanted that the loop-body is situation , it is wanted that the loop-body is executed at least once, no matter what the intial executed at least once, no matter what the intial state of the test-expression is .In such cases, the state of the test-expression is .In such cases, the do-while loop is the obvious choice . The syntax of do-while loop is the obvious choice . The syntax of the do-while loop isthe do-while loop is
DoDo
{{
Statement;Statement;
}while (test-expression);}while (test-expression);
The braces { } are not necessary when The braces { } are not necessary when the loop body contains single the loop body contains single statement.The following statement.The following do-whiledo-while loop prints all upper-case letters:loop prints all upper-case letters:
char ch = ‘A’;char ch = ‘A’;dodo{{cout<<“\n”<<ch;cout<<“\n”<<ch;ch++ch++}while (ch<= ‘Z’ );}while (ch<= ‘Z’ );
The above code prints characters The above code prints characters from ‘A’ onwards until the from ‘A’ onwards until the condition ch<=‘Z’ becomes condition ch<=‘Z’ becomes false.false.