Presentation<br />nested loops<br />By:- <br />Balwinder Singh <br />
     contents<br /><ul><li>What is a Loop?
Nested While loops.
Do…..While loops.
Nested For loops.
Question.</li></li></ul><li>     What is a loop?<br /><ul><li>A loop is a programming structure that contains an executabl...
Good programmers include a way to satisfy the condition somewhere inside the executable block.</li></ul>loops<br />
     Types of nested loops<br /><ul><li>Nested While Loops.
Do…..While Loops.
Nested For Loops.</li></ul>nested loops<br />
      Nested while loops<br /><ul><li>WHILE loops may be nested, that is you can put a WHILE loop inside another WHILE loop.
 However, one must start the inner loop after starting the outer loop and end the inner loop before ending the outer loop ...
      Example:- nested while loops<br />#include<stdio.h><br />#include<conio.h><br />main()<br />{ <br />int r=1,c=1;<br ...
Do……while loop<br /><ul><li>Executes the loop statement once and then repeats the loop statement until the condition is ze...
Upcoming SlideShare
Loading in …5
×

Presentation on nesting of loops

7,480 views

Published on

Published in: Education
  • Be the first to comment

Presentation on nesting of loops

  1. 1. Presentation<br />nested loops<br />By:- <br />Balwinder Singh <br />
  2. 2. contents<br /><ul><li>What is a Loop?
  3. 3. Nested While loops.
  4. 4. Do…..While loops.
  5. 5. Nested For loops.
  6. 6. Question.</li></li></ul><li> What is a loop?<br /><ul><li>A loop is a programming structure that contains an executable block of code that repeats so long as a given condition is true/not true.
  7. 7. Good programmers include a way to satisfy the condition somewhere inside the executable block.</li></ul>loops<br />
  8. 8. Types of nested loops<br /><ul><li>Nested While Loops.
  9. 9. Do…..While Loops.
  10. 10. Nested For Loops.</li></ul>nested loops<br />
  11. 11. Nested while loops<br /><ul><li>WHILE loops may be nested, that is you can put a WHILE loop inside another WHILE loop.
  12. 12. However, one must start the inner loop after starting the outer loop and end the inner loop before ending the outer loop for a logically correct nesting. </li></li></ul><li> Example:- nested while loops<br />To print the following series:-<br />1<br />1 2<br />1 2 3<br />1 2 3 4<br />1 2 3 4 5 <br />
  13. 13. Example:- nested while loops<br />#include<stdio.h><br />#include<conio.h><br />main()<br />{ <br />int r=1,c=1;<br /> while (r<=5)<br /> {<br /> c=1;<br /> while(c<=r)<br /> {<br />printf(“%d”,c);<br />c++;<br /> }<br />printf(“n”);<br /> r++;<br /> }<br />getch();<br />}<br />
  14. 14. Do……while loop<br /><ul><li>Executes the loop statement once and then repeats the loop statement until the condition is zero (false).
  15. 15. Starts with a do statement and then tests the repetition condition in the while statement at the end of the loop.</li></ul> Syntax:- do<br /> {<br /> statements;<br /> }<br /> while(condition);<br />
  16. 16. The while Loop<br />condition<br />evaluated<br />true<br />false<br />statement<br /> Logic of a do….while loops<br />
  17. 17. Example:- do….While loop<br /> Accept any number from the user to print the number is positive or negative.<br />#include<stdio.h><br />#include<conio.h><br />main()<br />{ <br />int a;<br />do<br /> {<br />printf(“Enter any number”);<br />scanf(“%”,&a);<br />if (a>=0)<br />printf(“Number is positive”);<br />else<br />printf(“Number is Negative”);<br /> }<br />while(a!=0);<br />getch();<br />}<br />
  18. 18. Nested for loops<br /><ul><li>A for loop can contain any kind of statement in its body, including another for loop.
  19. 19. The inner loop should have a different name for its loop counter variable so that it will not conflict with the outer loop.</li></li></ul><li> Example:- nested forloops<br />Write a program to print the following series:-<br /> 1<br /> 2 3<br /> 4 5 6<br /> 7 8 9 10<br />
  20. 20. Example:- nested for loops<br />#include<stdio.h><br />#include<conio.h><br />main()<br />{ <br />intr,c,k=1;<br /> for (r=1;r<=4;r++)<br /> {<br /> for (c=1;c<=r; c++)<br /> {<br />printf(“%d”,k);<br /> k++;<br /> }<br />printf(“n”);<br /> }<br />getch();<br />} <br />
  21. 21. anyQuestions???<br />Nested While<br />Do….While<br />Nested For<br />

×