Iteration Statements in ECMAScript
Statements in ECMAScript – Part 3
ECMAScript 6
Foreword: In this part of the series, I talk about iteration statements in ECMAScript.
By: Chrysanthus Date Published: 13 May 2016
Introduction
Iteration statements are the do-while, while and for loops.
The do-while Statement
The syntax for the do-while statement is:
do Statement while ( Expression );
It can have one or more internal statements in the place of Statement. The internal statements end with semicolon. If there are more than one internal statements, they will be placed in a block. Here, Expression is the condition. An example of the do-while statement is in the code:
n = 0;
do
++n;
while (n<5);
Both the internal statement and the whole do-while statement have been terminated by semicolon. You could still terminate either of them by pressing the Enter Key. However, from now to the end of this tutorial, all statements, except the block statement will be terminated by semicolon. The first statement in the above code is not part of the do-while statement.
The while Statement
The syntax for the while statement is:
while ( Expression ) Statement
Here, Expression is the condition. The statement can have one or more internal statements in the place of Statement. An example of the while statement is in the code:
n = 0;
while (n<5)
++n;
The first statement here is not part of the while statement.
The syntax for the for-statement is:
for ( Expressionopt; Expressionopt ; Expressionopt ) Statement
An example for the for-statement is:
for (n=0; n<5; ++n)
alert(n);
There is one internal statement in the example. The internal statement in the example is a function call (left-hand-side) expression statement. For the syntax, Statement can be replaced by one or more internal statements in a block.
There are two other variants of the for-loop, which I will talk about later.
The break Statement
The “break;” statement can be used to terminate a loop before its determined end. The syntax for this is:
break;
Try the following code and note that the loop ends after n is 2.
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<script type="text/ECMAScript">
for (n=0; n<5; ++n)
{
alert(n);
if (n == 2)
{
break;
}
}
</script>
</body>
</html>
The extra HTML code is to produce a web page.
Each time in the loop, the if-condition is checked for the value of true. When n is 2, the if-condition will return true; making the if-block to execute. In the if-block, you have just one statement, the break statement. It is just one word, break. Always end the break statement and other statements with a semicolon. The break statement stops the loop from repeating. In this case it stopped the loop when the internal if-condition occurred (was true).
You can cause an iteration to be skipped as the loop is repeating. You use the continue statement for this. The syntax for this is:
continue;
The following code illustrates this, when n is 2. The iteration for n equal 2 is skipped.
<script type="text/ECMAScript">
for (n=0; n<5; ++n)
{
if (n == 2)
{
continue;
}
alert(n);
}
</script>
In order to skip the iteration of the whole block, you place the continue statement and its condition at the beginning of the block.
That is it for this part of the series. We stop here and continue in the next part.
Chrys
Related Links
ECMAScript BasicsECMAScript Operators
Expressions in ECMAScript
Statements in ECMAScript
Custom Objects in ECMAScript
Functions in ECMAScript
ECMAScript Date Object
The ECMAScript String Object
ECMAScript String Regular Expressions
ECMAScript Template Literal
The ECMAScript Array
ECMAScript Sets and Maps
ECMAScript Number
Scopes in ECMAScript
Mastering the ECMAScript (JavaScript) eval Function
Sending Email with ECMAScript
ECMAScript Insecurities and Prevention
Advanced Course
Advanced ECMAScript Regular Expressions
Promise in ECMAScript 2015
Generator in ECMAScript 2015
ECMAScript Module
More Related Links
Node Mailsend
EMySQL API
Node.js Web Development Course
Major in Website Design
Low Level Programming - Writing ECMAScript Module
ECMAScript Course
BACK NEXT