Using while loop rather than for loop

Using while loop rather than for loop - Mensajes

#1 Publicado: 25/3/2021 6:21:23
hanskl

hanskl

7 me gusta en 158 mensajes.

Grupo: User

Hi,

Can someone help me change my for loop to a while loop?

Also, there a few uses of "programming" functions in the sheet (for loop, sums etc.). What are the best practices for use in indexes? Can I use "i" for all the programming functions, or should I use a new letter every time?

Best regards,

while loop.sm (22,54 KiB) descargado 1282 vez(es).
#2 Publicado: 25/3/2021 10:15:38
Davide Carpi

Davide Carpi

1.443 me gusta en 2.936 mensajes.

Grupo: Moderator

while loop repeats itself as long as the condition (1st argument) it's true. So you need that it's true before the loop starts to run it at least once AND a modifier inside the loop to change it when some criterion is met.

In your example, you migth implement it like this:

2021-03-25 14_02_55-SMath Studio - [while loop.sm_].png

you can even move the i<100 check inside an infinite loop loop

2021-03-25 14_11_50-SMath Studio - [while loop.sm_].png

If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
#3 Publicado: 25/3/2021 10:47:51
hanskl

hanskl

7 me gusta en 158 mensajes.

Grupo: User

Davide,

Thank you for the suggestion. The condition for continuing the loop should be (sum(PS.i))<(sum(PS.0)).

For this kind of iteration, does one make more sense over the other?

Best regards,
#4 Publicado: 25/3/2021 17:33:20
Davide Carpi

Davide Carpi

1.443 me gusta en 2.936 mensajes.

Grupo: Moderator

When working with while you have to focus mainly on how to stop the loop. If there is always a convergence, then you are fine. But if you get stuck in an infinite loop the program might crash. So be careful if you plan to not cap the number of iterations.
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
#5 Publicado: 26/3/2021 14:25:48
hanskl

hanskl

7 me gusta en 158 mensajes.

Grupo: User

Are there any advantages to a while loop versus a for loop?

Do you have some tips/insight to the question regarding using the same letter over and over again in loops, sums etc.?

Best regards,
#6 Publicado: 26/3/2021 16:17:42
Jean Giraud

Jean Giraud

983 me gusta en 6.866 mensajes.

Grupo: User

Wrote


1. Are there any advantages to a while loop versus a for loop?
2. Do you have some tips/insight to the question regarding using the same letter over and over again in loops, sums etc.?


............
Document would help, as you may have specific case loop.
1. Golden ratio is a self iterative While solver min/ax all at once
2. Inverse(f,L,H,N) is a simple forLoop as it invokes an auto-iterative solver
3. Critical region is a While of two variables [x,y].
Enjoy ... Jean

Golden ratio QUICK.sm (43,03 KiB) descargado 1217 vez(es).
  • Nuevos mensajes
  • Sin mensajes nuevos