440,352 Members | 1,593 Online
Need help? Post your question and get tips & solutions from a community of 440,352 IT Pros & Developers. It's quick & easy.

# Repetition Structures: Looping

 P: 1 Hi, Can someone help me with this assignment, I am confused of what is needed to be done here. I am suposed to design a program that models a worm's behavior in the following scenario: A worm is moving toward an apple. each time it moves, the worm cuts the distance between itself and the apple by its own body lenght until the worm is close enough to enter the apple. The worm can enter the apple when it is within a body lenth of the apple. I was told I can make up my own length for the worm and distantand what ever I need to make this design. So can someone give me some example of what I can do for this scenario that deals with a Repetition structure design? Thank you Mar 2 '07 #1
4 Replies

 100+ P: 1,646 Hi. It is the policy of TSDN not to provide answers to student assignments. You need to do the work yourself and if you are completely stuck you should return to your teacher and work it out with them. Mar 2 '07 #2

 P: 11 Hi everyone.. I made a program of Summation.. I have 2 labels, 1 textbox and 1 button. Once I typed the number in textbox for example "3" and click the button, the result is in Label where it says " The Summation of 3 is 6. I coded it using for next statement and it works, in do until loop it works also but when I used the DO WHILE LOOP sometimes it has infinite loop or the error that I encountered in "The Summation of 3 is 4". Here is the code that I did in DO WHILE: intSum = 1 x = 1 y = 1 If Val(TextBox1.Text) > 0 Then Do While x < Val(TextBox1.Text) intSum = intSum + x x = x + y y = x + 1 Loop lblResult.Caption = "Summation of" & TextBox1.Text & "is" & Str(intSum) & "." Else lblResult.Caption = "Enter a positive number" End If but here is the is the code for FOR NEXT STATEMENT: If Val(TextBox1.Text) > 0 Then intSum = 0 For x = 1 To Val(TextBox1.Text) intSum = intSum + x Next x lblResult.Caption = "Summation of" & TextBox1.Text & "is" & Str(intSum) & "." Else lblResult.Caption = "Enter a positive number" End If In DO UNTIL LOOP: If Val(TextBox1.Text) > 0 Then intSum = 0 x = 1 Do Until x > Val(TextBox1.Text) intSum = intSum + x x = x + 1 Loop lblResult.Caption = "Summation of" & TextBox1.Text & "is" & Str(intSum) & "." Else lblResult.Caption = "Enter a positive number" End If Any help would be appreciated. Because I'm confused in DO WHILE LOOP. It is easy to use the FOR NEXT Statement because it increments but in DO WHILE I get the wrong result.. I want to know just the concepts. Jun 11 '09 #3

 100+ P: 491 well i guess you must be using a wrong if statement, if it works in a for loop and not in a while loop. Try using comments in your code, this will make an error which is like this alot easier and enables you to figure out logic errors easier. Write down the logic of the loop in plain language. Another techique to use when checking ur code in loops is to use a variable table. Where you make a table and change the variables in order downwards as you go through the code. This way you will be able to understand exactly what your code is doing. Newer versions of vb have this built in if you look at the variable table while exectuting with breakpoints. Answering you question a bit more directly a do while loop occurs aslong as the statement is true ..... in this case the problem occurs because your loop condition is > or < Where as in the for loop u are telling it to go upuntil and including the value in the textbox rather than the smaller value Therefore change ur While loop > into an = Jun 11 '09 #4

 P: 11 hi daniel aristidou, sorry for the late reply but thanks for the post.. I really appreciated it. Well I will follow what you've mentioned so the next time I code, I will make a comment and trace it. I'm confused on the 2nd technique you've mentioned: "Another techique to use when checking ur code in loops is to use a variable table. Where you make a table and change the variables in order downwards as you go through the code." I'm just newbie in programming and trying to practice based on what I have reviewed and browse on the net. I have a background on Visual Basic but I focus a lot on a network side. For a change, I'm interesting in programming side so I study VB. Just the concepts on Looping structures because I know it is very important. I'm using VB6 but since I don't have an installer yet, Temporarily, I practice on VBA Excel. I got the correct result yesterday. Here is the code that I've done: If Val(TextBox1.Text) > 0 Then intSum = 0 x = 1 Do While x <= Val(TextBox1.Text) intSum = intSum + x x = x + 1 Loop lblResult.Caption = "Summation of " & TextBox1.Text & " is " & Str(intSum) & "." Else lblResult.Caption = "Enter a positive number" End If End Sub Based on what I have understand on DO UNTIL LOOP-->it repeats the block of statements until the statement is true while in DO WHILE-->it is the same but if it is false it will stop and render the statement. Correct me if I commit some mistake here. I will study more on looping structures in VB and I want to learn more. But again, Thanks for the post. =) Jun 16 '09 #5