/

HTML / CSS / JavaScript Tutorial

JavaScript Statement: do … while

[this page | pdf | back links]

In JavaScript, the do … while statement executes a block of statements, then repeats the block while the condition remains true.

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
table,th,tr,td {border: 1px solid black; border-collapse: collapse;}
</style>
</head>
<body>
<table>
<tr>
<th>Example</th>
<th>Resulting value of <code>x</code></th>
</tr>
<tr>
<td><code id="Example"></code></td>
<td><code id="Result"></code></td>
</tr>
</table>

<script>
document.getElementById("Example").innerHTML =
  'var i = 1; var x = 0;<br>' +
  'do {<br>' +
  '&nbsp;&nbsp;x = x + i;<br>' +
  '&nbsp;&nbsp;i++;<br>' +
  '}<br>' +
  'while (i&lt;=4)';
var i = 1; var x = 0;
do {
  x = x + i;
  i++;
  }
while (i<=4);
document.getElementById("Result").innerHTML = x;
</script>

</body>
</html>


NAVIGATION LINKS
Contents | Prev | Next | JavaScript Reserved Words


Desktop view | Switch to Mobile