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

  • Context: Fortran 
  • Thread starter Thread starter rk2ray
  • Start date Start date
  • Tags Tags
    Code Error Fortran
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a FORTRAN If/Else statement that is not functioning as intended. Participants explore the logic of the code, particularly in relation to temperature thresholds and output statements. The focus is on identifying issues within the conditional structure and ensuring all possible temperature values are accounted for.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Conceptual clarification

Main Points Raised

  • One participant notes that the original code fails to handle the case when the temperature is exactly 97.5, suggesting that this prevents reaching other conditions.
  • Another participant proposes a revised order for the If/Else statements, placing the higher temperature checks first to ensure all cases are covered.
  • A later reply emphasizes the importance of testing the code with boundary values to confirm that all cases are reachable.
  • One participant points out that the WRITE statements in the code may require quotes around the text to be printed, indicating a potential syntax issue.

Areas of Agreement / Disagreement

Participants generally agree that the original code has a flaw regarding the handling of the temperature value of 97.5. However, there is no consensus on the correctness of the WRITE statements, as some participants express uncertainty about their syntax.

Contextual Notes

Limitations include potential misunderstandings of FORTRAN syntax and the specific requirements for the WRITE statements, which remain unresolved in the discussion.

Who May Find This Useful

This discussion may be useful for individuals learning FORTRAN programming, particularly those interested in control structures and debugging code related to conditional statements.

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

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
11
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
9K