question on IF-ELSE condition

This is the development of a 2nd Edition of Enders' RATS Programming Manual.

Moderator: TomDoan

hardmann
Posts: 246
Joined: Sat Feb 26, 2011 9:49 pm

question on IF-ELSE condition

Unread post by hardmann »

Dear Tom:

I encountered a if-else problem. I copy code from RATS Programming Manual(2ed), p153.
It works, the code below the asterisk line is not executed.

Code: Select all

do rep=1,100
compute r=%ran(1.0)
if r<-2.0
disp "Big Negative value" r
else
if r<2.0
disp "Between -2 and 2" r
else
disp "Big Positive value" r
end do rep

disp "************"
* compute r = 0.56577
if r<-2.0
disp "Big Negative value" r
else
if r<2.0
disp "Between -2 and 2" r
else
disp "Big Positive value" r
I do not know why.

Best Regard
Hardmann
varcabic
Posts: 6
Joined: Thu Oct 10, 2013 5:34 am

Re: question on IF-ELSE condition

Unread post by varcabic »

The first part of your code is inside the loop, so if-else works without parenthesis. The other part, after ****** is outside the loop. So, in order to work, if-else condition should be inside the parenthesis. Thy this:

disp "************"
* compute r = 0.56577
{
if r<-2.0
disp "Big Negative value" r
else
if r<2.0
disp "Between -2 and 2" r
else
disp "Big Positive value" r
}
hardmann
Posts: 246
Joined: Sat Feb 26, 2011 9:49 pm

Re: question on IF-ELSE condition

Unread post by hardmann »

Dear varcabic:

Thanks.
I want to know why parentheses are added?


best regard
Hardmann
TomDoan
Posts: 7731
Joined: Wed Nov 01, 2006 4:36 pm

Re: question on IF-ELSE condition

Unread post by TomDoan »

An IF-ELSE block outside of a more complicated program segment (your first example was inside a DO loop) requires something to indicate when you are done with the compiler logic. That's either by including the whole segment in {...} or by using an END IF.

https://estima.com/webhelp/topics/compiledsections.html
hardmann
Posts: 246
Joined: Sat Feb 26, 2011 9:49 pm

Re: question on IF-ELSE condition

Unread post by hardmann »

Dear Tom:
Thanks. I completely understand. In other programming languages, the integrity of the If-else-end if block is emphasized, and I thought Rats had an internal mechanism for completion.

Best Regard
Hardmann
Post Reply