Using while loop rather than for loop

Using while loop rather than for loop - Сообщения

#1 Опубликовано: 25.03.2021 06:21:23
hanskl

hanskl

7 сообщений из 158 понравились пользователям.

Группа: 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 КиБ) скачан 43 раз(а).
#2 Опубликовано: 25.03.2021 10:15:38
Davide Carpi

Davide Carpi

1416 сообщений из 2873 понравились пользователям.

Группа: 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 Опубликовано: 25.03.2021 10:47:51
hanskl

hanskl

7 сообщений из 158 понравились пользователям.

Группа: 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 Опубликовано: 25.03.2021 17:33:20
Davide Carpi

Davide Carpi

1416 сообщений из 2873 понравились пользователям.

Группа: 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 Опубликовано: 26.03.2021 14:25:48
hanskl

hanskl

7 сообщений из 158 понравились пользователям.

Группа: 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 Опубликовано: 26.03.2021 16:17:42
Jean Giraud

Jean Giraud

983 сообщений из 6866 понравились пользователям.

Группа: 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 КиБ) скачан 48 раз(а).
  • Новые сообщения Новые сообщения
  • Нет новых сообщений Нет новых сообщений