Discuss Scratch

Thingied
Scratcher
1000+ posts

Loops

I see a lot of people use repeat until loops with nothing inside them instead of forever loops. Can anyone explain to me the difference or is there none?
repeat until <>

end

forever

end
The_5th_Scratcher
Scratcher
100+ posts

Loops

repeat until “true” loops are always false when nothing is inside
fdreerf
Scratcher
1000+ posts

Loops

The_5th_Scratcher wrote:

repeat until “true” loops are always false when nothing is inside
Excellent observation. Would you like to attempt to answer the question now, preferably with some insight the OP doesn't already know?
Thingied
Scratcher
1000+ posts

Loops

The_5th_Scratcher wrote:

repeat until “true” loops are always false when nothing is inside
Yeah but what's the difference between it and a forever loop because I see people put them in forever loops like this:
forever
...
repeat until <>
end
Prime689
Scratcher
1000+ posts

Loops

The “repeat until <>” loop runs faster than the forever block(?).

Last edited by Prime689 (May 27, 2021 00:29:00)

han614698
Scratcher
1000+ posts

Loops

Prime689 wrote:

The “repeat until <>” loop runs faster than the forever block(?).
[citation needed]
Thingied
Scratcher
1000+ posts

Loops

Prime689 wrote:

The “repeat until <>” loop runs faster than the forever block because I experemented how many commands the loops have done. The “repeated until <>” loop summed to 12714097, and the forever loop summed to 308070.
So which one should I use and what is the purpose of putting a repeat until in a forever loop if it's never going to end?
imfh
Scratcher
1000+ posts

Loops

If you want a loop to run forever, use the forever block. If you don’t want a loop to run forever and you want to have the loop stop after some condition occurs, use a repeat until block. Using an empty repeat until block to repeat forever will just create confusion; your code will be cleaned if you use forever to repeat forever.

The repeat until block is not faster than a forever block when repeating forever. In fact, the repeat until block is slightly slower than the forever block since it has to parse the input value (which is nothing) and decide to continue repeating. Note that the time lost is very small, so you won’t notice it in the vast majority of circumstances.

Source: https://github.com/LLK/scratch-vm/blob/develop/src/blocks/scratch3_control.js
    repeatUntil (args, util) {
        const condition = Cast.toBoolean(args.CONDITION);
        // If the condition is false (repeat UNTIL), start the branch.
        if (!condition) {
            util.startBranch(1, true);
        }
    }
 
    forever (args, util) {
        util.startBranch(1, true);
    }

Last edited by imfh (May 27, 2021 00:51:48)

Powered by DjangoBB