|
-
Jun 16th, 2011, 02:54 AM
#1
Thread Starter
Hyperactive Member
math, simple logic
is my logic problem?
I want that number "75", foreach 20
show result:
0
20
40
60
75
any advice please!
Code:
Private Sub Command1_Click()
Dim a As Integer
Dim b As Integer
Dim c As Integer
a = "75"
b = a Mod 20
c = a \ 20
Dim i As Integer
For i = 0 To a Step 20
List1.AddItem i
Next i
List1.AddItem b
List1.AddItem c
End Sub
Last edited by rpool; Jun 16th, 2011 at 02:57 AM.
-
Jun 16th, 2011, 05:17 AM
#2
Re: math, simple logic
i m not sure that what u r expecting, but something like this...
Code:
Private Sub Command1_Click()
Dim a As Integer
Dim b As Integer
Dim c As Integer
a = 75
b = 20
c = a \ b
Dim i As Integer
For i = 0 To c
List1.AddItem i * b
Next i
List1.AddItem a
End Sub
-
Jun 16th, 2011, 06:49 AM
#3
Re: math, simple logic
after the loop
vb Code:
if not i = a then list1.additem a
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jun 16th, 2011, 07:34 AM
#4
Re: math, simple logic
pete, is it to avoid repeat the last value when a mod b = 0? if yes, i think it is like this after the loop
Code:
If Not (i - 1) * b = a Then List1.AddItem a
-
Jun 16th, 2011, 04:14 PM
#5
Re: math, simple logic
is it to avoid repeat the last value when a mod b = 0?
the requested result in th op showed the values for each step and the max value, regardless of whether it was a step
show result:
0
20
40
60
75
at least that was my interpretation of it
as far as i could see the mod result was not required
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jun 16th, 2011, 08:58 PM
#6
Re: math, simple logic
actuly, if a mod b =0 then the max value is achieved in the loop itself, so hav to avoid the same value repeat again in the list
Code:
If Not b * c = a Then List1.AddItem a
'or
If Not a Mod b = 0 Then List1.AddItem a
-
Jun 17th, 2011, 04:01 AM
#7
Re: math, simple logic
so hav to avoid the same value repeat again in the list
i did that with not i = a
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jun 17th, 2011, 04:32 AM
#8
Re: math, simple logic
actuly 'i' = quotient of a/b, so 'i' cant be equal to 'a' unless 'b' = 1
-
Jun 17th, 2011, 05:26 AM
#9
Re: math, simple logic
as i is the counter of the loop, after the loop it is equal to the last step, in this case 60, so not = to a
if it was step 25 or a = 80 then i would = a
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jun 17th, 2011, 07:24 PM
#10
Re: math, simple logic
 Originally Posted by westconn1
as i is the counter of the loop, after the loop it is equal to the last step, in this case 60, so not = to a
if it was step 25 or a = 80 then i would = a
That's not true. After the loop, the value of i will go one more extra step beyond the last step.
Code:
For i = 0 to 13 step 3
'-- do nothing
Next
Debug.Print i '-- at this point: i = 15
Why not do it like this without extra calculation:
(a - 1) is used to avoid (i = a) within the loop.
Code:
Private Sub Command1_Click()
Dim a As Long
Dim b As Long
Dim i As Long
a = 75
b = 20
For i = 0 To (a - 1) Step b
List1.AddItem i
Next
List1.AddItem a
End Sub
-
Jun 17th, 2011, 08:41 PM
#11
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|