"Why" Question about Unit 3 Lesson 18 Bubble 10a

Here’s what I did.

What I expect to happen: Alien continues moving around the screen after its first “circuit.”
What actually happens: Alien goes around the screen once, but it keeps moving up and off the screen at the end. I thought it would keep checking my conditionals as long as the Draw Loop keeps looping.

I understand how the exemplar solution works, but I’m curious about why in mine, it doesn’t keep looping around and checking the conditionals. Or maybe it does, just not in the way I expected.

I would appreciate any insight and am happy to clarify my question if needed.

Thanks!
Melody

Hello @msutton,

Interesting question. Here is how I see it…
It has to do with the order of the if statements. Although the draw loop is very fast, it doesn’t read all the if statements simultaneously. A computer still has to read code from the top to the bottom. The upper left corner for the alien is the final if statement and it doesn’t move to the “next” line of code (which would be the first if statement back at the top of the draw loop) because it never leaves the last if statement as that statement still evaluates to true. You can experiment and move around the if statements within the draw loop. This will cause the alien to leave the screen at different corners.

Hope that helps:)
Michelle

Thanks, Michelle! That makes sense, but wow! Now I think the Draw Loop works differently than I thought it did. Why does it only go through the conditionals once? I thought it would go through them all and then loop through them again and again until the player hits Reset.

Melody

Thanks to both Melody and Michelle for helping me make sense of this issue. I had the same thing going on and your posts were very helpful. With your help I was able to make the program do what both Melody and I thought would happen and I think I learned two helpful things.

  1. A program will loop inside an if conditional until it is no longer true. (I think this is correct, but feel free to tell me if I am wrong)

  2. We have the alien moving until it is in a position less than an integer. For example if it moves until alien.x<50, it will stop moving at something like 47. To make it work smoothly like Melody and I wanted it to, I added a line of code inside the conditional like alien.x=50. By doing this I avoided any confusion because the alien would have both alien.x<50 and alien.y<50 after it went around the screen one time.

Thank you!
Fred

1 Like