0

I am planning to make a BMI calculation in which this program will prompt the user to enter new inputs repetitively as long as both inputs are positive. In this case, my input is weight and height. I have applied while loop in my code. However, when I enter two positive inputs and after the BMI calculation has been performed, the process ends and exits. May I know which line did I code wrongly?

while 1

if (Weight > 0 && Height > 0)
Weight = input('Please Enter Your Weight In kg: ');
Height = input('Please Enter Your Height In m : ');
    
    BMI = Weight/(Height^2);

    if (BMI<=18.5)
    disp(['Health condition: THIN. Your BMI is ', num2str(BMI)]);
    

    elseif (BMI>=18.6) && (BMI<=24.9)
    disp(['Health condition: HEALTHY. Your BMI is ', num2str(BMI)]);
    

    elseif (BMI<=25) && (BMI<=29.9)
    disp(['Health condition: OVERWEIGHT. Your BMI is ', num2str(BMI)]);
    

    else (BMI>=30)
    disp(['Health condition: OBESE. Your BMI is ', num2str(BMI)]);
    
    end
 
        break
    end
end

Thank you.

1 Answer 1

1

First, you can't call the if statement if weight and height conditions have not been defined yet with the prompt. So you should call input() before if:

Weight = input('Please Enter Your Weight In kg: ');
Height = input('Please Enter Your Height In m : ');
if (Weight > 0 && Height > 0)

Now, you can run your code. Your problem is thebreak. This is how you defined your code:

while loop 1 (forever)
    prompt asking for weight and height
    if weight & height > 0 (positive)
        (...some other calculations and if)
    break

If you check this pseudo-code, you are telling your code that if weight and height are positive, break the while loop, therefore it stops. The right condition should be:

if weight & height > 0 (positive)
    run...
else (not positive)
    break

This is your code fixed:

while 1

    Weight = input('Please Enter Your Weight In kg: ');
    Height = input('Please Enter Your Height In m : ');
    if (Weight > 0 && Height > 0)
        
        BMI = Weight/(Height^2);
    
        if (BMI<=18.5)
        disp(['Health condition: THIN. Your BMI is ', num2str(BMI)]);
        
    
        elseif (BMI>=18.6) && (BMI<=24.9)
        disp(['Health condition: HEALTHY. Your BMI is ', num2str(BMI)]);
        
    
        elseif (BMI<=25) && (BMI<=29.9)
        disp(['Health condition: OVERWEIGHT. Your BMI is ', num2str(BMI)]);
        
    
        else (BMI>=30)
        disp(['Health condition: OBESE. Your BMI is ', num2str(BMI)]);
        
        end
        
    else
        break
    end
end

Try to take always care of the identation. Your first if and its final end wasn't equally.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.