在循环时转换为下一个循环

时间:2013-11-01 12:22:00

标签: vb.net coding-style

我需要帮助将此代码类型从Do Loop WhileDo While Loop转换为For...Next循环。我使用Visual Basic 2010 Express作为运行这些代码的程序,如果有人可以帮助我,我将非常感谢。下面是需要转换为For ... Next循环类型的代码。再次感谢能够提供帮助的任何人。

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

1 个答案:

答案 0 :(得分:4)

试试这个:

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

注意:Step 2相当于intNum += 2循环中的Do语法。