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

In summary, the code needs to be written like this:IF ( temp > 103 ) THENWRITE (*, *) NTemperature dangerously highNELSE IF ( temp > 99.5) THENWRITE (*, *) NTemperature slightly highNELSE IF ( temp > 97.5) THENWRITE (*, *) NTemperature normalNELSE IF ( temp < 97.5) THENWRITE (*, *) NTemperature belwo highN
  • #1
rk2ray
9
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
  • #2
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.
 
  • #3
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
 
  • #4
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:
  • #5
thank you borg. I am learning about Fortran. I'm a rookie here.

But your tip really helps.
 
  • #6
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?
 
  • #7
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"
 

What is FORTRAN code error?

FORTRAN code error refers to a mistake or bug in a program written in the FORTRAN programming language. This can cause the program to not function as intended or to produce incorrect results.

What are the common types of FORTRAN code errors?

Some common types of FORTRAN code errors include syntax errors, logic errors, and runtime errors. Syntax errors occur when the code is not written according to the proper FORTRAN syntax. Logic errors occur when the code does not produce the desired output due to incorrect logic or algorithm. Runtime errors occur when the program encounters an unexpected problem while it is running.

How can I debug a FORTRAN code error?

The first step in debugging a FORTRAN code error is to carefully review the code and look for any obvious mistakes. Next, you can use tools such as a debugger or a code analyzer to identify and fix the error. It can also be helpful to use print statements to track the values of variables and identify where the error is occurring.

What are some best practices for avoiding FORTRAN code errors?

To avoid FORTRAN code errors, it is important to follow good coding practices such as using descriptive variable names, commenting your code, and testing your code frequently. It can also be helpful to break your code into smaller, manageable chunks and to use functions or subroutines to improve code organization.

Where can I get help with a FORTRAN code error?

If you are struggling to fix a FORTRAN code error, you can reach out to online forums, communities, or programming groups for assistance. You can also consult the FORTRAN language documentation or seek help from a colleague or mentor who is familiar with FORTRAN programming.

Similar threads

  • Programming and Computer Science
Replies
4
Views
617
Replies
11
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
3
Replies
75
Views
4K
Back
Top