This forum is now read-only. Please use our new forums! Go to forums
What's a real world example for using "while" loops?
Just wanting to know, for understanding purposes, what an example of a use of a “while” loop in a program would be. (More than one would be appreciated, too!) Thanks!
Answer 513076b2e2ec491225007971
Any time your program needs to keep doing something (repeat the same or a similar action) until something happens and you don’t know in advance when or if that something happens, a while
loop is useful. Every programming language has some sort of while loop construct, so I’m going to give you an example in pseudocode:
// music player loop:
while (user does NOT press stop AND NOT end of playlist) {
get the next song
play the song
}
// user input loop
ask user a yes-or-no question
get answer
while (answer is NOT "yes" AND answer is NOT "no") {
repeat the question // this must be a fairly stupid user
}
Answer 5130968aba280229e5008a49
while your game character is level 1, she can only do 5 points damage
while you press the accelerator, you do drive
while the timer is running, the timer counter is progressing…
etc
Sorry, I can only think in game terms, but I think these would all work with while loops..
1 comments

Thanks!
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
1 comments
Thanks!