Thursday, July 30, 2009

Visual basic 2005 do while...loops?

i have this section of code:





Do While f %26lt; p


If c %26gt; b Then


f = f + 1


Form1.txtTime.Text = f


k = d * a


m = Math.Floor(c / b)


e2 = m + a


Form1.txtShares.Text = e2


g = (c - (b * m)) + k


Form1.txtMoney.Text = g


Else


f = f + 1


Form1.txtTime.Text = f


k = d * a


j = c + k


Form1.txtMoney.Text = j


End If


Loop





and it won't loop properly. the first condition in the if then else statement is usually false so it goes on to the else part, but when the loop is concerned it stays in the else part of the code, i need a way to make the loop statement go over the entire if then statement for the duration of the loop rather than just looping through the else statement

Visual basic 2005 do while...loops?
"i need a way to make the loop statement go over the entire if then statement for the duration of the loop rather than just looping through the else statement"





are you sure it satisfies the condition of the if statement?


as long as f is not less than p then it will always loop into the else statement
Reply:What do you mean by "go over the entire if then statement"? I'm sure it does check the if condition, the thing is that "c%26gt;b" is an invariant here, meaning it stays true (or false) for the entire loop sequence. So this section of code is equivalent to this one:


If c %26gt; b Then


Do While f %26lt; p


...


Loop


Else


Do While f %26lt; p


...


Loop


End If





Hope this helps.
Reply:looks ok strange it wont
Reply:I do not see any code which assigns values to either C or B





The default values for unassigned variables are zero


so both C %26amp; B are equal to zero


The If condition C%26gt;B would then be 0%26gt;0 (is zero greater than zero) resulting in false and triggering the else portion of the code.





During the loop there is no code which modifies the value of either C or B so this condition will always be false.





You will need to add code which assigns updated values to C and/or B so that the evaluation of C%26gt;B has a chance of becomming true rather than being always stuck as false as it is now


No comments:

Post a Comment