From the course: Computer Science Principles: Programming

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Use while loops

Use while loops

From the course: Computer Science Principles: Programming

Start my 1-month free trial

Use while loops

- Programming allows us to repeat actions multiple times. We can used named sections of code called functions to execute lines of code whenever we need to run them. That's fine for when we need to intermittently run sections of code, but if we want to run the code over and over again, we can use loops. Loops are ways a programmer can take a section of code and have a program run them multiple times. Using a conditional, the programmer can define a limit to the number of times a loop runs. The most basic loop is a while loop. It works like this. At the top you ask a question as a conditional statement that evaluates as either true or false. If the answer to the question is true, then the code in the loop runs, and then it goes back to the top and asks the question again. If the answer to the question is ever false, then the loop stops and doesn't run again, and the program continues with code after the loop. With this type of loop, it will run for as long as it is true. But there is a…

Contents