Using Integer Expressions in an If Statement: Help Needed!

In summary, the programmer is trying to use an if statement to compare sums of integers, but is getting errors. He needs to use a more specific comparison, but does not know how to start.
  • #1
Lukejambo
13
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
  • #2
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
 
  • #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.
 
  • #4
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.
 

Related to Using Integer Expressions in an If Statement: Help Needed!

1. What is an integer expression?

An integer expression is a mathematical expression that evaluates to a whole number, also known as an integer. It can include numbers, variables, and mathematical operators such as addition, subtraction, multiplication, and division.

2. How do I use an integer expression in an if statement?

To use an integer expression in an if statement, you can compare it to a specific value or use logical operators such as greater than or less than. For example, you can use the expression "x + y" in an if statement to check if the sum of two variables is greater than a certain number.

3. Can I use multiple integer expressions in an if statement?

Yes, you can use multiple integer expressions in an if statement by combining them with logical operators such as "&&" (and) or "||" (or). This allows you to create more complex conditions for your if statement to evaluate.

4. What happens if the integer expression in my if statement is not true?

If the integer expression in your if statement is not true, the code within the if statement will not be executed. It will instead move on to the next line of code outside of the if statement. You can also include an else statement to specify what should happen if the if statement is not true.

5. Can I use functions or methods in my integer expressions?

Yes, you can use functions or methods in your integer expressions as long as they return an integer value. For example, you can use the function "Math.sqrt(x)" to find the square root of a number and use it in your integer expression.

Similar threads

  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
12
Views
3K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
6
Views
4K
  • Programming and Computer Science
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
Replies
6
Views
3K
  • Programming and Computer Science
Replies
6
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
Back
Top