Fortran Using Integer Expressions in an If Statement: Help Needed!

AI Thread Summary
The discussion centers on troubleshooting an if statement in a programming context, specifically regarding the use of logical comparisons with an integer array. The primary issue arises from the incorrect use of chained comparisons, such as "1 <= sum(i,j) <= 2," which is not valid in programming. Instead, the correct approach involves using logical operators, specifically ".and.," to combine conditions, as in "1 <= sum(i,j) .and. sum(i,j) <= 2." Additionally, there are concerns about overlapping conditions in the if statements, which could lead to ambiguity when sum(i,j) equals certain values. Suggestions include refining the conditions to ensure they are distinct and using clearer variable names for better readability. The discussion also emphasizes the importance of using proper syntax and parentheses to avoid errors, particularly in older programming languages like FORTRAN. Finally, it is recommended to address the first error encountered, as resolving it may eliminate subsequent errors.
Lukejambo
Messages
13
Reaction score
0
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:
Technology news on Phys.org
Lukejambo said:
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.
 
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
6
Views
2K
Replies
2
Views
2K
Replies
4
Views
2K
Replies
2
Views
2K
Replies
12
Views
3K
Replies
8
Views
2K
Replies
6
Views
3K
Replies
6
Views
3K
Back
Top