if statements with eqv/and

Steve Kargl sgk@troutmask.apl.washington.edu
Sun Feb 11 18:22:00 GMT 2007


On Sun, Feb 11, 2007 at 11:40:40AM -0600, MILAD FATENEJAD wrote:
> Hello:
> I had a question about if statements involving .eqv. and .and..
> I noticed that the following code does not run as I would expect...
> 
> program test
>   implicit none
> 
>   logical :: boolean = .false.
>   integer :: num = 3
>   integer :: int = 2
>  
>   if(boolean .eqv. .TRUE. .AND. num == 2 ) then
>      print *, "Hello World"   
>   end if
> 
> end program test
> 
> If I run this program it prints "Hello World", however, if I change
> "boolean .eqv. .TRUE." to "int == 2" doesn't print anything.
> 
> I would like it to interpret the if as:
> 
> if( (boolean .eqn. .true.) .and. (num == 2) )
> 

Then you'll need to insert the parentheses.

Section 7.3 of the Fortran 2003 standard gives the precedence
the operators.  Without the parentheses in you original 
logical expression you have the following precedence "==",
".and.", and finally ".eqv.", so

(boolean .eqv. .TRUE. .AND. num == 2)

evaluates as (boolean .eqv. (.TRUE. .AND. (num == 2)))

-- 
Steve



More information about the Fortran mailing list