While Loop
JavaScript While Loop
The while
loop is another control structure in JavaScript that allows you to execute a block of code as long as a specified condition is true. It is useful when you do not know in advance how many times you need to repeat a block of code.
Syntax
condition: This is the condition that is checked before each iteration. If the condition evaluates to
true
, the loop continues. If it evaluates tofalse
, the loop stops.
Example
Let's consider a simple example of a while
loop that prints the numbers from 1 to 5.
In this example:
The variable
i
is initialized to1
.The
while
loop checks ifi
is less than or equal to5
.As long as the condition is
true
, the code inside the loop (console.log(i); i++;
) is executed.The value of
i
is incremented by1
after each iteration.When
i
becomes6
, the conditioni <= 5
evaluates tofalse
, and the loop stops.
Using the While Loop with Arrays
The while
loop can also be used to iterate over the elements of an array. Here's an example:
In this example:
The variable
index
is initialized to0
.The
while
loop checks ifindex
is less than the length of thefruits
array.As long as the condition is
true
, the code inside the loop (console.log(fruits[index]); index++;
) is executed.The value of
index
is incremented by1
after each iteration.When
index
becomes equal to the length of thefruits
array, the conditionindex < fruits.length
evaluates tofalse
, and the loop stops.
The do...while
Loop
do...while
LoopThe do...while
loop is a variant of the while
loop that executes the code block at least once before checking the condition. The condition is evaluated after the code block has been executed.
Syntax
Example
Let's consider a simple example of a do...while
loop that prints the numbers from 1 to 5.
In this example:
The variable
i
is initialized to1
.The code block inside the
do
statement (console.log(i); i++;
) is executed.After the code block is executed, the condition
i <= 5
is checked.As long as the condition is
true
, the loop continues.When
i
becomes6
, the conditioni <= 5
evaluates tofalse
, and the loop stops.
Using While Loop with codeswithpankaj.com
To illustrate the use of the while
loop with codeswithpankaj.com, let's consider an example where we generate a list of tutorial topics.
In this example:
The variable
index
is initialized to0
.The
while
loop checks ifindex
is less than the length of thetopics
array.As long as the condition is
true
, the code inside the loop (console.log('Learn ' + topics[index] + ' on codeswithpankaj.com'); index++;
) generates a message for each tutorial topic.
Best Practices
Avoid Infinite Loops: Make sure the loop has a terminating condition to avoid infinite loops that can crash your program.
Initialize Loop Control Variables Properly: Ensure that the loop control variables are properly initialized and updated within the loop.
Use
do...while
for At Least One Execution: Use thedo...while
loop when you need the code block to execute at least once, regardless of the condition.
Summary
The while
loop and the do...while
loop are useful control structures in JavaScript for repeating a block of code based on a condition. By understanding their syntax and usage, you can effectively perform repeated tasks in your programs. Practice using the while
loop to master control structures in JavaScript with codeswithpankaj.com.
Last updated