What an infinite loop is, how while (true) compares with the forever block in Scratch, and how to break out of one with break or Ctrl+C. From the lesson "Loops" in C programming basics (CS50 Shorts). Source: CS50 Shorts, "Loops" by CS50, Harvard University Original: https://cs50.harvard.edu/x/2024/shorts/loops/ Licence: CC BY-NC-SA 4.0 (https://creativecommons.org/licenses/by-nc-sa/4.0/) Changes: Cut from 0:28 to 1:21, reformatted to vertical with a blurred background and re-encoded; captions, summary and thumbnail by Upgram. Not endorsed by CS50 or Harvard University. Shared under the same licence.
First is forever from scratch, which compares to while true. This is what we call an infinite loop. The lines of code between the curly braces, much like the puzzle pieces that fit inside the C of the forever block, will execute repeatedly from top to bottom over and over and over forever. After all, if you recall from our discussion of Boolean expressions, true is always true. So if we're doing something so long as true is true, which seems a little silly, true is always true. So it'll always run. It'll run forever and ever and ever until we find a way to break out of it with a break statement, which again we saw a little while ago when we were talking about switch, or otherwise just kill our program. Which incidentally, if you ever find yourself in a situation with an infinite loop and you don't know how to stop your program from running, just hit Control and C, and that'll kill your program for you. But this again is called an infinite loop. It is a loop that will run forever.
Շարունակելու համար ներբեռնեք հավելվածը
Հավանեք, մեկնաբանեք, հետևեք հեղինակներին և վերահսկեք Ձեր առաջընթացը Upgram հավելվածում։
Ներբեռնել Upgram-ը
Մեկնաբանություններ