Struggling with If statement
Page 1 of 112 Replies - 590 Views - Last Post: 20 November 2012 - 07:32 AM
#1
Struggling with If statement
Posted 20 November 2012 - 02:41 AM
I am not sure if the code i am trying to set up is C or not as it is within an online form building tool that i am using, so apologies if this is in the wrong forum
I am trying to create a code that looks at an number entered in a field and displays an answer based on the number.
This is the code that is not working:
IF [BMI] < '18.5' THEN
'Underweight - it is likely you are not getting enough nutrients'
ELSEIF [BMI] > '18.5' THEN
'Healthy weight'
ELSEIF [BMI] > '24.9' THEN
'Overweight'
ELSEIF [BMI] > '29.9' THEN
'Obese'
ELSEIF [BMI] > '39.9' THEN
'Severely Obese'
ENDIF
It seems to work up till the "Healthy Weight" but all numbers after that show as healthy as well.
Below is the table i am replicating for the results:
BMI kg/m2 Classification
<18.5 Underweight-it is likely you are not getting enough nutrients
18.5-24.9 Healthy weight
25-29.9 Overweight
30-39.9 Obese
≥40 Severely obese
Any help you could provide would be greatly appreciated, and again sorry if this is in the wrong area.
Replies To: Struggling with If statement
#2
Re: Struggling with If statement
Posted 20 November 2012 - 02:49 AM
Your problem is that you don't limit your numbers. The code is evaluated from top to bottom here, so are those chained ifs. When the first if is executed, it breaks from the chain of else-ifs and that's it.
If you pass in number 100, then in first if 100 is greater than 18.5, and so if body gets executed and that's it. You need limits. And code tags.
I don't know what language is that, but let me guess that it uses logical "AND"
ELSEIF [BMI] > '18.5' AND [BMI] <= '24.9' THENAnd so on.
This post has been edited by Xupicor: 20 November 2012 - 02:49 AM
#3
Re: Struggling with If statement
Posted 20 November 2012 - 02:52 AM
Lets take a value of 25
The control enters your first IF, finds it is false and goes to the next IF
Here, it checks IF 25>18.5 .That returns true. Hence it will print 'healthy weight' although the person is overweight.
You can resolve this by the following way:
ELSEIF( [BMI]>18.5 AND [BMI]<=24.9) THEN
'Healthy weight'
ELSEIF ( [BMI]>24.9 AND [BMI]<=29.9) THEN
'Overweight'
And so on.
This is the best method by which you can get the correct results.
Edit:Xupicor was way too quick for me
regards,
Raghav
This post has been edited by raghav.naganathan: 20 November 2012 - 02:54 AM
#4
Re: Struggling with If statement
Posted 20 November 2012 - 03:02 AM
If i change it to be:
IF ( [BMI] >'18.5' AND [BMI] <='24.9' ) THEN
'Healthy weight'
IF ( [BMI] >'24.9' AND BMI <='29.9' ) THEN
'Overweight'
It says:
Error
Evaluation Error. Please check your syntax for errors.
This is the forms help files:
http://forms.logifor..._Statements.htm
Any ideas?
#5
Re: Struggling with If statement
Posted 20 November 2012 - 04:02 AM
#6
Re: Struggling with If statement
Posted 20 November 2012 - 06:20 AM
If you consider languages like C,C++, Java they work the way Xupicor and I have explained, except that the word AND and THEN is not going to be used.
Instead, we use symbols for AND like && and 'THEN' keyword is omitted from the code as it is understood.
regards,
Raghav
#7
Re: Struggling with If statement
Posted 20 November 2012 - 06:42 AM
I am not sure what language this is as it is inside a web based online form solution.
I have tried the suggestions above but it does not allow them.
Can anyone think of anything else i am doing wrong?
#8
Re: Struggling with If statement
Posted 20 November 2012 - 06:50 AM
http://forms.logifor..._Statements.htm
which seems to show all of the commands that are available and how to set up simple calculations.
Can i set up what i need using the above as a guide?
#9
Re: Struggling with If statement
Posted 20 November 2012 - 07:06 AM
IF [BMI] > '39.9' THEN 'Severely Obese' ELSEIF [BMI] > '29.9' THEN 'Obese'
And so on in the reverse order. In that way, you are sure to get the correct results
regards,
Raghav
#10
Re: Struggling with If statement
Posted 20 November 2012 - 07:13 AM
raghav.naganathan, on 20 November 2012 - 07:06 AM, said:
IF [BMI] > '39.9' THEN 'Severely Obese' ELSEIF [BMI] > '29.9' THEN 'Obese'
And so on in the reverse order. In that way, you are sure to get the correct results
regards,
Raghav
Brilliant - this works perfectly - really appreciate your help!!!!
#11
Re: Struggling with If statement
Posted 20 November 2012 - 07:21 AM
simmons, on 20 November 2012 - 11:02 AM, said:
IF ( [BMI] >'18.5' AND [BMI] <='24.9' ) THEN
'Healthy weight'
IF ( [BMI] >'24.9' AND BMI <='29.9' ) THEN
'Overweight'
It says:
Error
Evaluation Error. Please check your syntax for errors.
Maybe the error is that there are no brackets around the second BMI?
#12
Re: Struggling with If statement
Posted 20 November 2012 - 07:29 AM
simmons, on 20 November 2012 - 07:43 PM, said:
raghav.naganathan, on 20 November 2012 - 07:06 AM, said:
IF [BMI] > '39.9' THEN 'Severely Obese' ELSEIF [BMI] > '29.9' THEN 'Obese'
And so on in the reverse order. In that way, you are sure to get the correct results
regards,
Raghav
Brilliant - this works perfectly - really appreciate your help!!!!
Ok, now the reason why this worked perfectly is that here,you are checking conditions in such a way that the BMI>18.5 condition is encountered last.
So, what you are making sure is that even if the number you have provided is greater that 18.5, the first condition to be executed will not leave any doubt by skipping the other conditions.
In other words, the BMI>18.5 condition will only hold true if all the other conditions are false.
regards,
Raghav
#13
Re: Struggling with If statement
Posted 20 November 2012 - 07:32 AM
|
|

New Topic/Question
Reply



MultiQuote





|