Your SlideShare is downloading. ×
While
While
While
While
While
While
While
While
While
While
While
While
While
While
While
While
While
While
While
While
While
While
While
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
130
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Loops of various sorts are used torepeat a set of statements somenumber of times.
  • 2. Print numbers from 0 to 1000
  • 3. int count_down=3;while (count_down > 0) { cout << "Hello "; count_down -= 1; }
  • 4. int count_down=3;while (count_down > 0) { cout << "Hello "; //count_down -= 1; }What happens now?
  • 5. int x = 10;while ( x > 0){ cout << x << endl; x = x – 3; }
  • 6. int x = 10;while (x > 0){ cout << x << endl; x = x – 3;}Output using the comparison x < 0 instead of x > 0?
  • 7. What happens here?int x = 1;while (x != 12){ cout << x << endl; x = x + 2;}
  • 8. Print the odd numbers less than 12int x = 1;while (x != 12){ cout << x << endl; x = x + 2;}How to fix it?
  • 9. While loopsWhats wrong with this? int x = 10; while ( x > 0 ); { cout << x << endl; x--; }
  • 10. While-loop and for-loop
  • 11. int x = 1;while (x < 12){ cout<<x<<endl; x = x + 2;}
  • 12. The for loopfor (initialization; expression; increment){ //statements here}Example: flowchart
  • 13. int x = 1;while (x < 12){ cout<<x<<endl; x = x + 2;}
  • 14. int x = 1; for (x=1; x<12; x=x+2)while (x < 12) {{ cout<<x<<endl; cout<<x<<endl; } x = x + 2;}
  • 15. int i ;for( i=0; i<10; ++i ){ cout << i << " ";}
  • 16. int i;for( i=0; i<10; ++i ){ cout << i << " ";}int i;for (i=0; i<10; i++){ cout <<i << " ";}
  • 17. int x, n=100;for (x=0; x<n; x++){ x=x+1;}cout<<"x after end of loop is "<<x<<endl;
  • 18. int x, n=100;for (x=0; x<n; x++){ x=x+1;}cout<<"x after end of loop is "<<x<<endl;x is the smallest even number >= n
  • 19. SELF TESTint x, n=100;for (x=0; x<n; x++){ cout<<x<<endl; x=x+1;}cout<<"x after end of loop is "<<x<<endl;
  • 20. SELF TESTint x, n=100;for (x=0; x<n; x++){ x=x+1; cout<<x<<endl;}cout<<"x after end of loop is "<<x<<endl;
  • 21. The For Loopint i ;for( i=23; i>=30; ++i ){ cout << i << " ";}for (i=13; i>=10; --i){ cout <<i <<" ";}
  • 22. The For LoopWhat is wrong here?int i, j, k ;for( i=0; i<10; i-- ){ cout<<i;}for (i=0; i<10; ){ j=i+30; k=j+30;}
  • 23. Fibonacci numbersF1=1F2=2Fn=Fn-1 + Fn-21 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 …

×