473,657 Members | 2,489 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Repetition Structures: Looping

1 New Member
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 5399
willakawill
1,646 Top Contributor
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
Kyosuke18
11 New Member
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.Te xt) > 0 Then
Do While x < Val(TextBox1.Te xt)
intSum = intSum + x
x = x + y
y = x + 1
Loop
lblResult.Capti on = "Summation of" & TextBox1.Text & "is" & Str(intSum) & "."
Else
lblResult.Capti on = "Enter a positive number"
End If

but here is the is the code for FOR NEXT STATEMENT:

If Val(TextBox1.Te xt) > 0 Then
intSum = 0
For x = 1 To Val(TextBox1.Te xt)
intSum = intSum + x
Next x
lblResult.Capti on = "Summation of" & TextBox1.Text & "is" & Str(intSum) & "."
Else
lblResult.Capti on = "Enter a positive number"
End If

In DO UNTIL LOOP:

If Val(TextBox1.Te xt) > 0 Then
intSum = 0
x = 1
Do Until x > Val(TextBox1.Te xt)
intSum = intSum + x
x = x + 1
Loop
lblResult.Capti on = "Summation of" & TextBox1.Text & "is" & Str(intSum) & "."
Else
lblResult.Capti on = "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 Contributor
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
Kyosuke18
11 New Member
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.Te xt) > 0 Then
intSum = 0
x = 1
Do While x <= Val(TextBox1.Te xt)
intSum = intSum + x
x = x + 1
Loop
lblResult.Capti on = "Summation of " & TextBox1.Text & " is " & Str(intSum) & "."
Else
lblResult.Capti on = "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
1990
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 cyclic references (a way to recognise a cycle in a graph is to do a depth-first search, marking vertices along the way. An already marked vertex means a cycle.) Do you know where I can find a function like this? To be more explicit about this...
1
1942
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 impossible, yet I ask anyway: Is it possible to eliminate the second (third, fourth, fifth, etc.) occurrence of any given word when its presence in the document is being scored, yet kept in the equation for modifications to the score when proximity...
0
1176
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 information as follows: <OrderLineItem> <ItemCode>40RP3</ItemCode> <ItemSummary>Personalised Ribbons Over 1500 (each) </ItemSummary> <ItemQuantity>1500</ItemQuantity>
9
7330
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. Here's the code: while ( ( fgets ( buf, BUFLEN, fin ) ) != NULL ) /* pulls a line from "fin" up to the length of BUFLEN and stores it in "buf" */ { rmNl ( buf ); /* remove new line */ if ( !lineWithBlanks ( buf ) ) { for (i = 0; buf !=...
6
34224
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 (with nested For … Next statements) and minimize the number of output statements. * *** ***** ******* ********* ******* *****
4
2991
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 following output would be ok a,b,c,a,d,c or a,c,d,a,d,a but the following would not; a,b,c,c, or a,b,b,c, etc... I hope that makes sense. If it helps to understand what I'm trying to do I have the following python code.
5
3053
by: NATHAN1511 | last post by:
How To Write A Program By Using Repetition Structure As Show Below: 1 12 123 1234 12345
15
3074
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 digit 1 time, 2nd digit 2 times, 3rd digit 3 times and so forth... I cannot get the last bit working, what bit of code can i use for the repetition part? many thanks
3
1354
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 wierd..they r not same.. i used "for" repetition..
0
8392
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8603
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1944
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1604
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.