Download presentation
Presentation is loading. Please wait.
Published byJob Murphy Modified over 8 years ago
1
LOOPS http://vustudents.ning.com In the Name of Allah The Most Merciful The Most Compassionate LOOPS http://vustudents.ning.com
2
LOOP n 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
3
for Loop n 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 n Initialization n Condition/Test expression n Increment /Decrement n Body of the Loop The syntax is: 1)for ( initialization; condition; inc/dec ) { Body of the loop }
4
n 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. n Condition: The body of the loop executes as long as this condition remains true. n 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. n Body of the loop: If more than 1 statement are to be executed these are enclosed in braces.
5
n for (int a=2;a<=16; a=a+2) cout<<“\t”<<a; Output: 246810121416 n int x=2; for (;x<=16; ) { cout<<“\t”<<x; x=x+2; } Output: 246810121416 No Semi colon
6
condition TRUE FALSEFLOWCHART Body of the Loop Inc/Dec Exit Initialization
7
while Loop n 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. n 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
8
condition TRUE FALSE FLOWCHART Body of the Loop Exit
9
n int n=2; while(n<10) { cout<<“\t”<<n; n=n+2; } Output: 2468 n NOTE: while(0) is always a false loop while(2) is an infinite loop
10
do-while Loop n It is also conditional loop. It is similar to the while loop but the condition is tested after executing the statements of the loop n The Syntax is do { statement(s); } while (condition); It is useful when the body of the loop is to be executed at least once.
11
condition TRUEFALSE FLOWCHART Body of the Loop Exit
12
# include void main() { int x=2; do { cout<<“\t”<<x; x=x+2; }while(x<10); } n Output?? 2468 Semicolon
13
The continue statement n It shifts the control back to the beginning of the loop.It skips the remaining statements within the loop body. continue Start of loop condition
14
//Factorial Program # include 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.