Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Tricky Fortran if-statement

Tricky Fortran if-statement

Tricky Fortran if-statement


I am trying to translate Fortran to C++, and I encountered a Fortran line I can't understand:

IF(DEC-1.) 1,2,2

1 <calculations on label 1>

2 <calculations on label 2 >

My problem is the IF statement. Obviously it goes to label 1 or 2 depending on the outcome of DEC-1. But how does it work? What is the logic?

It is a rounding function. I don't want to paste in the very code since it is not mine.
For example, if NUMBER is 39.99 and DEC 1.0, it goes to 2.
Thankful for any help

RE: Tricky Fortran if-statement

It is an arithmetic if statement. Goes to first label if < 0, second label if = 0, third label if > 0. Comes from the old (circa 1954) IBM instruction set. In your case it is basically


if ((DEC - 1.) < 0)
    calculations on label 1
    calculations on label 2
That is provided calculations on label1 don't fall into calculations on label 2


if (DEC-1) 1, 2, 2
1    do something and fall into 2
2    do something


      if (DEC-1) 1, 2, 2
2     do something and fall into 1
1     do something 

RE: Tricky Fortran if-statement

Hi xwb
Thank you for splendid answer! And no, it doesn't fall through since it does RETURN before next label.
I suspected this was REALLY old stuff... :-\
Thanks again!

RE: Tricky Fortran if-statement

Fortran, unlike C, does not have keywords. I have seen a common block, and array called if. You may come across something weird like


program main
      integer if(20)
      if(1) = 10
      if(2) = 20
      if (if(1).gt. 5) if (if(2)) 10, 20, 30
      print *, "Less than 5"
10    print *, "Less than 0"
20    print *, "Equal to 0"
30    print *, "Greater than 0"

RE: Tricky Fortran if-statement

Thank you again xwb! Yes that one is even worse, really hope something like it never comes up.
Seems there is an old math functions library I have here, where we have this old stuff...

RE: Tricky Fortran if-statement

The function finishes like this:


It should be (meaning it is interpreted as):

No 'N' of type 'ROUND', as it seems.
Don't understand how things like this even compile... :-\

RE: Tricky Fortran if-statement

Spaces are allowed in fortran identifiers. Can lead to errors


DO 10 I = 1, 5, 1   ! start of a do loop
DO 20 J = 1, 5      ! start of a do loop
! Problem is the , is next to the . on the keyboard so we can get
DO 30 K = 1.5       ! assignment  DO30K = 1.5

! Sometimes you can stare at that for ages and wonder why the loop doesn't execute
! The above can be avoided if the increment is always added
DO 40 L = 1.5,1     ! compilation error
DO 50 M = 1,5.1     ! compilation error

! Alternatively, use the modern do loop
DO I = 1.5  ! Assignment
ENDDO ! this will get a compilation error - no matching DO 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close