473,473 Members | 4,297 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

For...Next Loop

7 New Member
I'm trying to request a number, n, from 1 to 30 and one of the letters S or P. Then, depending upon whether S or P was selected, calculate the sum or product of the numbers from 1 to n. The calculations should be carried out in Funtion procedures.
Apr 15 '10 #1
9 2378
vb5prgrmr
305 Recognized Expert Contributor
And what code do you have so far? Look at the Rnd function in vb's help...
Apr 15 '10 #2
yarbrough40
320 Contributor
It would also be helpful if you actually asked a question.
Apr 15 '10 #3
jbhowell
7 New Member
I'm sorry my question is how do I get started. What I have so far isn't working which is no surprise. Below is what I have so far, please don't laugh, I'm new and learning but not well.

Expand|Select|Wrap|Line Numbers
  1. Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
  2.         Dim num As String = ""
  3.         For n = 1 To 30
  4.             num = txtNumber.Text
  5.         Next
  6.         CalculationS(num)
  7.         CalculationP(CDbl(num))
  8.     End Sub
  9.     Function CalculationS(ByVal num As String) As String
  10.         Dim calc As Double
  11.         Dim s As String = ""
  12.         If s = txtLetter.Text Then
  13.             calc = 1 + CDbl(num)
  14.         End If
  15.         Return CStr(calc)
  16.     End Function
  17.     Function CalculationP(ByVal num As Double) As Double
  18.         Dim calc As Double
  19.         Dim p As String = ""
  20.         If p = txtLetter.Text Then
  21.             calc = 1 * num
  22.         End If
  23.         Return calc
  24.     End Function
  25. End Class
Thanks,
Jennifer
Apr 16 '10 #4
yarbrough40
320 Contributor
I'm still very confused by all this. Could you back up and explain a little better the whole thing. Start with what your inputs are and then what the desired outputs would be from those inputs...

for example:
I have this form "Form1" which has 2 textboxes: "txtNumber", "txtLetter". The user types a random number into txtNumber between 1 and 30 and then types either S or P into txtLetter - then presses a button. I would like for the app to take that number and do bla bla and then finally return bla to another textbox where the user can do bla.

Apr 16 '10 #5
jbhowell
7 New Member
@yarbrough40
In a form I have a text box requesting the user to input a number between 1 and 30 as well as a letter p or s. When the button is clicked, depending on if p or s was selected, it should calculate the sum or product of the number from 1 to n. I'm supposed to use functions for the calculations. I've been working on it some more and have the below but it's still not right. Thanks for any advice you might have for me.
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Dim num As Double
Dim calc As Double
lstAnswer.Items.Clear()
For n As Integer = 1 To 30
num = CDbl(txtNumber.Text)
Next
CalculationS(num, calc)
CalculationP(num, calc)
lstAnswer.Items.Add(num & calc)
End Sub
Function CalculationS(ByVal num As Double, ByVal calc As Double) As Double
If txtLetter.Text = "S" Then
calc = 1 + num
End If
Return calc
lstAnswer.Items.Clear()
End Function
Function CalculationP(ByVal num As Double, ByVal calc As Double) As Double
If txtLetter.Text = "P" Then
calc = 1 * num
End If
Return calc
lstAnswer.Items.Clear()
End Function
Apr 16 '10 #6
patjones
931 Recognized Expert Contributor
I see a couple of problems here.

First, you're testing for option "p" or "s" inside the functions that are supposedly going to do the calculating. Make the determination of p or s the first thing you do, and then call the appropriate function depending on the option.

Second, your For...Next loop isn't really doing anything. It's just going to execute the assignment num = CDbl(txtNumber.Text) 30 times, giving you the same result for "num" each time. The way it is now, the calculations will take place outside the loop. These are accumulating calculations which must be done the number of times the loop specifies, and therefore placed inside the loop.

