A loop is a sequence of instructions that are repeated until a certain condition is met. In contrast to variable which help you NOT to repeat code, loops help you TO repeat code. There are three types of loops (for loops, while loops and do-while loops). For loops show the initial action, loop condition, and action performed for each iteration. While and do-while loops run a program until it is false. A for, while and do-while loop example is shown below.
for ( let x=1; x < 5, x++) {
alert(x)
}
This code will test the code until the condition is true. While loops test the code until the condition is false.
let count=0
while (x<10) {
Alert (“Code is true”) ;
count++
}
This code will run until condition is false. Do- while loops will execute a code while it is true, until it is false.
let count=0
do{
alert(“Code is true);
count++;
} while(count<10)
When count reaches 10, the code will be false and stop executing.
Leave a Reply
You must be logged in to post a comment.