473,320 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Repetition Structures: Looping

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 5382
willakawill
1,646 1GB
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
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
daniel aristidou
491 256MB
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
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

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: bearophileHUGS | last post by:
(This is a repost from another python newsgroup). While using some nested data structures, I've seen that I'd like to have a function that tells me if a given data structure contains one or more...
1
by: Chris Gamache | last post by:
For my particular case, word repetition shouldn't be relevant in determining the rank of a document. If I strip() the vector, I loose what relevance proximity and weight add to the rank. It seems...
0
by: Tim Chmielewski | last post by:
I am working on an Access 2000 database that imports a set-format XML file for use in order printing. At the moment it just imports the normal XML, but I want to be able to import extra...
9
suzee_q00
by: suzee_q00 | last post by:
I will admit that lots of times the obvious eludes me and this is most likely one of those times but if anyone has any ideas on what I can do to make this code work, I would greatly appreciate it....
6
by: udhas | last post by:
Write a program that prints the following diamond shape. You may use output statements that print a single asterisk (*), a single space or a single carriage return. Maximize your use of repetition...
4
by: mj.clift | last post by:
Hi All, I need to be able to choose a random string from an array. That is easy enough, but I want to restrict the repetition of that string until one or two other choices have been made. So the...
5
by: NATHAN1511 | last post by:
How To Write A Program By Using Repetition Structure As Show Below: 1 12 123 1234 12345
15
by: Beany | last post by:
Hi All, i need help with the following: i need to write a simple program that asks the user to input a number (minimum of 3 digits), which i seperate into individual digits and print the 1st...
3
by: anizatie | last post by:
if i wanna do some programming lets say... i hv a=a+3*a*(1.0^-10) how to generate the answer if a=(1.0^-10) ive done but wen i compared the answer using calculator..the answer is...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.