140 likes | 225 Views
In the Name of Allah The Most Merciful The Most Compassionate LOOPS http://vustudents.ning.com. LOOP. A statement or a set of statement that is executed repeatedly is called a loop.There are three kinds of Loops in C++ for loop while loop do-while loop. for Loop.
E N D
In the Name of Allah The Most Merciful The Most CompassionateLOOPShttp://vustudents.ning.com
LOOP • A statement or a set of statement that is executed repeatedly is called a loop.There are three kinds of Loops in C++ for loop while loop do-while loop
for Loop • The for loop statement is used to execute a set of statements repeatedly for a fixed number of times. It is also know as “counter loop”. It has the following parts • Initialization • Condition/Test expression • Increment /Decrement • Body of the Loop The syntax is: 1) for ( initialization; condition; inc/dec ) { Body of the loop }
Initialization: The value of variable(s) of assigned when control enters into the loop first time.It is optional.if it is to be omitted then a semicolon is used in its place. • Condition: The body of the loop executes as long as this condition remains true. • Increment/Decrement: the value of the variable(s) is incremented or decremented. For more than one variable they are separated by commas. This part is executed after executing the body of the loop..Its use is also optional. If it is not used then the control variable must be inc/dec inside the body of the loop. • Body of the loop: If more than 1 statement are to be executed these are enclosed in braces.
No Semi colon • for (int a=2;a<=16; a=a+2) cout<<“\t”<<a; Output: 2 4 6 8 10 12 14 16 • int x=2; for (;x<=16; ) { cout<<“\t”<<x; x=x+2; } Output: 2 4 6 8 10 12 14 16
FLOWCHART Initialization FALSE condition Exit TRUE Body of the Loop Inc/Dec
while Loop • It is a conditional loop. It is used to execute a statement or a set of statements as long as the given condition remains true. • The Syntax is while(condition) { statement(s); } If the loop never ends (the condition is never false) ,it is said to be an infinite loop
FLOWCHART FALSE condition Exit TRUE Body of the Loop
int n=2; while(n<10) { cout<<“\t”<<n; n=n+2; } Output: 2 4 6 8 • NOTE: while(0) is always a false loop while(2) is an infinite loop
do-while Loop • It is also conditional loop. It is similar to the while loop but the condition is tested after executing the statements of the loop • The Syntax is do { statement(s); } while (condition); It is useful when the body of the loop is to be executed at least once.
FLOWCHART Body of the Loop Exit condition TRUE FALSE
# include<iostream.h> void main() { int x=2; do { cout<<“\t”<<x; x=x+2; }while(x<10); } • Output?? 2 4 6 8 Semicolon
The continue statement • It shifts the control back to the beginning of the loop.It skips the remaining statements within the loop body. Start of loop continue condition
//Factorial Program # include<iostream.h> void main() { long int fact,n,c; cout<<“\nEnter a number: ”; cin>>n; fact =1; while(n>=1) { fact = fact*n ; n-- ; } cout<<“\nFactorial= ”<<fact; } http://vustudents.ning.com