Nesting IF-ELSE Statements in a Do Loop

Use this forum to post questions about syntax problems or general programming issues. Questions on implementing a particular aspect of econometrics should go in "Econometrics Issues" below.
Gregory
Posts: 20
Joined: Mon Nov 13, 2006 8:05 am

Nesting IF-ELSE Statements in a Do Loop

Unread post by Gregory »

I can't get the following to work.

* Start of the main loop

do i=1,10

* Now I want to do something conditional on iteration i.

if i>1

{
do j=1,n
set residSeries(j) gEnd+i-1 gEnd+i-1 = rv(gEnd+i-1)(j)
end do
}

else

{
do k=gStart,gEnd
do j=1,n
set residSeries(j) k k = rv(k)(j)
end do
end do
}

end if


end do
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Unread post by TomDoan »

You don't need the "end if" when the if-else is inside a loop (or any other compiled structure).

Code: Select all

do k=gStart,gEnd 
do j=1,n 
set residSeries(j) k k = rv(k)(j) 
end do 
end do 
Also, you can simplify this to

Code: Select all

do j=1,n
   set residSeries(j) start end = rv(t)(j)
end do j
(You can use "T" as a subscript for anything on the right side of the SET instruction.
Gregory
Posts: 20
Joined: Mon Nov 13, 2006 8:05 am

Unread post by Gregory »

You don't need the "end if" when the if-else is inside a loop (or any other compiled structure).
Is that the case even if I have other instructions that must be executed after the "end if"? That would mean that RATS uses the curly brackets to determine the contents of the IF block. If so, in the code below, would RATS recognize that "Other stuff here" is to be done on each iteration but is not part of the IF block?

do i=1,10

Some stuff here

if i>1
{IF True stuff}
Else
{IF False stuff}

Other stuff here

end do i
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Unread post by TomDoan »

That's correct. The {...} enclose the instructions covered by the IF and ELSE. The loop includes everything down to the END that matches the DO.

It's actually quite rare for an IF...ELSE block to be the only statements in a compiled section, so an END IF (which is more correctly END compiled stuff) is almost never needed.
Gregory
Posts: 20
Joined: Mon Nov 13, 2006 8:05 am

Unread post by Gregory »

Crystal clear! Thank you.

Gregory
"You sound pretty good, kid, but can your mom recognize you on the radio?"
- Les Paul
Post Reply