Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortran Fortran If help

  1. Mar 12, 2015 #1
    Hi, so I'm trying to use the following if statement:

    Code (Fortran):
    if (sum(i,j) <=1.0 ) then
      else if (1 <= sum(i,j) <= 2) then
    else if (sum(i,j)<=3 .and. sum(i,j)=>2) then
      else if (sum(i,j)<=4 .and. sum(i,j)=>3) then
    end if
    Where sum(i,j) is an integer array and so is death, survive, birthm si and sidash.

    However I get errors saying that I cannot use <= and .and. logical statements comparing integer expressions.

    I need to compare sum(i,j) in these regions of numbers so can anyone help me out with this?
    Last edited by a moderator: Apr 9, 2015
  2. jcsd
  3. Mar 12, 2015 #2


    Staff: Mentor

    I don't believe this is legal:
    Code (Text):
    else if (1 <= sum(i,j) <= 2) then
    That's more of a mathematical notation than one used in programming.
    It should be like this:
    Code (Text):
    else if (1 <= sum(i,j) .and. sum(i, j) <= 2) then
    BTW, your separate cases are not distinct. If sum(i, j) == 2, then you will have a match in the first else if clause and in the second else if clause.

    A better way would be like this:
    Code (Text):
    else if (1 <= sum(i,j) .and. sum(i, j) < 2) then
  4. Mar 13, 2015 #3
    It would be nice if your "sum" array was named more clearly to indicate the sum of what; you should develop a better variable naming style...clearer but not too verbose. Not to mention that "sum()" is an intrinsic function from Fortran90 and up.
  5. Mar 13, 2015 #4


    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    Going back to the early, most conservative FORTRAN syntax, try something like ((sum(i,j) .le. 4 ).and. (sum(i,j) .ge. 3)). Don't be cheap with your parentheses, they don't cost anything. Also, try to make note of which line of code the first error statement comes from. The first error often dominoes to make other false error statements in later code. So try to solve the first one and the others might disappear.

    Once you get the old syntax working, you can test more modern syntax, step-by-step.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook