Download - Looping in c++
Made By :-Made By :-Deeksha GopaliyaDeeksha Gopaliya
1111thth Science Science
COMPUTERCOMPUTER PROJECTPROJECT
L O O P I N G
EXIT
LOOPSLOOPS
FORFOR LOOPLOOPFORFOR LOOPLOOP DODO WHILEWHILELOOPLOOP
DODO WHILEWHILELOOPLOOP
WHILEWHILE LOOPLOOP
WHILEWHILE LOOPLOOP
PARTS OF A LOOP
1. Initialization Expression - Before entering in a loop , its control variables must be initialized. The initializations expression gives the loop variables their first value.
2. Test Expression - The test expression is the expression whose truth value decides whether the loop body will be executed or not.
3. Update Expression - The update expression change the values of loop variables. The update expression is executed ; at the end of the loop after loop body is executed.
4. The body of the Loop – The statements that are executed repeatedly as long as the condition is true ,form the body of loop.
FOR LOOPFOR LOOPFor Loop executes a sequence of statements
multiple t imeand abbreviates the code that manages the loop
variable .Syntax: for ( initial ization; condition; update
expression) Body of the loop;Example: #include<iostream.h> output: void main( ) 1 { 2 for ( int i=0; i<=5; i++) 3 cout<<“\n”<<i; 4 } 5
Functioning oF For Loop in c++
For loop is used when we don’t know in advance how many times the loop will times the loop will be executed .
The loop terminates as soon as the condition becomes false.
EXIT
WHILEWHILE LOOPLOOPWhile loop While loop i s an entry-control led loop. It repeats a statement or
group of statements while a condition is true. It tests the condition before executing the loop.
Syntax: init ial ization; while (condit ion) loop=body; updating expression;
Example : #include<iostream.h> Output: void main( ) 1 { 2 int i=1 3 while ( i<=5) 4 cout<<“\n”<<i; 5 i++; }
DO WHILE LOOPDO WHILE LOOPDo while LoopDo while Loop is a exit-control led loop. It evaluates its text expression at the
bottom of the loop after executing the loop body statements.
Syntax: initial ization;
do
{
loop body;
updating express ion;
}while (condition) ;
Example : #include<iostream.h. Output:
void main( ) 1
{ int i=1 2
do 3
{ 4
cout<<“\n”<<i; 5
i++; 6
}while ( i<=5)
}
Comparison between while loop and do while loop
While loop test condition before executing the loop body whereas do while loop test condition after executing loop body.
While loop is a entry-controlled loop whereas do while loop is a exit-controlled loop.
While loop is preferred when we don’t want to execute the loop body even once in case the condition is false whereas do while loop is preferred when we want to execute the loop body at least once.
Funct ion ing o f Nes t ed Loop
Example:#include<iostream.h>void main( ){for( int i=1; i<=3; i++){for( int j=1; j<=i; j++) {cout<<i;}cout<<“\n”;} }
Inner
Loop
Outer
Loop
Output:
11
1212
123123
POWERPOINTPOWERPOINT PRESENTATION PRESENTATION
ON ON LOOPINGLOOPING IN C++ IN C++
Made By:-Made By:-Deeksha Gopaliya Deeksha Gopaliya