Page 104 - DCAP202_Fundamentals of Web Programming
P. 104
Unit 8: Programming Constructs in JavaScript
} Notes
while (i<=5);
</script>
</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 equal to 0.
The loop will run
i will increase by 1 each time the loop runs.
While i is less than, or equal to, 5, the loop will continue to run.
Task Give examples of while loop and do … while loop.
8.2.4 Break
The break statement will break the loop and continue executing the code that follows after the
loop (if any).
Example: Break example
<html>
<body>
<script type=”text/javascript”>
var i=0;
for (i=0;i<=10;i++)
{
if (i==3)
{
break;
}
document.write(“The number is “ + i);
document.write(“<br />”);
}
</script>
</body>
</html>
LOVELY PROFESSIONAL UNIVERSITY 97