For and While loops


Someone in the office here recalls once being told that (I think this is how it went) when a While loop is executing in OptoScript, it takes up the processor until completion of the loop, where a For loop would be allowed to be split up and let other charts nab some processor time as well, even if it hadn’t completed yet.

Is this true?

Also, is there any way to break out of a For loop?


[B]Short answers: [/B]
No, that’s not true.
Use a while loop if you want to “break out.”

[B]Longer answer:[/B]
You don’t have to worry about one chart blocking other charts from running. The processor time is shared equally between each of your charts (and the “host task”) in turn.

However, if you have a lot of charts running, that means they each have a smaller slice of the pie, and that’s why it’s a good idea to add an appropriate delay in [I]any looping logic[/I] (using the “DelaySec” or “DelayMsec” Timing command), BECAUSE any charts in a “delay” give up their slice until that delay is over.

Are you looping to check the temperature? No need to do that every 1 ms, maybe every 10 seconds or so. Even better, use a variable for these (like “long_delay” or “medium_delay” or “short_delay”). That way, if you’re troubleshooting logic and don’t want to wait for that whole “long_delay” value, you can temporarily modify it during your debugging.

More info on time-slicing, etc. [U][B]in doc 1779, our PAC Project System Performance tech note[/B][/U].

I hope that helps! Happy looping.