Page 153 - DCAP408_WEB_PROGRAMMING
P. 153
Unit 5: Scripting Language
</script> Notes
</body>
</html>
Output
The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
Explanation
i is equal to 0.
While i is less than, or equal to, 5, the loop will continue to run.
i will increase by 1 each time the loop runs.
5.6.3 The do...while Loop
The do...while loop is a variant of the while loop. This loop will execute the block of code ONCE,
and then it will repeat the loop as long as the specified condition is true.
Syntax
do
{
code to be executed
}
while (var<=endvalue);
Example: do while example.
The example below uses a do...while loop. The do...while loop will always be executed at least
once, even if the condition is false, because the statements are executed before the condition is
tested:
<html>
<body>
<script type=”text/javascript”>
var i=0;
do
{
document.write(“The number is” + i);
document.write(“<br />”);
i++;
}
LOVELY PROFESSIONAL UNIVERSITY 147