Presentation is loading. Please wait.

Presentation is loading. Please wait.

Repetition – For and Do While

Similar presentations


Presentation on theme: "Repetition – For and Do While"— Presentation transcript:

1 Repetition – For and Do While
Yonglei Tao

2 Future Balance You put $10,000 into a bank account that earns % interest per year. How much will your account balance be in five years?

3 Calculation After Year Calculation Balance 0 $10, balance = balance + balance * 0.05 $10, balance = balance + balance * 0.05 $11, balance = balance + balance * 0.05 $11, balance = balance + balance * 0.05 $12, balance = balance + balance * 0.05 $12,762.82

4 The For Loop Dim balance, interest As Double balance = For year As Integer = 1 To 5 interest = balance * 0.05 balance = balance + interest Next year lblDisplay.Text = Format (balance, “currency”)

5 The Do While Loop Dim balance, interest As Double Dim year As Integer ‘ declare control variable balance = year = 1 ‘ initialize control varaible Do While year <= 5 ‘ test control variable interest = balance * 0.05 balance = balance + interest year = year + 1 ‘ update control variable Loop lblDisplay.Text = Format (balance, “currency”)

6 A Problem You put $10,000 into a bank account that earns 5% interest per year. How many years does it take for the account balance to be double the original?

7 Calculation ….. After Year Calculation Balance 0 $10,000.00
$10,000.00 balance = balance + balance * $10,500.00 balance = balance + balance * $11,025.00 balance = balance + balance * $11,576.25 balance = balance + balance * $12,155.06 balance = balance + balance * $12,762.82 …..

8 The Do While Loop Dim balance, interest As Double Dim year As Integer = 1 balance = Do While year <= 5 interest = balance * 0.05 balance = balance + interest year = year + 1 Loop lblDisplay.Text = Format (balance, “currency”) balance < 20000

9 Find the Number of Iterations
i = 0.5 Do While i <= 1.3 lblDisplay.Text = lblDisplay.Text & i & “ “ i = i Loop Dim sum As Integer = 0 For i As Integer = 2 To 20 Step 5 sum = sum + i Next i


Download ppt "Repetition – For and Do While"

Similar presentations


Ads by Google