Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: if else if elese if.... statement not working

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default if else if elese if.... statement not working

    I have the code below in a class, when I call the method e.g. patient1.monitorBloodPressure and if the blood pressure is set to for example 139 it will still read Blood Pressure level normal it will not go down the if else if ladder, am I blocking the code or have I written it in the wrong order?

    	public void monitorBloodPressure() {
    			if (bloodPressure<120)
    			{
    				System.out.println("Blood Pressure Level Normal");
    			}
    			else if (bloodPressure>=120||bloodPressure<=139)
    			{
    				System.out.println("HEALTH WARNING - High Blood Pressure - Prehypertension");
    			}
    			else if (bloodPressure>=140||bloodPressure<=159)
    			{
    				System.out.println("HEALTH WARNING - High Blood Pressure - Hypertension Stage 1");
    			}
    			else if (bloodPressure>=160||bloodPressure<=179)
    			{
    				System.out.println("HEALTH WARNING - High Blood Pressure - Hypertension Stage 2");
    			}
    			else if (bloodPressure>=180)
    			{
    				System.out.println("HEALTH WARNING - High Blood Pressure - Hypertensive Crisis - Emergency Care Needed");
    			}
    			else
    			{
    				System.out.println("Error");
    			}
    	}

    Thanks


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: if else if elese if.... statement not working

    Nothing appears to be wrong. Print out the value of bloodPressure in the beginning of your method. I'd guess that you didn't set it correctly.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: if else if elese if.... statement not working

    Posting an SSCCE will illustrate your problem better. As is, my advice would be to print out the value of the variable 'bloodPressure' at the beginning of the method, then run the code to verify the value is what you expect.

    Edit: too slow, aussiemcgr beat me to it

  4. #4
    Junior Member
    Join Date
    Oct 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: if else if elese if.... statement not working

    i tried putting:

    System.out.println(bloodPressure);

    this returns the correct value but it outputs HEALTH WARNING - High Blood Pressure - Prehypertension

    instead of going to:

    else if (bloodPressure>=180)
    			{
    				System.out.println("HEALTH WARNING - High Blood Pressure - Hypertensive Crisis - Emergency Care Needed");


    --- Update ---

    I fixed it it doesnt work with || (or) operators I changed them to && and it worked, but shouldn't it work with OR?

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: if else if elese if.... statement not working

    Quote Originally Posted by ahans View Post
    I fixed it it doesnt work with || (or) operators I changed them to && and it worked, but shouldn't it work with OR?
    No. if-else statements will enter the first block which returns true. In the case of the OR operation, if the first condition is true, the second condition is not even checked. For AND operations, both conditions are always checked.
    So if you entered a blood pressure of 170 with your OR operations, this is what would happen:
    if(bloodPressure<120)? - No, next statement
    if(bloodPressure>=120)? - Yes, enter if block and print out statement
    If you did the same with AND operations instead of OR operations:
    if(bloodPressure<120)? - No, next statement
    if(bloodPressure>=120)? - Yes, continue evaluating
    	if(bloodPressure<=139)? - No, next statement
    if(bloodPressure>=140)? - Yes, continue evaluating
    	if(bloodPressure<=159)? - No, next statement
    if(bloodPressure>=160)? - Yes, continue evaluating
    	if(bloodPressure<=179)? - Yes, enter if block and print out statement
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. java awt...if statement not working properly
    By edward005 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 28th, 2013, 02:57 AM
  2. Replies: 2
    Last Post: June 25th, 2013, 06:33 AM
  3. IF statement accumulataion not working properly - random numbers
    By StrugglerWithJava in forum Loops & Control Statements
    Replies: 1
    Last Post: April 4th, 2013, 07:48 AM
  4. switch statement not working for JFrame Calculator
    By kaddyshack in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2011, 10:35 AM
  5. if else statement not working properly
    By tina G in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 29th, 2010, 08:04 AM