Fortran Why Is My FORTRAN If/Else Statement Not Working Correctly?

AI Thread Summary
The discussion centers on a Fortran code snippet that evaluates temperature ranges but fails to account for the exact value of 97.5 degrees. The initial code structure leads to a scenario where no output occurs if the temperature is exactly 97.5. A suggested solution involves rearranging the conditions to prioritize the higher temperature thresholds first, ensuring all cases are covered. Additionally, it is noted that the WRITE statements need proper quotation marks around the text to be printed. Participants emphasize the importance of testing the code with boundary values to verify that all conditions are met. The conversation also highlights the significance of formatting code for readability, especially in larger programs.
rk2ray
Messages
7
Reaction score
0
I am not able to get what's wrong in below.


IF ( temp < 97.5 ) THEN
WRITE (*, *) NTemperature below normalN
ELSE IF ( temp > 97.5) THEN
WRITE (*, *) NTemperature normalN
ELSE IF ( temp > 99.5) THEN
WRITE (*, *) NTemperature slightly highN
ELSE IF ( temp > 103.0) THEN
WRITE (*, *) NTemperature dangerously highN
END IF

Thanks.
 
Technology news on Phys.org
rk2ray said:
I am not able to get what's wrong in below.

Code:
IF ( temp < 97.5 ) THEN
    WRITE (*, *) NTemperature below normalN
ELSE IF ( temp > 97.5) THEN
    WRITE (*, *) NTemperature normalN
ELSE IF ( temp > 99.5) THEN
    WRITE (*, *) NTemperature slightly highN
ELSE IF ( temp > 103.0) THEN
    WRITE (*, *) NTemperature dangerously highN
END IF

Thanks.
The first two cases cover everything except when the temperature is exactly 97.5 so it can never reach the other cases where the temp is something else. Put the 103 case first followed by the 99.5 case.
 
So it should be...the following code..am i right?

IF ( temp > 103 ) THEN
WRITE (*, *) NTemperature dangerously highN
ELSE IF ( temp > 99.5) THEN
WRITE (*, *) NTemperature slightly highN
ELSE IF ( temp > 97.5) THEN
WRITE (*, *) NTemperature normalN
ELSE IF ( temp < 97.5) THEN
WRITE (*, *) NTemperature belwo highN
END IF
 
What do you think? Is it possible to reach every case with the right starting numbers? Try numbers that are equal to, just above, and just below each of your cases and see if it's possible to reach every case. The way that it's written, nothing will ever print if the temp is exactly 97.5. Can you think of how to fix that? The code is correct (except for the 97.5 problem) but you should always test like this.

BTW, please use code blocks and indentation when posting code. This one is small enough but larger programs can be difficult to read otherwise. And, welcome to Physics Forums. :smile:
 
Last edited:
thank you borg. I am learning about Fortran. I'm a rookie here.

But your tip really helps.
 
rk2ray said:
thank you borg. I am learning about Fortran. I'm a rookie here.

But your tip really helps.
Glad to help. I'm not a Fortran programmer but If/Else is universal in coding.

Note: I'm only addressing the If/Else problem. Since I don't know Fortran, I have no idea if the WRITE statements are correct. They look odd to me but I guess that's how they're written?
 
Borg said:
Note: I'm only addressing the If/Else problem. Since I don't know Fortran, I have no idea if the WRITE statements are correct. They look odd to me but I guess that's how they're written?
They need quotes around the text that is to be printed.

For example, instead of this:
Code:
WRITE (*, *) NTemperature dangerously highN

do this:
Code:
WRITE (*, *) "Temperature dangerously high"
 

Similar threads

Back
Top