user2941166
user2941166

Reputation: 1

Converting Do While Looping to For Next Looping

I needed help converting this code type from Do Loop While and Do While Loop to For...Next looping. I am using Visual Basic 2010 Express as the program to run these codes and would greatly appreciate if anyone could help me. Below is the code that needs to be converted to the For...Next looping type. Once again thank you to anyone who can help.

Private Sub btnDo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDo.Click  
   Dim intNum, intSum As IntegerDo  
   intSum += intNum    ‘accumulator/running total  
   intNum += 2        ‘update intNum   
   Loop While (intNum <= 50)  
       Me.lblDoAnswer.Text = intSum.ToString  
End Sub

Private Sub btnDoWhile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)      Handles btnDoWhile.Click Dim intNum, intSum As Integer    
   Do While (intNum <= 50)  
       intSum += intNum    ‘accumulator/running total  
       intNum += 2        ‘update intNum  
   Loop  
   Me.lblDoWhileAnswer.Text = intSum.ToString  
End Sub

Upvotes: 0

Views: 1255

Answers (1)

Karl Anderson
Karl Anderson

Reputation: 34846

Try this:

For intNum As Integer = 0 To 50 Step 2
    intSum += intNum    ‘accumulator/running total
Next

Note: Step 2 is the equivalent of the intNum += 2 syntax you had in the Do loop.

Upvotes: 4

Related Questions