|
-
May 6th, 2013, 04:11 PM
#1
Thread Starter
Member
Need help with Subtraction and variables
Hello,
I have a few If-Then statements and the final one that I need is to basically "subtract 1 from every 5"
In my case, for every 5 miles traveled I need to subtract 1 year of life - its a wackly problem that I have.
How can I code this? Googling this results in a bunch of time subtraction links - that's not really what i need though.
Thanks!
-
May 6th, 2013, 04:21 PM
#2
Re: Need help with Subtraction and variables
life = life - (milesTravelled \ 5)
-
May 6th, 2013, 04:23 PM
#3
Thread Starter
Member
Re: Need help with Subtraction and variables
 Originally Posted by Joacim Andersson
life = life - (milesTravelled \ 5)
Cool, I'll play around with that. The problem that I have is that I have the following code:
If sex = "M" Then
If state = "IL" Then
age = 67
ElseIf state = "WI" Then
age = 70
ElseIf state = "MN" Then
age = 71
ElseIf state = "TX" Then
age = 72
End If
If smoker = "Y" Then
age = age - 8
ElseIf smoker = "N" Then
age = age
If height > 72 Or height < 66 Then
age = age - 2
If weight > 220 Then
age = age - 4
ElseIf weight < 130 Then
age = age - 2
End If
End If
End If
End If
Would I stick that somewhere in the last If-Then, or should I create a brand new one? I guess I don't know where to put it
-
May 6th, 2013, 04:25 PM
#4
New Member
Re: Need help with Subtraction and variables
You can probably use some sort of modular division to achieve this. If the distance / 5 does not = 0, then you know you have not traveled 5 miles. Then, you take whatever value you get from the division and multiply by 1 to get how many years to subtract.
I.E, If distTraveled mod 2 = 0 Then
subLife = (distTraveled / 2) * 1
-
May 6th, 2013, 04:27 PM
#5
Re: Need help with Subtraction and variables
Create a brand new one. After all, the miles traveled is an additive impact, so you can calculate the age based on the other factors just as you have. Once you have that age, then perform the calculation for miles traveled. For one thing, this will make the code a bit easier to read, but there's a bigger issue. One thing you probably ought to consider is that miles traveled can be HUGE, even for a very young person. I think that you ought to calculate the impact of the miles traveled and put it into a temporary variable, then compare that to age. After all, it seems like you run the risk of getting the age into negative territory, which would be decidedly disturbing.
One thing to note about what JA wrote is that he used \ rather than /. This is a key distinction, as \ is an integer divide which returns an integer, whereas / is a decimal divide that will normally return a double, but can return a decimal if the operands are decimal.
My usual boring signature: Nothing
 
-
May 6th, 2013, 04:36 PM
#6
Re: Need help with Subtraction and variables
I can't really say where the code should go, I have a hard time understanding the logic you have this far. Apparently you're height and weight only have an impact on your age if you're not a smoker and also only if you're a male, since nothing of what you posted have any impact if not the sex is set to "M". So with that kind of strange logic maybe the miles you've travelled is only effecting your age if you live in IL.
You obviously know what the logic really should be, should the miles travelled effect the life expectancy regardless of where you live, if you're a smoker and also regardless of your height and weight? If yes, then it should probably not be inside any of the If statements at all since it will effect you regardless of the other parameters. If it does rely on some other parameter then it should be inside the If statement for that particular parameter.
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
|