# Fortran If help

• Fortran

## Main Question or Discussion Point

Hi, so I'm trying to use the following if statement:

Fortran:
if (sum(i,j) <=1.0 ) then
sidash=(si)*(Death)
else if (1 <= sum(i,j) <= 2) then
sidash=(si)*((sqrt(2.0)+1.0)*(2.0-sum(i,j))*(Death)+(sum(i,j)-1.0)*(Survive))
else if (sum(i,j)<=3 .and. sum(i,j)=>2) then
sidash=(si)*((sqrt(2.0)+1.0)*(3.0-sum(i,j))*(Survive)+(sum(i,j)-2.0)*(Birth))
else if (sum(i,j)<=4 .and. sum(i,j)=>3) then
sidash=(si)*((sqrt(2.0)+1.0)*(4.0-sum(i,j))*(Birth)+(sum(i,j)-3.0)*(Death))
else
sidash=(si)*(Death)
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:

Related Programming and Computer Science News on Phys.org
Mark44
Mentor
Hi, so I'm trying to use the following if statement:

if (sum(i,j) <=1.0 ) then
sidash=(si)*(Death)
else if (1 <= sum(i,j) <= 2) then
sidash=(si)*((sqrt(2.0)+1.0)*(2.0-sum(i,j))*(Death)+(sum(i,j)-1.0)*(Survive))
else if (sum(i,j)<=3 .and. sum(i,j)=>2) then
sidash=(si)*((sqrt(2.0)+1.0)*(3.0-sum(i,j))*(Survive)+(sum(i,j)-2.0)*(Birth))
else if (sum(i,j)<=4 .and. sum(i,j)=>3) then
sidash=(si)*((sqrt(2.0)+1.0)*(4.0-sum(i,j))*(Birth)+(sum(i,j)-3.0)*(Death))
else
sidash=(si)*(Death)
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?
I don't believe this is legal:
Code:
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:
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:
else if (1 <= sum(i,j) .and. sum(i, j) < 2) then

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.

FactChecker