Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Revision exercises on loop

3,714 views

Published on

loop revision

  • Be the first to comment

  • Be the first to like this

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 />

×