如何将此For ... Next语句更改为Do ... Loop [英] How do I change this For... Next statementinto a Do...Loop

查看:261
本文介绍了如何将此For ... Next语句更改为Do ... Loop的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Const intTERM As Integer = 5
        Dim dblPrincipal As Double
        Dim dblPayment As Double
        Dim blnIsConverted As Boolean

        lblPayments.Text = String.Empty
        blnIsConverted = _
            Double.TryParse(txtPrincipal.Text, dblPrincipal)

        If blnIsConverted Then
              For dblRate As Double = 0.05 To 0.1 Step 0.01
                dblPayment = -Financial.Pmt(dblRate / 12, intTERM * 12, dblPrincipal)
                lblPayments.Text = lblPayments.Text & _
                dblRate.ToString("P0") & "->" _
                & dblPayment.ToString("C2") & _
                ControlChars.NewLine
           Next dblRate

When I put in Do....Loop it says dblRate is not declared

推荐答案

这是因为未声明dblRate!

That is because dblRate is NOT declared!
Dim dblPrincipal As Double
Dim dblPayment As Double
Dim blnIsConverted As Boolean





不存在。



声明并看到差异!



你在if then语句中声明它,所以如果符合条件则声明它。

当你把在do循环中,它不是在有条件的基础上声明的。

在循环外声明它并使其可用。



Not there.

Declare it and see the difference!

You are declaring it within the if then statement, so it is declared if the conditions are met.
When you put this in a do loop it is not declared on a conditional basis.
Declare it outside the loop and have it available.


在For循环中,它是3 -in-1:

1.声明和初始化
In For loop, it is 3-in-1:
1. declaration and initialization of
dblRate As Double = 0.05



2.条件检查


2. condition check

dblRate <= 0.1



3.每次迭代按dblRate增加0.01



对于Do While循环,它仅检查条件,即点2.您必须在代码中分别处理点1和3。参见示例:


3. increase by dblRate by 0.01 for each iteration

In the case of Do While loop, it only checks the condition, i.e. point 2. You have to take care of points 1 and 3 separately in your code. See example:

If blnIsConverted Then
     ' must declare and initilise dblRate
     Dim dblRate As Double = 0.05

     Do While dblRate <= 0.1
        ' other code
        ' increase by the step value of 0.01
        dblRate = dblRate  + 0.01
     Loop

Next dblRate


这篇关于如何将此For ... Next语句更改为Do ... Loop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