forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Programing doubts with IF and .AND.

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General
View previous topic :: View next topic  
Author Message
kingkastle



Joined: 22 May 2008
Posts: 20

PostPosted: Thu May 29, 2008 5:42 pm    Post subject: Programing doubts with IF and .AND. Reply with quote

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
View user's profile Send private message Send e-mail
brucebowler
Guest





PostPosted: Thu May 29, 2008 6:24 pm    Post subject: Re: Programing doubts with IF and .AND. Reply with quote

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

PostPosted: Thu May 29, 2008 6:53 pm    Post subject: Reply with quote

Sorry, but itīs still not clear to me......, would you mind an expanded explanation???
thanks
Back to top
View user's profile Send private message Send e-mail
brucebowler
Guest





PostPosted: Thu May 29, 2008 7:12 pm    Post subject: Reply with quote

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

PostPosted: Fri May 30, 2008 11:40 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group