Java if-else question

EvilManagedCare

Senior member
Nov 6, 2004
324
0
0
Code attached below (sorry couldn't get it in the body of the message where I wanted it).

I missed this question on an exam for my intro to Java class I had last week. The class doesn't meet for a few days yet and it's driving me crazy as to why I got it wrong.

Anyway, I said the output should have been:
The porridge is too hot!
The porridge is just right!

The correct answer is just: The porridge is too hot!

Evidently my logic is flawed on how if-else statements work. The way I thought it through was the first test condition being true (temp was greater than 90). Since it was true, the subsequent else statements would thus not be executed.

The flow of the program, at least in my mind, goes to the next line after the else statements, which is:
if ( temp == 80 ) System.out.println( "This porridge is just right!" );

By the time this line is reached, the value of temp, is equal to 80, so why wouldn't this output be printed as well? Once one if statement has been tested true, are all subsequent if statements skipped?

I thought I was right and that the instructor had made a mistake, but maybe I overlooked a brace somewhere.

Thanks in advance for the help.
 

mundane

Diamond Member
Jun 7, 2002
5,603
8
81
I think you're looking for else if, not just nesting an if inside of an else statement.
 

EvilManagedCare

Senior member
Nov 6, 2004
324
0
0
Originally posted by: Argo
I wish people would stop posting their homework assignments here.

I wish people would post something relevant to the topic. Argo, obviously did not read the text in full of the original post. This is from an exam I took on 4/3/06 not a homework assignment. Certainly after having lurked on this forum as long as I have I know better than to ask for help with homework.

Not every post with programming questions are for the cheap answer to homework. Not everybody posting here is looking to get their grade through someone else's knowledge. Of course, you cannot tell if I am being truthful, but you'll have to have faith I am. If you don't want to help, save your observations and move to another thread. Sorry you're so jaded and cynical that you must assume others are looking to take advantage of your delicate genius, Argo.

That said, thank you diegoalalcatraz, BTW, for your response.
 

Argo

Lifer
Apr 8, 2000
10,045
0
0
Originally posted by: EvilManagedCare
Originally posted by: Argo
I wish people would stop posting their homework assignments here.

I wish people would post something relevant to the topic. Argo, obviously did not read the text in full of the original post. This is from an exam I took on 4/3/06 not a homework assignment. Certainly after having lurked on this forum as long as I have I know better than to ask for help with homework.

Not every post with programming questions are for the cheap answer to homework. Not everybody posting here is looking to get their grade through someone else's knowledge. Of course, you cannot tell if I am being truthful, but you'll have to have faith I am. If you don't want to help, save your observations and move to another thread. Sorry you're so jaded and cynical that you must assume others are looking to take advantage of your delicate genius, Argo.

That said, thank you diegoalalcatraz, BTW, for your response.

I appologize, I missed that part. That being said 9/10 programming questions posted on these forums are homework assignments where it's obvious that the person spent abosultely no time thinking about it on their own, so it's understandable why one would get upset.
 

EvilManagedCare

Senior member
Nov 6, 2004
324
0
0
I understand, Argo, and appreciate your apology. I'm sorry I flew off the handle in my response. There have even been some posts that a novice programmer such as myself can tell are obvious attempts at getting homework done. It is equally frustrating because I know how hard I work on my own assignments with no help. I would have tested this on my own compiler at home, but since I'm at work and don't have administrative privileges, I can't install the latest Java SDK or compiler so I couldn't get the answer on my own at the office.

What had thrown me off in the attached is the code following the final else-if statements. I had never seen code inserted between the else and if in an else-if chain (like I said, I'm a novice :) ) except maybe following the last else statement not followed by an if. The examples I have to go from are from the text book, which obviously cannot illustrate each and every contingency.

Most examples had been:

if ( ... )
{ ... }
else
if( ... )

I had yet to see or neglected to see:
if ( ... )
else
{ ... }
if ( ... )
 

mundane

Diamond Member
Jun 7, 2002
5,603
8
81
I see your question now. Are you sure the code snippet is accurate? If it is, I don't see why your answer is incorrect. I suspect the professor's intent was to have an if temp > 150, else if temp < 70, else if temp == 80.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Your professor is right. I jsut compiled it and ran it and it comes back with your professor's answer.

I let visual studio fix the tabbing for you so the code is formatted better, and it's obvious then. The "if ( temp == 80 )" is inside the big "else" block. See the code below.

This would have been really easy if your professor gave you code that was formatted properly.

 

EvilManagedCare

Senior member
Nov 6, 2004
324
0
0
Damn, as I suspected, an overlooked brace! Thanks for doing that, notfred. I guess I've learned the importance of being able to read others' code especially when it's not formatted the way I would.