# Struggling with If statement

Page 1 of 1

## 12 Replies - 1112 Views - Last Post: 20 November 2012 - 07:32 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=300942&amp;s=eab2f42e29fd9d271e5d439e47395830&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 simmons

• New D.I.C Head

Reputation: 0
• 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

• Nasal Demon

Reputation: 457
• Posts: 1,179
• 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

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• 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

• New D.I.C Head

Reputation: 0
• 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

• Saucy!

Reputation: 6246
• Posts: 24,014
• 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

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• 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

• New D.I.C Head

Reputation: 0
• 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

• New D.I.C Head

Reputation: 0
• 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

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• 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

• New D.I.C Head

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

## Re: Struggling with If statement

Posted 20 November 2012 - 07:13 AM

raghav.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

• D.I.C Lover

Reputation: 2579
• Posts: 4,123
• Joined: 21-June 11

## Re: Struggling with If statement

Posted 20 November 2012 - 07:21 AM

simmons, 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

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• Joined: 14-September 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:

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

• New D.I.C Head

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

## Re: Struggling with If statement

Posted 20 November 2012 - 07:32 AM

Thanks for all your help everyone!
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }