LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   logical statement in fortran (https://www.linuxquestions.org/questions/programming-9/logical-statement-in-fortran-909745/)

vjramana 10-24-2011 01:29 AM

logical statement in fortran
 
I am trying to group set of data according some condition using FORTRAN code. The code is as below

Code:

gauche = 0.0
trans = 0.0
do i = 1, total_data
!write(*,*) nombor(i), dihedral(i)

if  (  (0 > dihedral(i) < 120) .or. (-120 > dihedral(i) < 0)  )  then

    gauche = gauche +  1
else
    trans = trans  +  1
endif     
end do

write(20,*) "Layer1_seg_total=  ",(gauche+trans)," ","gauche_seg= ",gauche,"trans_seg= trans

But when I compile I get error message as below:

Code:

if  ((0 > dihedral(i) < 120) .or. (-120 > dihedral(i) < 0))  then
                      1
Error: Expected a right parenthesis in expression at (1)
population.f90:42.5:

else
    1
Error: Unexpected ELSE statement at (1)
population.f90:44.4:

endif 
  1
Error: Expecting END DO statement at (1)

I can not trace the error. Can anyone suggest the mistake? NOTE: this is not an assignment

Thanks in advance

Nominal Animal 10-24-2011 05:23 AM

In Fortran (and most other programming languages) comparison operators compare exactly two expressions. You have three expressions with two operators. Although yours is logical, Fortran does not support your form.

The correct way to express condition a > b < c in Fortran is
Code:

(a > b .and. b < c)
Also, I believe you have a logic error in your if clause. Another way to write your current logic is
Code:

if (dihedral(i) > -120 .and. dihedral(i) < 120 .and. dihedral(i) /= 0) then
which somehow I think does not match your intent. I could be wrong, of course.

ajschaeffer 10-24-2011 05:33 AM

Alternatively, you could use:
Code:

if ( abs(dihedral(i))<120. .and. dihedral(i)/=0. ) then

vjramana 10-24-2011 06:02 AM

Thank you.
It works now.
Regards


All times are GMT -5. The time now is 06:01 AM.