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.

Nesting IF-ELSE Statements in a Do Loop

Postby Gregory » Wed Aug 06, 2008 3:05 pm

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
Gregory
 
Posts: 20
Joined: Mon Nov 13, 2006 9:05 am
Location: Montreal

Postby TomDoan » Thu Aug 07, 2008 4:26 pm

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.
TomDoan
 
Posts: 2720
Joined: Wed Nov 01, 2006 5:36 pm

Postby Gregory » Fri Aug 08, 2008 9:04 am

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
Gregory
 
Posts: 20
Joined: Mon Nov 13, 2006 9:05 am
Location: Montreal

Postby TomDoan » Fri Aug 08, 2008 9:18 am

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.
TomDoan
 
Posts: 2720
Joined: Wed Nov 01, 2006 5:36 pm

Postby Gregory » Fri Aug 08, 2008 7:29 pm

Crystal clear! Thank you.

Gregory
"You sound pretty good, kid, but can your mom recognize you on the radio?"
- Les Paul
Gregory
 
Posts: 20
Joined: Mon Nov 13, 2006 9:05 am
Location: Montreal


Return to Help With Programming

Who is online

Users browsing this forum: No registered users and 1 guest