QB / QB64 Discussion Forum      Other Subforums, Links and Downloads
 

 Return to Index  

A problem

November 13 2010 at 6:56 PM
  (Login Mikrondel)
R


Response to BMI program help

 

In the following block of code:

IF bodyMassIndex <= 16.49 THEN PRINT "Severely underweight, or anorexic"
IF (bodyMassIndex >= 16.5) AND (bodyMassIndex <= 18.49) THEN PRINT "Underweight"
IF (bodyMassIndex >= 18.5) AND (bodyMassIndex <= 24.99) THEN PRINT "normal"
IF (bodyMassIndex >= 25) AND (bodyMassIndex <= 29.99) THEN PRINT "Overweight"
IF (bodyMassIndex >= 30) AND (bodyMassIndex <= 34.99) THEN PRINT "Obese"
IF (bodyMassIndex >= 35) AND (bodyMassIndex <= 39.99) THEN PRINT "Clinically Obese"
IF bodyMassIndex >= 40 THEN PRINT "Morbidly Obese"

What happens if bodyMassIndex is exactly 16.495? No message gets printed. One solution is to take advantage of the difference between < and <=, like so:

IF bodyMassIndex < 16.5 THEN PRINT "Severely underweight, or anorexic"
IF bodyMassIndex >= 16.5 AND bodyMassIndex < 18.5 THEN PRINT "Underweight"
IF bodyMassIndex >= 18.5 AND bodyMassIndex < 25 THEN PRINT "normal"
'etc...



A neater way to solve the problem would be to use ELSEIFs, like this:

IF bodyMassIndex < 16.5 THEN
    PRINT "Severely underweight, or anorexic"
ELSEIF bodyMassIndex < 18.5 THEN
    PRINT "Underweight"
ELSEIF bodyMassIndex < 25 THEN
    PRINT "normal"
ELSEIF bodyMassIndex < 30 THEN
    PRINT "Overweight"
ELSEIF bodyMassIndex < 35 THEN
    PRINT "Obese"
ELSEIF bodyMassIndex < 40 THEN
    PRINT "Clinically Obese"
ELSE
    PRINT "Morbidly Obese"
END IF

 
 Respond to this message   
Responses

 Copyright © 1999-2014 Network54. All rights reserved.   Terms of Use   Privacy Statement  

Newbies usually go to www.qbasic.com and click on The QBasic Forum
Forum regulars have their own ways, which include The QBasic Community Forums