While

202 views
181 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
202
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

While

  1. 1. Loops of various sorts are used torepeat a set of statements somenumber of times.
  2. 2. Print numbers from 0 to 1000
  3. 3. int count_down=3;while (count_down > 0) { cout << "Hello "; count_down -= 1; }
  4. 4. int count_down=3;while (count_down > 0) { cout << "Hello "; //count_down -= 1; }What happens now?
  5. 5. int x = 10;while ( x > 0){ cout << x << endl; x = x – 3; }
  6. 6. int x = 10;while (x > 0){ cout << x << endl; x = x – 3;}Output using the comparison x < 0 instead of x > 0?
  7. 7. What happens here?int x = 1;while (x != 12){ cout << x << endl; x = x + 2;}
  8. 8. Print the odd numbers less than 12int x = 1;while (x != 12){ cout << x << endl; x = x + 2;}How to fix it?
  9. 9. While loopsWhats wrong with this? int x = 10; while ( x > 0 ); { cout << x << endl; x--; }
  10. 10. While-loop and for-loop
  11. 11. int x = 1;while (x < 12){ cout<<x<<endl; x = x + 2;}
  12. 12. The for loopfor (initialization; expression; increment){ //statements here}Example: flowchart
  13. 13. int x = 1;while (x < 12){ cout<<x<<endl; x = x + 2;}
  14. 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. 15. int i ;for( i=0; i<10; ++i ){ cout << i << " ";}
  16. 16. int i;for( i=0; i<10; ++i ){ cout << i << " ";}int i;for (i=0; i<10; i++){ cout <<i << " ";}
  17. 17. int x, n=100;for (x=0; x<n; x++){ x=x+1;}cout<<"x after end of loop is "<<x<<endl;
  18. 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. 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. 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. 21. The For Loopint i ;for( i=23; i>=30; ++i ){ cout << i << " ";}for (i=13; i>=10; --i){ cout <<i <<" ";}
  22. 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. 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 …

×