Set i /0*100/ ;

parameter a(i)

;

a('0') = 0 ;

loop(i, a(i+1) = a(i)+1 ;

)

;

Display a;

How I can transform the above loop, so that iteration stop when a(i+1) = 20

I used the following

loop(i$(a(i) lt 20), a(i+1) = (a(i)+1)$(a(i) lt 20) ;

but when value reaches to 20, iteration keep going.

Best Regards

## LOOP

- Gideon Kruseman
- User
**Posts:**19**Joined:**10 months ago

### Re: LOOP

When you reach 20 the calculation is not executed so a(i+1) is equal to zero. which means it is below 20 and the process starts over again. so you will need to build in a switch that tells you, you have reached your threshold level. Something along these lines will work.

Cheers, Gideon

Set i /0*100/ ;

parameter a(i)

;

scalar init;init=0;

a('0') = 0 ;

loop(i${(a(i) lt 20) and (init ne 1)},

a(i+1) = a(i)+1;

init${a(i+1) eq 20}=1;

) ;

Display a;

Cheers, Gideon

Set i /0*100/ ;

parameter a(i)

;

scalar init;init=0;

a('0') = 0 ;

loop(i${(a(i) lt 20) and (init ne 1)},

a(i+1) = a(i)+1;

init${a(i+1) eq 20}=1;

) ;

Display a;

Gideon Kruseman

ex-ante and foresight lead @CIMMYT, big data focal point @CIMMYT, coordinator CoP socio-economic data @CGIAR_BigData

ex-ante and foresight lead @CIMMYT, big data focal point @CIMMYT, coordinator CoP socio-economic data @CGIAR_BigData

### Re: LOOP

Thank you Sir, It works

Can you recommend any book with case studies or examples for loop functions?

Can you recommend any book with case studies or examples for loop functions?

Gideon Kruseman wrote: ↑4 months agoWhen you reach 20 the calculation is not executed so a(i+1) is equal to zero. which means it is below 20 and the process starts over again. so you will need to build in a switch that tells you, you have reached your threshold level. Something along these lines will work.

Cheers, Gideon

Set i /0*100/ ;

parameter a(i)

;

scalar init;init=0;

a('0') = 0 ;

loop(i${(a(i) lt 20) and (init ne 1)},

a(i+1) = a(i)+1;

init${a(i+1) eq 20}=1;

) ;

Display a;