 # BPC Script logic for Dummies? (Part 5)

Like other program language or script, the logic script also supports Loop statement.

Let’s see how it works.

Here is the syntax of *FOR – *NEXT statement.

*FOR  {variable1} = {set1} [ AND {variable2={set2}]

{other statement…}

*NEXT

And here is an example.

*FOR %CURR%=USD,EURO

*REC(FACTOR=1.1,TIME=2011.OCT,CURRENCY=%CURR%)

*NEXT

So what is the meaning of the example above?

1. We set a variable as %CURR%
2. %CURR% variable will be replaced with two values USD and EURO.
3. *REC statement includes %CURR% variable.
4. Therefore, it will be translated two statement as below
because *REC statement exists inside of *FOR – *NEXT statement.

*REC(FACTOR=1.1,TIME=2011.OCT,CURRENCY=USD)
*REC(FACTOR=1.1,TIME=2011.OCT,CURRENCY=EURO)

Let’s assume %CURR% varible has USD,EURO,CHF,KRW then the it will be translated as below.

*REC(FACTOR=1.1,TIME=2011.OCT,CURRENCY=USD)
*REC(FACTOR=1.1,TIME=2011.OCT,CURRENCY=EURO)

*REC(FACTOR=1.1,TIME=2011.OCT,CURRENCY=CHF)
*REC(FACTOR=1.1,TIME=2011.OCT,CURRENCY=KRW)

Someone may say “we can use multiple line of *REC statement”.

Of course, it is correct if it is simple but we need *FOR – *NEXT statement because it can be used as a nested form.

For example,

*FOR %YEAR%=2003,2004,2005
*FOR %MONTH%=JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC
*NEXT
*NEXT

If the user is using *REC statement,user should write 36 statements instead of above simple a *FOR – *NEXT statement.

(NOTE: BPC NW supports Nested FOR – NEXT in the version 7.5 )

In addtion, User may use two variable sets like the exmaple below.

*FOR %X%=1,2,3 AND %Y%=A,B,C

*REC(FACTOR=1.5,TIME=%X%,CURRENCY=%Y%)

*NEXT

So the first variable set and the second variable set will be matched 1 to 1; then will be replaced as the example below.

*REC(FACTOR=1.5,TIME=1 ,CURRENCY=A)
*REC(FACTOR=1.5,TIME=2 ,CURRENCY=B)
*REC(FACTOR=1.5,TIME=3 ,CURRENCY=C)

What if the number of values is not matched between first and second variable?

If the first variable has less values than  the  second  variable,
the  extra  values  of  the  second  variable  will be  ignored.

If  the  first  variable  has more values than the second variable,
the missing values of the second variable will be assumed null so please be careful to match the number of varible.

The last thing about *FOR – *NEXT is using data set variable as values.
Users can use data_set like %TIME_SET% instead of specifying all time members.
This is very useful when we use script logic with dynamic dataset.

For example, We can use

*FOR %MYTIME%=%TIME_SET%

*FOR %MYTIME%=2003.JAN,2004.JAN,2005.JAN

Therefore, users can execute script logic dynamically based on the passed data sets.

We will see how to use GET and FLD function and memory variable in the next post as the last topic.

### Assigned Tags

You must be Logged on to comment or reply to a post. Hi James,

Thanks for the informative blog again. Question on the BPC Netweaver version: Does this version support nested *FOR/*NEXT loops?

I don't think it did a year ago, but I haven't checked in some time, so want to make sure. The documentation for the Netweaver version doesn't mention this as an option.

Cheers,
Ethan James Lim
Blog Post Author
Hi Ethan,

Unfortunately, NW doesn't support it.
(I should have put a note. I will change it.)

I didn't check version 10 but as I know it is same as version 7.5.

Even though NW version doesn't support some functionality of Logic script, it can be supported using BADI which doesn't exist in BPC MS.

Thank you for the good comment.

Regards,
James Lim James Lim
Blog Post Author
Hi Ethan,

I just checked NW 7.5 and it worked.
I had tested before I answered previous comment with NW 7.5 but there was a problem with my system so I posted wrong answer.

Nested For - Next is supporting in BPC NW 7.5 and 10.

Thank you.
Regards,
James Lim Hi James,

Great, thanks for testing! It would be nice to get the help.sap.com updated with this information. Do you have any pull there? 😉

Cheers,
Ethan James Lim
Blog Post Author
Ethan,

Good suggestion!
I will ask it to document team.
It will be updated so please check it later.

Thank you.
Regards,
James Lim