Link to home
Start Free TrialLog in
Avatar of dawber39
dawber39Flag for United States of America

asked on

Do-Loop

How can I change this code - to a (Do-Loop) statement?
Dim rates() As Decimal = {6.5D, 8.3D, 4, 2, 10.5D}
        For Each rate As Decimal In rates
            ratesLabel.Text = ratesLabel.Text _
            & rate.ToString("N1") _
            & ControlChars.NewLine
        Next rate
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dawber39

ASKER

I actually have to write it three different ways for an exercise - and I am stuck on the do loop. I did the for, and the for each, now I am stuck.
homework... well, that's what I guessed :-)

let me give you a hint:
do loop  is until a condition, yes?
so, assuming you keep the array, the loop has to START at the first item, and loop until the last item in the array.
that should be enough to find the code.
post your findings here, so we can help if there is some issue...



well - not really homework - but I will except your definition. This is what I have  - and it does not work.

Dim rates() As Decimal = {6.5D, 8.3D, 4, 2, 10.5D}
        Dim counter As Integer
        Dim rate As String
        Do Until counter = rates.Length
            rate = rates(counter)
            ratesLabel.Text = ratesLabel.Text _
            & rate.ToString("N1") _
            & ControlChars.NewLine
        Loop
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It still does not work -  I was doing some much more complicated than this - this little one is racking my brain.
Dim rates() As Decimal = {6.5D, 8.3D, 4, 2, 10.5D}
        Dim counter As Integer
        Dim rate As String
        Do Until counter = rates.Length
            rate = rates(counter)
            ratesLabel.Text = ratesLabel.Text _
            & rate.ToString("N1") _
            & ControlChars.NewLine
            counter = +1
        Loop
did you initialize the counter variable correctly?
also, do you increment the counter variable correctly (do a debug and watch the value of counter) when running through the loop
I didn't increment the counter correctly -:
Dim rates() As Decimal = {6.5D, 8.3D, 4, 2, 10.5D}
Dim counter As Integer
Dim rate As Decimal
Do Until counter = rates.Length
rate = rates(counter)
ratesLabel.Text = ratesLabel.Text _
& rate.ToString("N1") _
& ControlChars.NewLine
counter = counter + 1
Loop
This one works - thanks for the direction
Avatar of juniorDev
juniorDev

You also had
counter =+1
This is not correct.  It should be
counter +=1 : This is the same thing as counter = counter + 1
This also works for:
counter *= variable
counter \= variable
counter -= variable
Thank you - I also realized that I could do away with the rate variable and just work with (rates & counter) and insert this:

rates(counter).ToString("N1")

I wasn't aware that - counter +=1 is the same thing as counter = counter + 1 - that is so cool


Thank you for your help