Wannabe
Wannabe

Reputation: 503

Do..While...Loop Result

What should the results be of the following pseudocode:

Initialize counter to 10

Do while counter < 100

Display counter multiplied by 2

Add 10 to the counter

End loop

I'm thinking: 20, 60, 140

This is my code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim multiplied As Integer
    Dim counter As Integer = 10
    Do While counter < 100
        multiplied = counter * 2
        Label1.Text = Label1.Text & ControlChars.NewLine & multiplied.ToString
        counter = multiplied + 10
    Loop
End Sub

Thanks guys!!

Upvotes: 2

Views: 2504

Answers (1)

Joel Coehoorn
Joel Coehoorn

Reputation: 416179

Display counter multiplied by 2

Nothing in that instruction says to modify the counter. Based on a strict interpretation of your instructions, the output should look like this:

20 40 60 80 100 120 140 160 180

Your code, however, matches the results you expect. If you want code that matches your instructions, do it like this:

Dim counter As Integer = 0
Dim result As New StringBuilder()
Dim delimiter As String = ""
Do While counter < 100
   result.Append(delimiter).Append( (counter*2).ToString() )
   delimiter = Environment.NewLine
   counter += 10
Loop
Label1.Text = result.ToString()

And for fun we could do something like this:

Label1.Text = Enumerable.Range(1, 9)
        .Select(Function(i) i * 10)
        .Aggregate("", Function(s, i) s = s & i.ToString() & ",")

Upvotes: 4

Related Questions