View previous topic :: View next topic |
Author |
Message |
kingkastle
Joined: 22 May 2008 Posts: 20
|
Posted: Thu May 29, 2008 5:42 pm Post subject: Programing doubts with IF and .AND. |
|
|
Hi,
I found a problem that drive me crazy and i canīt find the way to short it out, here it is:
I created an IF statment and I would like to use .AND. into the logical statment as I show below:
DO i = 1, siz
Diff(i)= Frct(i) - Obs(i)
IF (ABS(Diff(i)) <= Threshold) THEN
x(1)=x(1)+1
ELSE IF (Diff(i) > Threshold) THEN
x(2)=x(2)+1
ELSE IF (Diff(i) < -Threshold) THEN
x(3)=x(3)+1
ELSE IF (ABS(Diff(i)) <= Threshold .AND. ABS(Frct(i)) <= Threshold) THEN !NO ESTA CORRECTO, I should think about
x(4)=x(4)+1
END IF
END DO
The point is: the program just forgets thte statmentment that is after .AND.
My question is: How could I write the statment to match all statments???
Thaqnks in advance |
|
Back to top |
|
|
brucebowler Guest
|
Posted: Thu May 29, 2008 6:24 pm Post subject: Re: Programing doubts with IF and .AND. |
|
|
Because of the first if, the last else if will never be executed (nor will the intervening else if's for that matter) |
|
Back to top |
|
|
kingkastle
Joined: 22 May 2008 Posts: 20
|
Posted: Thu May 29, 2008 6:53 pm Post subject: |
|
|
Sorry, but itīs still not clear to me......, would you mind an expanded explanation???
thanks |
|
Back to top |
|
|
brucebowler Guest
|
Posted: Thu May 29, 2008 7:12 pm Post subject: |
|
|
Consider the case where abs(diff(i)) <= threshold.
In that case, x(1) will be incremented by 1 and control will transfer to the next statement after the endif and NONE of the else statements will be executed.
Depending on what it is you want to do, you might try something like
Code: | if (abs(diff(i)) <= threshold) then
x(1) = x(1) + 1
else if (diff(i) > threshold) then
x(2) = x(2) + 1
else if (diff(i) < -threshold) then
x(3) = x(3) + 1
endif
if (abs(diff(i)) <= threshold .and. abs(frct(i)) <= threshold) then
x(4) = x(4) + 1
endif
|
Note there are LOTS of other ways do to this that may be more efficient, depending on the rest of the code. (those other ways are left as an exercise for the reader) |
|
Back to top |
|
|
kingkastle
Joined: 22 May 2008 Posts: 20
|
Posted: Fri May 30, 2008 11:40 am Post subject: |
|
|
Thanks I got it!!!,
I understood what you was saying in the first post: In the way it was wrote, the last condition will never be executed.
I re-wrote the code and now is working perfectly, here it is:
DO i = 1, siz
Diff(i)= Frct(i) - Obs(i)
IF (ABS(Diff(i)) <= Threshold .AND. ABS(Frct(i)) <= Threshold) THEN
x(4)=x(4)+1
ELSE IF (Diff(i) > Threshold) THEN
x(2)=x(2)+1
ELSE IF (Diff(i) < -Threshold) THEN
x(3)=x(3)+1
ELSE IF (ABS(Diff(i)) <= Threshold) THEN
x(1)=x(1)+1
END IF
END DO
Thanks very much |
|
Back to top |
|
|
|