# I need help with looping

• (2 Pages)
• 1
• 2

## 16 Replies - 1510 Views - Last Post: 25 April 2012 - 05:16 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=275812&amp;s=194f6071871e89fc5ac54ed343ce78bf&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 EdwinAndrew

• New D.I.C Head

Reputation: 0
• Posts: 15
• Joined: 18-April 12

# I need help with looping

Posted 18 April 2012 - 06:21 PM

Well, Lately in my computer science class I'm trying to get ahead of the class. I got stuck on a mathematical problem: "Suppose two rabbits are placed on the island and that every pair of rabbits has a litter of 4 rabbits. So after 1 generation there would be 2 + 4 = 6 rabbits. After two generations there would be 6 + 12 = 18 rabbits.How would the rabbit population grow with each generation?" I understand that if the 3rd generation came it would be 54 rabbits. I just can't get the numbers and values to come under the labels in the textbox.Also I must show how many rabbits there are in a square mile.
Please do not do the program for me but just tell me what I need to do to get it right thanks!

Is This A Good Question/Topic? 0

## Replies To: I need help with looping

### #2 DimitriV

• vexing conundrum

Reputation: 587
• Posts: 2,746
• Joined: 24-July 11

## Re: I need help with looping

Posted 18 April 2012 - 06:30 PM

I'll give you some pseudo code.
Begin Loop
rabbits = rabbits * 2
wait a bit
Show result
Loop

### #3 DimitriV

• vexing conundrum

Reputation: 587
• Posts: 2,746
• Joined: 24-July 11

## Re: I need help with looping

Posted 18 April 2012 - 06:35 PM

Use a Timer. You can Set the interval so that every time that time is reached it will execute code.
http://msdn.microsof...orms.timer.aspx

### #4 aizo24

• New D.I.C Head

Reputation: 1
• Posts: 17
• Joined: 16-April 12

## Re: I need help with looping

Posted 18 April 2012 - 06:42 PM

No Need for timer, just use
```Threading.Thread.Sleep(1000) '1000 = 1 sec.
```

### #5 EdwinAndrew

• New D.I.C Head

Reputation: 0
• Posts: 15
• Joined: 18-April 12

## Re: I need help with looping

Posted 18 April 2012 - 06:51 PM

aizo24, on 18 April 2012 - 06:42 PM, said:

No Need for timer, just use
```Threading.Thread.Sleep(1000) '1000 = 1 sec.
```

Will you explain to me what the code means because I've been learning Visual Basic just for 1 month.

### #6 DimitriV

• vexing conundrum

Reputation: 587
• Posts: 2,746
• Joined: 24-July 11

## Re: I need help with looping

Posted 18 April 2012 - 06:54 PM

It means the thread that the program is operating in will sleep (pause) for 1000 milliseconds.
A better explanation is here though: http://msdn.microsof...0bd51t.aspx#Y40

### #7 aizo24

• New D.I.C Head

Reputation: 1
• Posts: 17
• Joined: 16-April 12

## Re: I need help with looping

Posted 18 April 2012 - 07:35 PM

Simply put, we all know that our computers executes operations and calculations in a snap, this code allows you to pause the operation or calculation by 1 sec. or depends on what value you use. therefore allowing you to see the result of every pass of the loop

hope this helps

### #8 _HAWK_

• Master(Of Foo)

Reputation: 1160
• Posts: 4,443
• Joined: 02-July 08

## Re: I need help with looping

Posted 18 April 2012 - 07:54 PM

Yes, because freezing the UI is a good thing (note inner sarcasm). I would avoid the Thread.Sleep part. A timer that executes a sub that recalculates the rabbit numbers would be better.

### #9 aizo24

• New D.I.C Head

Reputation: 1
• Posts: 17
• Joined: 16-April 12

## Re: I need help with looping

Posted 18 April 2012 - 08:16 PM

No need to be sarcastic bro...it's just my opinion. Well for
A small, i mean not complicated calculation
Why do it the hard way right? Hehe well
Again just an opinion and im not an expert, just trying to help

### #10 _HAWK_

• Master(Of Foo)

Reputation: 1160
• Posts: 4,443
• Joined: 02-July 08

## Re: I need help with looping

Posted 18 April 2012 - 09:56 PM

But it was irresistible! Don't take it wrong I just like to teach the right way to do things - no user wants an unresponsive app b/t events. Homework should not be any diff either. I was trying an approach that was not like

### #11 EdwinAndrew

• New D.I.C Head

Reputation: 0
• Posts: 15
• Joined: 18-April 12

## Re: I need help with looping

Posted 19 April 2012 - 01:09 PM

10 ProveIt 01 Counting Rabbits

Look out! You almost hit that Jack Rabbit crossing the road! Do you suppose that rabbit thinks it can live forever? Say, that gives me an idea. Suppose rabbits did live forever. Let’s say that there is an island somewhere with lots of vegetation for rabbits, and no predators. Suppose two rabbits are placed on the island and that every pair of rabbits has a litter of 4 rabbits. So after 1 generation there would be 2 + 4 = 6 rabbits. After two generations there would be 6 + 12 = 18 rabbits.

How would the rabbit population grow with each generation?
Show the generation number, the number of rabbits, and the number of rabbits per square foot, assuming the island is one square mile in size and that there are 5280 feet in a mile.

Allow the user to choose the number of generations to show.

In order to make the display easier to read, use the Visual Basic constants vbTab and vbNewLine.

