Looping in c++

13
Made By :- Made By :- Deeksha Gopaliya Deeksha Gopaliya 11 11 th th Science Science COMPUTER COMPUTER PROJECT PROJECT

Transcript of Looping in c++

Page 1: Looping in c++

Made By :-Made By :-Deeksha GopaliyaDeeksha Gopaliya

1111thth Science Science

COMPUTERCOMPUTER PROJECTPROJECT

Page 2: Looping in c++
Page 3: Looping in c++

L O O P I N G

EXIT

Page 4: Looping in c++

LOOPSLOOPS

FORFOR LOOPLOOPFORFOR LOOPLOOP DODO WHILEWHILELOOPLOOP

DODO WHILEWHILELOOPLOOP

WHILEWHILE LOOPLOOP

WHILEWHILE LOOPLOOP

Page 5: Looping in c++

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.

Page 6: Looping in c++

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

Page 7: Looping in c++

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

Page 8: Looping in c++

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++; }

Page 9: Looping in c++

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)

}

Page 10: Looping in c++

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.

Page 11: Looping in c++
Page 12: Looping in c++

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

Page 13: Looping in c++

POWERPOINTPOWERPOINT PRESENTATION PRESENTATION

ON ON LOOPINGLOOPING IN C++ IN C++

Made By:-Made By:-Deeksha Gopaliya Deeksha Gopaliya