5 steps to creating a while loop
What is a while loop in javascript? How does it work?
A while loop executes a block of code as long as the expression is true. Seriously, it will run over & over & OVER as long as it’s true.
First, some vocabulary you should already be familiar with..
Code Block — code placed in-between the curly brackets {}. All code that is run each time the loop is run.
Conditional statements — runs a block of code if a particular statement is true.
Function — set of instructions that tells the computer to do something, usually repetitive. Like memory blocks used to save a mini program.
Building a simple while loop…
1.Basic while loop structure
2.Create a variable to keep track of the times the loop runs
3.Each time it runs we’ll add +1 to the counter
4.Create condition, it doesn’t have to be a number but at some point it needs to evaluate to false, so it can end the madness and see it’s family for once.
5.Print the outcome to the page.
In this example the counter will continue to +1 to our variable until the condition evaluates to false.
Hopefully this helps make a little more sense out of while loops for you. Thanks for stoping by and come back… after a while, crocodile (sorry just wanted to work that in.).
//Newbie so if you have additional insights, comment and I’ll update.