17
Revision Exercises on “Loops”

Revision exercises on loop

Embed Size (px)

DESCRIPTION

loop revision

Citation preview

Page 1: Revision exercises on loop

Revision Exercises on “Loops”

Page 2: Revision exercises on loop

while loop for loop do while Vs while loops

Agenda

Page 3: Revision exercises on loop

while loop

Page 4: Revision exercises on loop

int i = 0;while (i < 101){// Processing : i++; // at the end}

while loop

Page 5: Revision exercises on loop

Write a while loop to solve the following problem

Annie has $0 at the start, she saves $300 per month. What will she save after 24 months?

Exercise 1

Page 6: Revision exercises on loop

How many variables we need?1. saving (start from 0)2. month (start from 1)3. savingPerMth = 300

Problem solving: variables?

Page 7: Revision exercises on loop

int saving = 0;int month = 1;int savingPerMth = 300;while (month <= 24){ saving = saving + savingPerMth; // saving += savingPerMth

month++;}Console.WriteLine(“Total saving = ” + saving.ToString() );

Possible solution

Page 8: Revision exercises on loop

Write a while loop to solve the following problem

John has $300 at the start, she saves $100 per month.

For every 6 months after he gets his bonuses, he saves extra $500.

What will he save after 12 months?

Exercise 2

Page 9: Revision exercises on loop

int saving = 300;int month = 1;int savingPerMth = 100;int bonus = 500;while (month <= 12){ saving += savingPerMth; // saving += savingPerMth

if ( (month%6) == 0) { saving += bonus; } month++;}Console.WriteLine(“Total saving = ” + saving.ToString() );

Possible solution

Page 10: Revision exercises on loop

for (int i=0; i < 101; i++ ) { // Processing}

for loop

Page 11: Revision exercises on loop

Write a while loop to solve the following problem

Annie has $0 at the start, she saves $300 per month. What will she save after 24 months?

Exercise 1 using for loop

Page 12: Revision exercises on loop

int saving = 0;int savingPerMth = 300; for (int month=1; month<=24; month++){ saving += savingPerMth; }

Console.WriteLine(“Total saving = ” + saving.ToString() );

Possible solution

Page 13: Revision exercises on loop

Write a while loop to solve the following problem

John has $300 at the start, she saves $100 per month.

For every 6 months after he gets his bonuses, he saves extra $500.

What will he save after 12 months?

Exercise 2 using for loop

Page 14: Revision exercises on loop

int saving = 300;int savingPerMth = 100;int bonus = 500;

for (int month=1; month<=12; month++){ saving += savingPerMth; if ( (month%6) == 0) { saving += bonus; }}

Console.WriteLine(“Total saving = ” + saving.ToString() );

Possible solution

Page 15: Revision exercises on loop

a) write a for loop to output 0 to 10

b) write a for loop to output 10, 8, 6, 4, 2 and 0

Exercise 3 using for loop

Page 16: Revision exercises on loop

a) for (int x = 0; x <=10; x++) { Console.WriteLine( x.ToString() ); }b) for (int x = 10; x >=0; x = x -2) { Console.WriteLine( x.ToString() ); }

Possible solution

Page 17: Revision exercises on loop

do loop

do{

} while ( ); // End condition

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