The document discusses the basics of the while loop and its variant, the do-while loop. A do-while loop guarantees that the loop body executes at least once, while a while loop evaluates a condition before execution. The syntax and execution process of both loops are explained, highlighting their differences in behavior based on the condition evaluation.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0 ratings0% found this document useful (0 votes)
21 views
While loops and do while loops in js
The document discusses the basics of the while loop and its variant, the do-while loop. A do-while loop guarantees that the loop body executes at least once, while a while loop evaluates a condition before execution. The syntax and execution process of both loops are explained, highlighting their differences in behavior based on the condition evaluation.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 3
Today's topic is focused on the "while
loop". Let's explore the basics of the while
loop, followed by the variant of the while
loop, known as the "do-while loop".
When using a do-while loop, you ensure
the loop body is executed at least once.
Next, let's move on to seeing the syntax of
the while loop which is a very basic loop.
The process of execution may differ, but
with the while loop, we simply evaluate a
condition, and if the condition is true, it will
continue under the while loop. If it is false,
then it will be executed.
When using a "for loop", there are three
statements that serve different purposes.
In contrast, with "while loops", we simply
evaluate a condition.
Do-while loop is a variation of while loop,where the block of code is executed at
least once, regardless of the loop
condition.
The syntax for a do-while loop is:
do { // code to be executed} while
(condition);
The block of code inside the curly braces
will always be executed first, and then the
loop condition is checked. If the condition
is true, the loop will execute again. This
process will continue until the condition
becomes false.
On the other hand, a while loop checks the
condition first, and if it is true, the block of
code is executed. If the condition is false,
the loop will not be executed at all.
For example, the condition 10 less than
2 is always false, but in a do-while loop,the block of code will be executed at least
once before the condition is checked,
whereas in a while loop, the code will not
be executed at all.