Looping Statement:
Loops in JavaScript allow a block of code to run multiple times as long as a given condition is satisfied. They help reduce repetition and make programs more efficient and organized.
Loops offer a quick and easy way to do something repeatedly. This chapter of the JavaScript Guide introduces the different iteration statements available to JavaScript.
Loops continue running until the condition becomes false.
They are useful for iterating over arrays, strings, and ranges of values.
While loop:
The while loop executes as long as the condition is true. It can be thought of as a repeating if statement.
A while statement executes its statements as long as a specified condition evaluates to true. A while statement looks as follows:
Syntax
while (condition) {
// Code to execute
}
If the condition becomes false, statement within the loop stops executing and control passes to the statement following the loop.
The condition test occurs before statement in the loop is executed. If the condition returns true, statement is executed and the condition is tested again. If the condition returns false, execution stops, and control is passed to the statement following while.
- While loop starts with the checking of Boolean condition. If it evaluated to true, then the loop body statements are executed otherwise first statement following the loop is executed. For this reason it is also called Entry control loop
- Once the condition is evaluated to true, the statements in the loop body are executed. Normally the statements contain an update value for the variable being processed for the next iteration.
- When the condition becomes false, the loop terminates which marks the end of its life cycle.
Today’s learning was not only about understanding the while loop but also about applying it through practical exercises. Solving programs helped me see how loops work in real situations, and writing blogs about my practice will help reinforce what I learn. Step by step, I am building both my coding skills and my habit of documenting my learning journey.
Given tasks in session
- To print the 5 4 3 2 1 using loop
let i=5;
while(i>=1){
console.log(i);
i--;
}
- Print the 1 as 5 times using loop
count = 0;
while(count < 5){
console.log(1);
count++;
}
References
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration
https://www.geeksforgeeks.org/javascript/loops-in-javascript/

Top comments (0)