Also, the loop is going to run from 1 to 30 all the time, the way you've written it...no matter what number the user inputs. If "n" is the numeric input, it seems you'd want something like

Expand|Select|Wrap|Line Numbers
  1. For j = 1 to n
  2.      'Calculations in here, probably depending on 'j'...
  3. Next

Pat
Apr 16 '10 #7
jbhowell
7 New Member
Can you help with how to format the calculations? I've changed my code to the below but the calculations are way off, they keep giving me the answer 50 no matter if p or s is selected. Thanks!

Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Dim num As Double = CDbl(txtNumber.Text)
Dim sum, product As Double
Dim n As Double
lstAnswer.Items.Clear()
If txtLetter.Text = "S" Then
sum = CalculateS(n)
lstAnswer.Items.Add(num & sum)
ElseIf txtLetter.Text = "P" Then
product = CalculationP(n)
lstAnswer.Items.Add(num & product)
End If
End Sub
Function CalculateS(ByVal n As Double) As Double
Dim result As Double
For i As Integer = 1 To CInt(n)
result += i

Next
Return result
End Function
Function CalculationP(ByVal n As Double) As Double
Dim result As Double
For j As Integer = 1 To CInt(n)
result += j
result = n * j
Next
Return result
End Function
Apr 17 '10 #8
patjones
931 Recognized Expert Contributor
I think this is a big improvement over your previous code.

First, don't you mean to pass "num" into the respective functions? As in sum = CalculateS(num) and product = CalculationP(num).

In function CalculateS I would initialize result = 0 before going into the For loop, just as a matter of good practice.

In CalculationP, I don't think that calculation is going to give the factorial. I would initialize result = 1 before the For loop, then put only result = result*j inside the loop.

Pat
Apr 17 '10 #9
jbhowell
7 New Member
@zepphead80
That worked perfectly, thank you so much! It seems so simple which makes me feel so stupid. Thanks again!
Apr 17 '10 #10

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

Similar topics

5
by: Petr Bravenec | last post by:
I have found that when I use the RETURN NEXT command in recursive function, not all records are returned. The only records I can obtain from function are records from the highest level of...
13
by: PeterZ | last post by:
Hi, Back to basics! My understanding is that the only way to exit a For-Next loop prematurely is with the 'break' keyword. How are you supposed to do that if you're inside a Switch...
14
by: Ale K. | last post by:
i know that For...Next as a Exit For.... there is any way from the middle of my for...next code to go to the next item and jump out part of my code , like the same thing that can be done with exit...
23
by: Mitchell Vincent | last post by:
Is there any way to "skip" iterations in a for loop? Example : for x = 1 to 10 if something = 1 next endif
13
by: andreas | last post by:
Hi, I want to do some calculation like ( t1 and t2 are known) for i = t1 to t2 for j = t1 to t2 ..... ..... for p = t1 to t2 for q = t1 to t2
4
by: Neo | last post by:
I found on error resume next doesn't work in for each... e.g. on error resume next for each x in y 'do stuff next if you have an error in for each loop, it falls in infinite loop... it...
0
ADezii
by: ADezii | last post by:
If you want to visit each item in an Array, you have two alternatives: Use a For Each..Next loop, using a Variant to retrieve each value in turn. Use a For...Next loop, looping from the Lower...
0
by: Semajthewise | last post by:
Hi all. I'm starting on my next part of my teach myself vb program. What I am trying to do is on button click open a textfile showing the math as it would be done long hand. I started writing the...
2
ADezii
by: ADezii | last post by:
If you are executing a code segment for a fixed number of iterations, always use a For...Next Loop instead of a Do...Loop, since it is significantly faster. Each pass through a Do...Loop that...
6
by: baldrick | last post by:
Hi, I have some intensive number crunching code that I want to break up into threads so PCs with several processors can do the job quicker. I have a loop that will go from say 1 to 10, and for...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
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...
0
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...
0
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...
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.