locked
How to stop and restart for loop RRS feed

  • Question

  • Hi everybody

    I wrote the following code to get values of DH. It works but I want to stop the loop at a Specific DH value and how to restart the loop if I want to add different inputs.

    Private

    SubbtnCalculate_Click(ByValsender AsSystem.Object, ByVale AsSystem.EventArgs) HandlesbtnCalculate.Click

     

    DimQ AsDouble


           

    DimdblX AsDouble


           

    DimS AsDouble= Rnd()

           

    DimL AsDouble


           

    Dimrho AsDouble


           

    Dimmu AsDouble


           

    DimD AsDecimal= Rnd()

           

    DimAe AsDecimal= Rnd()

           

    DimDCal AsDouble= Rnd()

           

    DimAeCal AsDouble


           

    DimDH AsDouble= 0.0

           

    DimsumDH AsDouble= 0D

           

    DimsumS AsDouble


            L = 50

            S = 0.5

            dblX = 4

            rho = 1000

            mu = 0.001

            D = 13.1

            Ae = 21.5

            DCal = D / 1000

            AeCal = (Ae / 1000000)

     

           

    DimDH_Helper AsDouble= (1.151 * (rho ^ -0.271) * (mu ^ 0.271) * (9.81 ^ -1) * (S ^ 0.802) * (DCal ^ -1.649) * (AeCal ^ 0.288))

     

           

    ForQ = dblX To500 StepdblX

                DH = ((((Q / (3600 * 1000))) / ((3.14 * DCal ^ 2) / 4)) ^ 1.729) * DH_Helper

                sumDH = sumDH + DH

                sumS = sumS + S

                txtLatDis.Text = txtLatDis.Text &

    ControlChars.NewLine & Q & vbTab & sumDH

           

    Next


            lblDeltaH.Text = Format(sumDH,

    "00.000")

            lblLatDis.Text = Q - dblX

            lblS.Text = sumS

       

    EndSub

     

    Wednesday, November 5, 2014 9:25 PM

Answers

  • http://msdn.microsoft.com/en-us/library/t2at9t47.aspx

    You want to exit out of a for loop then you use an Exit For statement. Once you exit out of the for loop, there is no restart, and you have to start for loop over again from the top -- from the scratch like when you first started it.

    • Proposed as answer by Mr. Monkeyboy Friday, November 7, 2014 6:24 AM
    • Marked as answer by Carl Cai Wednesday, November 19, 2014 9:44 AM
    Wednesday, November 5, 2014 9:34 PM
  • But do you advise to use anthor loop else for?

    Use the same For/Next loop, but put it in a function.    When you call the function, pass in the values for the for loop to use (start point, increment, exit value and any variables that are used internally in the loop).  The return value appears to be a List(Of String) that you can put in a textbox. 

    When you For/Next loop is set up in a Function you can call it from any point in your code, as many times as needed.   See:

    http://msdn.microsoft.com/en-us/library/6xxtk8kx.aspx

    • Proposed as answer by Mr. Monkeyboy Friday, November 7, 2014 6:24 AM
    • Marked as answer by Carl Cai Wednesday, November 19, 2014 9:44 AM
    Thursday, November 6, 2014 8:18 PM

All replies

  • http://msdn.microsoft.com/en-us/library/t2at9t47.aspx

    You want to exit out of a for loop then you use an Exit For statement. Once you exit out of the for loop, there is no restart, and you have to start for loop over again from the top -- from the scratch like when you first started it.

    • Proposed as answer by Mr. Monkeyboy Friday, November 7, 2014 6:24 AM
    • Marked as answer by Carl Cai Wednesday, November 19, 2014 9:44 AM
    Wednesday, November 5, 2014 9:34 PM
  • Hello,

    Can you please edit your question and place your code into a code block


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Wednesday, November 5, 2014 9:45 PM
  • Thanks for your note I got satisfying result. But do you advise to use anthor loop else for?
    Thursday, November 6, 2014 7:32 PM
  • But do you advise to use anthor loop else for?

    Use the same For/Next loop, but put it in a function.    When you call the function, pass in the values for the for loop to use (start point, increment, exit value and any variables that are used internally in the loop).  The return value appears to be a List(Of String) that you can put in a textbox. 

    When you For/Next loop is set up in a Function you can call it from any point in your code, as many times as needed.   See:

    http://msdn.microsoft.com/en-us/library/6xxtk8kx.aspx

    • Proposed as answer by Mr. Monkeyboy Friday, November 7, 2014 6:24 AM
    • Marked as answer by Carl Cai Wednesday, November 19, 2014 9:44 AM
    Thursday, November 6, 2014 8:18 PM
  • I'm so greatfull for you, it works.
    Sunday, November 9, 2014 11:00 AM