- 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.
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.
