Unit 3 Lesson 17- "While" block?

**Game Lab - Code.org
Hi all, I have a student who is starting level 17. I just completed it myself, as I am newer to coding. So far he has called two variables, but for some reason, his program runs very slowly every time. At first I thought it might have been the connection on his device, but I tried running the program from my device, and it lags a lot too. None of the other programs that I am running are lagging.

A second question I have is about the “while” block that he has in his code. It looks like the “if-then” block, but it says “while.” He tends to code in text and I just revert to blocks because it makes it a bit easier for me to see. Is there a “while” block, and if so, what does it do? I have tried messing around with the code a bit myself, band I eliminated the “while” and created the sprites in the draw loop and it works better, but I want to know why it works better.

Here is the update: Code.org

Any insights appreciated!

Hi @dmurr-pinsker,

I agree with you. I saw that the two sprites were drawn but they did lag a little. For me, I did not see a background drawn (it was white). What is happening is that the while loop is the only thing running. A while loop runs as long as the condition is true. It can be a tricky loop to use because of this - it can loop forever. If you use it, you should always use a condition that will eventually evaluate to false so that the program can move out of the loop and onto the next line of code. So, the background blocks were never running and the program was having to evaluate the draw loop AND the while loop to draw the sprites. If you remove the while loop and place the draw sprites below the background, you see both the pink background and the sprites and top.

Also, the only background you see is the final background. The computer reads the others too quickly to show the other backgrounds. The student could use the random number in the rgb blocks to animate different colors for the background if that was the intent of the multiple background blocks.

Hope that helps!
~Michelle

Hi Michelle,

This actually does help quite a bit. So if I understand you correctly, the while loop pretty much always needs a boolean that can eventually evaluate to false-otherwise that while loop will run forever, and the program can’t really move forward.

To be successful with the while loop, the student would need to use the while block followed by comparison operators so that the expression would eventually evaluate to false. Do I have that right?

And regarding the background, I tried what you talked about with the random number for rgb, and it seems to work. The most recent update is here: Game Lab - Code.org

So here is my other question: Why does the program run more slowly and have time to show different colors of background when using the random numbers for RGB values rather than set values?

I am new to coding, so forgive my ignorance. But I find it very interesting.

Thank you again for your insights.

@dmurr-pinsker,

Here is the documentation on the while loop. It more clearly shows the syntax for the while loop and yes, they include a boolean expression to determine if it runs. Again, remember that the expression should eventually result to false so that it doesn’t loop infinitely.

I’ll be honest, I’m not sure if you don’t see the first 3 background tags because of the speed of the draw loop (1/30th of a second) or if it is the fact that there is only one background so that when there are multiple background blocks, the computer reads the final tag. You see the random number backgrounds because they are constantly changing 30x per second. I do think on your example (nice job!:wink: ) we are probably only seeing the final background block.

~Great questions! Hope this helps:)
Michelle