12 Replies - 750 Views - Last Post: 20 November 2012 - 07:32 AM Rate Topic: -----

#1 simmons  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-November 12

Struggling with If statement

Posted 20 November 2012 - 02:41 AM

Hi,

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Struggling with If statement

#2 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: Struggling with If statement

Posted 20 November 2012 - 02:49 AM

This is definitely not C nor C++.

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' THEN
And so on.

This post has been edited by Xupicor: 20 November 2012 - 02:49 AM

Was This Post Helpful? 2
  • +
  • -

#3 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Struggling with If statement

Posted 20 November 2012 - 02:52 AM

Ok, I got the problem what you are facing.

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

Was This Post Helpful? 1
  • +
  • -

#4 simmons  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-November 12

Re: Struggling with If statement

Posted 20 November 2012 - 03:02 AM

Thanks for you quick replies both!

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?
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6052
  • View blog
  • Posts: 23,487
  • Joined: 23-August 08

Re: Struggling with If statement

Posted 20 November 2012 - 04:02 AM

Moved to Other Languages
Was This Post Helpful? 0
  • +
  • -

#6 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Struggling with If statement

Posted 20 November 2012 - 06:20 AM

I'm sorry...what language is this?

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
Was This Post Helpful? 0
  • +
  • -

#7 simmons  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-November 12

Re: Struggling with If statement

Posted 20 November 2012 - 06:42 AM

Thanks,

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?
Was This Post Helpful? 0
  • +
  • -

#8 simmons  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-November 12

Re: Struggling with If statement

Posted 20 November 2012 - 06:50 AM

All i have to go on is this:

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?
Was This Post Helpful? 0
  • +
  • -

#9 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Struggling with If statement

Posted 20 November 2012 - 07:06 AM

I have an idea. I would suggest you start from the bottom most of the conditions then go to the topmost.

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
Was This Post Helpful? 1
  • +
  • -

#10 simmons  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-November 12

Re: Struggling with If statement

Posted 20 November 2012 - 07:13 AM

View Postraghav.naganathan, on 20 November 2012 - 07:06 AM, said:

I have an idea. I would suggest you start from the bottom most of the conditions then go to the topmost.

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!!!!
Was This Post Helpful? 0
  • +
  • -

#11 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2100
  • View blog
  • Posts: 3,197
  • Joined: 21-June 11

Re: Struggling with If statement

Posted 20 November 2012 - 07:21 AM

View Postsimmons, on 20 November 2012 - 11:02 AM, said:

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.


Maybe the error is that there are no brackets around the second BMI?
Was This Post Helpful? 0
  • +
  • -

#12 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Struggling with If statement

Posted 20 November 2012 - 07:29 AM

View Postsimmons, on 20 November 2012 - 07:43 PM, said:

View Postraghav.naganathan, on 20 November 2012 - 07:06 AM, said:

I have an idea. I would suggest you start from the bottom most of the conditions then go to the topmost.

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
Was This Post Helpful? 1
  • +
  • -

#13 simmons  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-November 12

Re: Struggling with If statement

Posted 20 November 2012 - 07:32 AM

Thanks for all your help everyone! :bananaman:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1