Loops in c++ programming language

mali49558 7,726 views 15 slides Dec 19, 2019
Slide 1
Slide 1 of 15
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15

About This Presentation

In this presentation slides you will able to understand easily ,this slides contain loops of c++ programming language which contain for loop , while loop , do while loop and nested these all are describe with definition,examples and flow charts


Slide Content

LOOP IN C++: The mechanism through which a statement or set of statement can be executed repeatedly is called loop . The statements that are executed repeatedly are called body of loop. The body of loop can be executed repeatedly for a specified number of times or until the given condition remains true . In c++ there are four types of loop as; 1) For loop 2) while loop 3) Do while loop 4) Nested loop @ Muhammad Ali

For loop The “for” loop is used to execute a statement or set of statements repeatedly for a specific numbers of times This loop is also known as counter loop. Syntax of for loop For{ initialization;condition;increment /decrement} { } Statements; Next statement; @Muhammad Ali

The “ for”loop is executed as follows ; Working of For loop When control enters into the loop 1)First initialization part is executed . It is executed only for the first time 2)After executing initialization part , the condition is evaluated.if it is True,then body of loop is executed. 3)After executing body of loop, increament / decreament part is executed increament / decreament part is last statement of body of “for” loop. . @Muhammad Ali

Our Services Insert the title of your subtitle Here Flow chart of for loop intialization condition Body of loop Increament / decreament Next statement True False @Muhammad Ali

Example of for loop #include < iostream.h > #include< conio.h > Main(); { Int n; For (n=1;n<=10;n++) cout <<n<< endl ; Cout <<“ok”; } @Muhammad Ali

While loop The “while loop ” is conditional loop structure it is used to Execute a statement or set of statement as long as the given condtioin remain true This loop structure is used when the programmer does not know in advance Syntax of while loop; While (condition) { statement; } Next – statement; @Muhammad Ali

The “ for”loop is executed as follows; When a “while ”loop statement is executed the condition is evaluate first If the condition is true then body of loop is executed After executing the body of loop, the execution control goes back to the “ while”statement and evaluates the condition again. If the given condition is still true , the body of loop is executed again. This process is repeated. When the given condition become false at any stage during execution ,the loop is terminated. @Muhammad Ali

Our Services Insert the title of your subtitle Here Flow chart of ” while”loop condition Body of loop Next statement False True @Muhammad Ali

E xample of “while ” loop #include< iostream.h > #include< conio.h > Main() { Int num ; Num =1; While( num <=15) { If (num%3==0) Cout << num << endl ; Num =num+1; } } @Muhammad Ali

DO WHILE LOOP ; as while loop ,do while loop is also used to execute a statement or set of statement as long as the given condition remains true The condition comes after the body of loop The condition is evaluate after executing the body of loop The body of loop must be executed at least once even if The condition is false Semicolon(;) is given after the while (condition) Syntax of Do while loop; Initialize Do { Body of loop Updation } While (condition) @Muhammad Ali

Flow chart of ”Do while”loop condition Body of loop Next statement False True @Muhammad Ali

E xample of “Do while ” loop #include< iostream.h > #include< conio.h > Main() { Int i =1; Do { Cout << i <<“/n”; i ++; } While (1<=10); } @Muhammad Ali

Nested loop; A  nested loop  is a  loop  within a  loop , an inner  loop   within the body of an outer one . How this works is that the first pass of the outer  loop  triggers the inner  loop , which executes to completion. Then the second pass of the outer  loop  triggers the inner  loop  again . This repeats until the outer  loop   finishes Syntax of nested loop ; Outer loop { statement (s)-1 Inner-loop { Body of inner loop } Statement (s)-2 } Next – statement; @Muhammad Ali

The “ nested”loop is executed as follows; The condition given in outer is evaluated.if condition is true Controls enters inside the body of outer loop.otherwise loop is Terminated. If body of outer loop contains any statement before the inner loop, it is executed . @Muhammad Ali

E xample of “nested” loop # include< iostream.h > #include< conio.h > Main() { Int u,i ; U=1; While (u<=3) { Cout <<u<< endl ; For ( i =1;i<=2;i++) Cout <<“I love Pakistan\n”; U=u+1; } } @Muhammad Ali