'continue' command
-
Hi,
I want to use a while loop to keep checking through a nested list, checking that the it meets a set of criteria. If any one of these criteria are not met, I want to make changes to the nested list, then start the while loop again without executing the rest of the code in the loop. The problem is, to do this I need a 'continue' command, and there doesn't seem to be one!
I understand that I can simulate a 'continue' command by making a few changes to my code.
Take this example. This code loops through the list example_list replacing every 0 with a 1, with a 'continue' command being used to start the while loop again every time a substitution is made. The while loop is only broken out of once the last bit of code in the loop is executed - which requires all of the code in the loop to be executed without the loop being started again by a 'continue' command. This means that the loop will only be broken out of once every 0 in the list has been replaced by a 1.
example_list = [1, 1, 0, 1, 1, 1, 0, 1, 0, 1] still_zeros = True while still_zeros loop carry_on = False for index = 0 to len(example_list) loop if example_list[index] == 0 then example_list[index] == 1 carry_on = True break //This breaks out of the for loop, but not the while loop endif repeat if carry_on then continue //This starts the while loop again endif still_zeros = False repeat
This code can be altered to get rid of the 'continue' command, while still retaining the same functionality, if this simple change is made:
example_list = [1, 1, 0, 1, 1, 1, 0, 1, 0, 1] still_zeros = True while still_zeros loop carry_on = False for index = 0 to len(example_list) loop if example_list[index] == 0 then example_list[index] == 1 carry_on = True break //This breaks out of the for loop, but not the while loop endif repeat if !carry_on then still_zeros = False endif repeat
Basically, after the point at which changes might be made to the list, the rest of the code in the while loop is inside an if statement, and is only executed if no changes have been made to the list. If changes HAVE been made, all of that code gets skipped for the current iteration of the while loop (the same effect as if a 'continue' command were used).
The problem is, while this approach works fine for simpler code, it quickly gets complicated if you have a loop that requires lots of uses of the 'continue' command, because every time you would use one, you instead have to paste the rest of the code (the stuff that a 'continue' command would skip) inside an if loop. This means that you can end up with lots and lots of loops!
i guess what I'm asking is:
(1) Is there a continue command, and I just can't find it in the list of commands?
And if the answer is 'no'...
(2) How do I petition for the continue command to be added to Fuze?
-
I'm aware that each 0 in the list can be replaced by a 1 much more easily:
example_list = [1, 1, 0, 1, 1, 1, 0, 1, 0, 1] for index = 0 to len(example_list) loop if example_list[index] == 0 then example_list[index] == 1 endif repeat
However, my goal is to have a code that only makes ONE CHANGE with each iteration of the while loop, before starting the whole while loop again after each change is made (it's too complicated to explain why, that's just what my project needs).
-
Just noticed, each time I wrote
example_list[index] == 1
it should actually have been
example_list[index] = 1
-
Another idea might be to have one outer while loop and one inner while loop, and instead of
continue
you dobreak
in the inner loop.If both loops have the same exit criteria, I would think they would be functionally identical to a single loop with
continue
. -
But to answer your questions:
(1) No, there is no
continue
command in Fuze.
(2) The people developing Fuze do read the forum, and will likely make note of any suggestions made. -
Request for 'continue' duly noted - there's no petitioning for new features :)
In the meantime:
example_list = [1, 1, 0, 1, 1, 1, 0, 1, 0, 1] still_zeros = True while still_zeros loop for index = 0 to len(example_list) loop if example_list[index] == 0 then example_list[index] == 1 break //This breaks out of the for loop, but not the while loop endif repeat // index will only get to equal len(example_list) if it // got all the way through without finding any more zeros to replace if index == len(example_list) then still_zeros = false endif repeat
I'm not at my switch at present but doesn't that simplify things somewhat?
NB: If the loop isn't iterating over as you might expect try addingint index = 0
just before the for loop. It sounds counter-intuitive but Fuze has needed it up until now due to scoping issues. I haven't tested yet if that is still the case in the latest patch.