Presentation is loading. Please wait.

Presentation is loading. Please wait.

Repetition Structures

Similar presentations


Presentation on theme: "Repetition Structures"— Presentation transcript:

1 Repetition Structures
Do...Loop Statement

2 Do...Loop Statement Do [{While | Until} condition] [statements]
[Exit Do] Loop Any number of Exit Do statements may be placed anywhere in the Do…Loop as an alternate way to exit a Do…Loop. Exit Do is often used after evaluating some condition, for example, If…Then, in which case the Exit Do statement transfers control to the statement immediately following the Loop.

3 Do...Loop Example Private Sub cmdDisplay_Click() Dim num As Integer
'Display the numbers from 1 to 10 num = 1 Do While num <= 10 picNumbers.Print num; num = num + 1 Loop End Sub

4 Do...Loop Example Private Sub cmdDisplay_Click()
Dim x As Integer, y As Integer x = 1 y = x * x Do While y <= 100 picOutput.Print y; x = x + 1 Loop End Sub Displays all perfect squares between 1 and 100

5 Private Sub cmdYears_Click()
Dim balance As Single, numYears As Integer 'Compute years required to become a millionaire picWhen.Cls balance = Val(txtAmount.Text) numYears = 0 Do While balance < balance = balance * balance numYears = numYears + 1 Loop picWhen.Print "In"; numYears; "years you will have a _ million dollars." End Sub

6 Do...Loop Statement Do [statements] [Exit Do]
Loop [{While | Until} condition]

7 Do...Loop Example Private Sub cmdEstimate_Click()
Dim amt As Single, yrs As Integer amt = 15000 yrs = 0 Do amt = amt * yrs = yrs + 1 Loop Until amt <= 0 picResult.Print "It takes"; yrs; "years to _ deplete the account." End Sub Subroutine calculates the years it takes to deplete a savings account with an initial balance of $15,000, paying 5% interest, and withdrawing $1000 annually.

8 Nested Do...Loop Example Do Do While Counter < 20
Counter = Counter + 1 If Counter = 10 Then Check = False Exit Do End If Loop Loop Until Check = False This example shows how Do...Loop statements can be nested. The inner Do...Loop statement loops 10 times, sets the value of the flag to False, and exits prematurely using the Exit Do statement. The outer loop exits immediately upon checking the value of the flag.


Download ppt "Repetition Structures"

Similar presentations


Ads by Google