If and elseif and else syntax

Reply

Join Date: Jun 2009
Posts: 38
Reputation: Jintu is an unknown quantity at this point 
Solved Threads: 0
Jintu Jintu is offline Offline
Light Poster

If and elseif and else syntax

 
0
  #1
Jul 8th, 2009
Hello all,

I have to write a program that calculate a bmi of person whose weight is 170 pounds (it increases by 1 pound for each month for 12 months) and 70 inches, after I have all the bmi for each weight from 170 - 182 pounds, under each weight I need to indicate if his bmi is underweight, normal, overweight or obese. I have written the program using php script, but it does not seem to work correctly, could anyone shed some light on this. The program code is:
  1. <html>
  2. <head>
  3. <title>Lab 10a</title>
  4. </head>
  5. <body>
  6. <?php
  7. $pounds = 170;
  8. $inches = 70;
  9. $increase = 0;
  10. $poundsinch = 703;
  11. while ($increase < 13)
  12. {
  13. $division = ($pounds) / ($inches * $inches) * ($poundsinch);
  14. echo "The BMI of $pounds pounds and $inches inches is : ".$division."<br />";
  15. echo " ";
  16. $increase = $increase + 1;
  17. $pounds = $pounds + 1;
  18. if ($division < 18.5)
  19. echo "You are underweight";
  20. elseif ($division < 25){
  21. echo "Your BMI is normal";
  22. elseif ($division < 29.9)
  23. echo "You are overweight";
  24. else{
  25. echo "You are obese";
  26.  
  27. }
  28.  
  29.  
  30.  
  31. ?>
  32.  
  33. </body>
  34. </html>

Thanks in advance,
Tony
Last edited by peter_budo; Jul 10th, 2009 at 6:41 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 152
Reputation: kireol is an unknown quantity at this point 
Solved Threads: 19
kireol kireol is offline Offline
Junior Poster

Re: If and elseif and else syntax

 
0
  #2
Jul 8th, 2009
I assume this is homework.


Part of learning to program is to put a bit more thought into things.

Not so good: It's not working. It doesnt work right. It's broke.

Good: I was expecting to see X and I see Y. What is the command to ____.


When you start to think this way, and post this way, your errors will sometimes become apparent to you, and you won't need as much help.
Last edited by kireol; Jul 8th, 2009 at 10:43 pm.
Why do Daniweb moderators edit CODE tags into CODE=style but don't check "solved".
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 51
Reputation: tom curren is an unknown quantity at this point 
Solved Threads: 3
tom curren's Avatar
tom curren tom curren is offline Offline
Unverified User

Re: If and elseif and else syntax

 
0
  #3
Jul 8th, 2009
Oh great. Now I have to google BMI and wade through corporate references or risk watching my intelligence decline.

Got it... BMI

Awful lot of science for something that within 15% is irrelevant.

Spending two seconds and only reading up to the first line of interest, the calculation of $DIVISION, I would start with looking up operator precedence in PHP and probably adding some parens.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 51
Reputation: tom curren is an unknown quantity at this point 
Solved Threads: 3
tom curren's Avatar
tom curren tom curren is offline Offline
Unverified User

Re: If and elseif and else syntax

 
0
  #4
Jul 8th, 2009
oh yes, and for heaven's sake, indent your code.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 165
Reputation: Fest3er is an unknown quantity at this point 
Solved Threads: 18
Fest3er Fest3er is offline Offline
Junior Poster

Re: If and elseif and else syntax

 
0
  #5
Jul 9th, 2009
To provide a useful answer:
  • use [code] and [/code] tags around your code so it is displayed on the forum in a readable fashion
  • use the "Preview Post" button to review your post before submitting it
  • be sure your code is properly indented
  • always use {} pairs around code blocks (while loops, if/else statements, etc.) even if there is only one statement in the block
  • verify that the braces are properly paired.
Your braces are neither balanced nor paired.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 28
Reputation: jcacquiescent27 is an unknown quantity at this point 
Solved Threads: 8
jcacquiescent27 jcacquiescent27 is offline Offline
Light Poster

Re: If and elseif and else syntax

 
0
  #6
Jul 9th, 2009
This is simply a matter of missing curly braces. Your script works as expected once they are all in there. Following Fest3er's advice will get you far.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 16
Reputation: dalefish is an unknown quantity at this point 
Solved Threads: 0
dalefish dalefish is offline Offline
Newbie Poster

Re: If and elseif and else syntax

 
0
  #7
Jul 9th, 2009
As was stated above, the clearer the question, ther clearer that answer. However, thought I'd help the lad/ladette out....
  1. <?php
  2. $pounds = 170;
  3. $inches = 70;
  4. $increase = 0;
  5. $poundsinch = 703;
  6. while ($increase < 13){
  7. $division = number_format(($pounds) / ($inches * $inches) * ($poundsinch),2);
  8. echo "The BMI of $pounds pounds and $inches inches is : ".$division." ";
  9. if ($division < 18.5){
  10. echo "You are underweight.<br />";
  11. }
  12. elseif ($division < 25){
  13. echo "Your BMI is normal.<br />";
  14. }
  15. elseif ($division < 29.9){
  16. echo "You are overweight.<br />";
  17. }
  18. else{
  19. echo "You are obese.<br />";
  20. }
  21. $increase++;
  22. $pounds++;
  23. }
  24. ?>
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC