Skip to Content

I don't understand why only run it once

« How do I...?
1 reply [Last post]
liu peng
Member

[scratchblocks]
when green flag clicked
repeat (10)
 set  [tiaosanX]  to  (250)
 forever if <(tiaosanX) > [-300]>
     change [tiaosanX] by <pick random(-3)to(-1) >
 end
end
[/scratchblocks]

I 'd like to repeat  10 times

but I don't understand why only run it once 

Complete project is here :
parachuting跳伞

 

and ,this can repeat  10 times,as follows:

[scratchblocks]
when green flag clicked
repeat (10)
 set  [tiaosanX]  to  (250)
 repeat until <(tiaosanX) < [-300]>
     change [tiaosanX] by <pick random(-3)to(-1) >
end
end
[/scratchblocks]

Who can help me ?scratch.mit.edu/forums/viewtopic.phpThanks!

Replies
Stefano Federici
Member

The internal loop

forever if <(tiaosanX) > [-300]>

cannot be repeated 10 times as this is an infinite loop (it will be repeated every time that tiaosanX is greater than -300) and it will just wait forever for the condition to be verified.

To leave the loop as soon as tiaosanX is smaller than -300 you have to replace it with a finite loop, that is a <repeat until> loop in which the condition is reversed:

repeat until <(tiaosanX) < [-300]>