Revision exercises on loop

3,034
-1

Published on

loop revision

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

  • Be the first to like this

No Downloads
Views
Total Views
3,034
On Slideshare
0
From Embeds
0
Number of Embeds
22
Actions
Shares
0
Downloads
52
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Revision exercises on loop

  1. 1. Revision Exercises on “Loops”<br />
  2. 2. while loop<br />for loop<br />do while Vs while loops<br />Agenda<br />
  3. 3. while loop<br />
  4. 4. while loop<br />inti = 0;<br />while (i < 101)<br />{<br />// Processing<br /> :<br />i++; // at the end<br />}<br />
  5. 5. Write a while loop to solve the following problem<br /> Annie has $0 at the start, she saves $300 per month. What will she save after 24 months?<br />Exercise 1<br />
  6. 6. How many variables we need?<br />saving (start from 0)<br />month (start from 1)<br />savingPerMth = 300<br />Problem solving: variables?<br />
  7. 7. int saving = 0;<br />int month = 1;<br />intsavingPerMth = 300;<br />while (month <= 24)<br />{<br /> saving = saving + savingPerMth; // saving += savingPerMth<br /> month++;<br />}<br />Console.WriteLine(“Total saving = ” + saving.ToString() );<br />Possible solution<br />
  8. 8. Write a while loop to solve the following problem<br /> John has $300 at the start, she saves $100 per month. <br /> For every 6 months after he gets his bonuses, he saves extra $500. <br /> What will he save after 12 months?<br />Exercise 2<br />
  9. 9. int saving = 300;<br />int month = 1;<br />intsavingPerMth = 100;<br />int bonus = 500;<br />while (month <= 12)<br />{<br /> saving += savingPerMth; // saving += savingPerMth<br /> if ( (month%6) == 0) { saving += bonus; }<br /> month++;<br />}<br />Console.WriteLine(“Total saving = ” + saving.ToString() );<br />Possible solution<br />
  10. 10. for loop<br />for (inti=0; i < 101; i++ ) <br />{<br /> // Processing<br />}<br />
  11. 11. Write a while loop to solve the following problem<br /> Annie has $0 at the start, she saves $300 per month. What will she save after 24 months?<br />Exercise 1 using for loop<br />
  12. 12. int saving = 0;<br />intsavingPerMth = 300;<br />for (int month=1; month<=24; month++)<br />{ saving += savingPerMth; }<br />Console.WriteLine(“Total saving = ” + saving.ToString() );<br />Possible solution<br />
  13. 13. Write a while loop to solve the following problem<br /> John has $300 at the start, she saves $100 per month. <br /> For every 6 months after he gets his bonuses, he saves extra $500. <br /> What will he save after 12 months?<br />Exercise 2 using for loop<br />
  14. 14. int saving = 300;<br />intsavingPerMth = 100;<br />int bonus = 500;<br />for (int month=1; month<=12; month++)<br />{ <br /> saving += savingPerMth; <br /> if ( (month%6) == 0) { saving += bonus; }<br />}<br />Console.WriteLine(“Total saving = ” + saving.ToString() );<br />Possible solution<br />
  15. 15. a) write a for loop to output 0 to 10<br />b) write a for loop to output 10, 8, 6, 4, 2 and 0<br />Exercise 3 using for loop<br />
  16. 16. a) <br /> for (int x = 0; x <=10; x++)<br /> {<br />Console.WriteLine( x.ToString() );<br /> }<br />b)<br /> for (int x = 10; x >=0; x = x -2)<br /> {<br />Console.WriteLine( x.ToString() );<br /> }<br />Possible solution<br />
  17. 17. do loop<br />do<br />{<br />} while ( ); // End condition<br />// The difference between the "while" and "do while" loops is that the code in a do while loop will get executed at least once, because its while part is at the end. <br />
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×