To format the output, use the Format function.
For example:
VariableX = Format(VariableX, “###,###,###,###,##0”)
Or
VariableY = Format(VariableY, “###,##0.0000”)

^ that was the assignment and the form must have 2 textboxes one for the Generations and one to show all the information. Also a button to calculate the population using the generation and input it into the textbox2

The attachment is the full question if you can't understand what I'm saying.  10_proveit01_instructions.doc (36.5K)

### #12 DimitriV

• vexing conundrum

Reputation: 587
• Posts: 2,746
• Joined: 24-July 11

## Re: I need help with looping

Posted 19 April 2012 - 01:58 PM

Doesn't look too hard…
Which part do you want to tackle first?

For example:
```TextBox1.Text += vbNewLine + generation + vbTab + rabbits + vbTab + concentration
```

^
|___Just pseudo.

### #13 EdwinAndrew

• New D.I.C Head

Reputation: 0
• Posts: 15
• Joined: 18-April 12

## Re: I need help with looping

Posted 19 April 2012 - 04:33 PM

DimitriV, on 19 April 2012 - 01:58 PM, said:

Doesn't look too hard…
Which part do you want to tackle first?

For example:
```TextBox1.Text += vbNewLine + generation + vbTab + rabbits + vbTab + concentration
```

^
|___Just pseudo.

I don't really understand when we have to do a loop statement "for..Next" and when i tried to do the program, i got 1001 as generation in textbox2. Becuase i looped generations to go up to 1000.Also the template i have came with something called windows generated code... i think.

If you guys want the template:
```Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.Button1 = New System.Windows.Forms.Button
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'Label1
'
Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label1.Location = New System.Drawing.Point(16, 8)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(88, 23)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Generations:"
'
'TextBox1
'
Me.TextBox1.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.TextBox1.Location = New System.Drawing.Point(112, 8)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(40, 22)
Me.TextBox1.TabIndex = 1
'
'Button1
'
Me.Button1.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Button1.Location = New System.Drawing.Point(16, 48)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(136, 32)
Me.Button1.TabIndex = 2
Me.Button1.Text = "Count Rabbits"
'
'TextBox2
'
Me.TextBox2.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.TextBox2.Location = New System.Drawing.Point(16, 104)
Me.TextBox2.Multiline = True
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
Me.TextBox2.Size = New System.Drawing.Size(376, 232)
Me.TextBox2.TabIndex = 3
'
'Form1
'
Me.ClientSize = New System.Drawing.Size(408, 362)
Me.Name = "Form1"
Me.Text = "Counting Rabbits"
Me.ResumeLayout(False)
Me.PerformLayout()

End Sub

#End Region

End Class

```

This is what I've tried:
```Public Class Form1
'Edwin Andrew
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.Button1 = New System.Windows.Forms.Button
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'Label1
'
Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label1.Location = New System.Drawing.Point(16, 8)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(88, 23)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Generations:"
'
'TextBox1
'
Me.TextBox1.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.TextBox1.Location = New System.Drawing.Point(112, 8)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(40, 22)
Me.TextBox1.TabIndex = 1
'
'Button1
'
Me.Button1.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Button1.Location = New System.Drawing.Point(16, 48)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(136, 32)
Me.Button1.TabIndex = 2
Me.Button1.Text = "Count Rabbits"
'
'TextBox2
'
Me.TextBox2.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.TextBox2.Location = New System.Drawing.Point(16, 104)
Me.TextBox2.Multiline = True
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
Me.TextBox2.Size = New System.Drawing.Size(376, 232)
Me.TextBox2.TabIndex = 3
'
'Form1
'
Me.ClientSize = New System.Drawing.Size(408, 362)
Me.Name = "Form1"
Me.Text = "Counting Rabbits"
Me.ResumeLayout(False)
Me.PerformLayout()

End Sub

#End Region
Dim Generations As Integer
Dim RabbitPair As Integer
Dim RabbitLitter As Integer
Dim Populations As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox2.Text = "Gen. " & vbTab & "Rabbits " & vbTab & "Per Square Mile "
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Generations = TextBox1.Text
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'2+4=6 in 1st generation
'6+12=18 in 2nd generation
'18+36=54 in 3rd generation
'Need to have generations go up by 1 as a interval
'Then Generations = Populations = (RabbitPair + RabbitLitter)
RabbitPair = 2
RabbitLitter = 4
TextBox2.Text = Generations
For Generations = 1 To 1000
Populations = (RabbitPair + RabbitLitter)
TextBox2.Text = Populations
Next
RabbitPair = Populations
RabbitLitter = (RabbitPair ^ 2)
'TextBox2.Text = "Gen. " & vbTab & "Rabbits " & vbTab & "Per Square Mile "
TextBox2.Text = "Gen. " & vbTab & "Rabbits " & vbTab & "Per Square Mile " & vbNewLine & Generations & vbTab & Populations

End Sub

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged

End Sub
End Class

```

### #14 _HAWK_

• Master(Of Foo)

Reputation: 1160
• Posts: 4,443
• Joined: 02-July 08

## Re: I need help with looping

Posted 19 April 2012 - 09:08 PM

Stay out of the designer generated code, you should be working in the form1.vb and form1.vb[design] and not the form1.Designer.vb.

### #15 EdwinAndrew

• New D.I.C Head

Reputation: 0
• Posts: 15
• Joined: 18-April 12

## Re: I need help with looping

Posted 20 April 2012 - 04:15 PM

I know i dont work on the designer generated code and i work on the form so did you find any problems with what i've done?