Using while loop rather than for loop

Using while loop rather than for loop - Messages

#1 Posted: 3/25/2021 6:21:23 AM
hanskl

hanskl

7 likes in 158 posts.

Group: 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 KiB) downloaded 41 time(s).
#2 Posted: 3/25/2021 10:15:38 AM
Davide Carpi

Davide Carpi

1416 likes in 2873 posts.

Group: 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 Posted: 3/25/2021 10:47:51 AM
hanskl

hanskl

7 likes in 158 posts.

Group: 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 Posted: 3/25/2021 5:33:20 PM
Davide Carpi

Davide Carpi

1416 likes in 2873 posts.

Group: 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 Posted: 3/26/2021 2:25:48 PM
hanskl

hanskl

7 likes in 158 posts.

Group: 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 Posted: 3/26/2021 4:17:42 PM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: 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 KiB) downloaded 43 time(s).
  • New Posts New Posts
  • No New Posts No New Posts