instructional systems
Index:
[Session12] JavaScript 2 (Basic syntax, control structure (if, for, etc.))
1 Chapter1
2 Chapter2
3 Chapter3
4 Chapter4
5 Chapter5
Your Location: Home Page  >  [3] Interactive web pages using JavaScript (on condition)  >  [Session12]JavaScript 2 (Basic syntax, control structure (if, for, etc.))  >  Chapter4  >  4.4 Repetition Control Statement 2 (while)
Repetition Control Statement 2 (while)
[The purpose of this section]
The syntax directing repetition includes for and while statements. In this section, the “while repetition control statement” is explained.

The Basics of “while Repetition Control Statement”

The “while repetition control statement” has a simple loop syntax as shown below.

while (evaluation expression value) {
     statement 1; statement 2;....;
}

If the evaluation expression value is TRUE, {statement 1;statement 2;...;} will be executed. Upon the execution, the evaluation expression value is checked again, and the next controlling action will be executed. If the evaluation expression value is FALSE from the beginning, {statement 1:statement 2;...;} will not be executed at all, and the control will move to the next line. Let’s see this in the following program.

Sample (while.html)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="ja">
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 <meta http-equiv="Content-Script-Type" content="text/javascript
 charset=utf-8">
 <title>Confirming while statement </title>
</head>
<body>
<h3>Confirming while statement </h3>
 <script type="text/javascript">
 <!--
    k=0;i=0;
    n=25;m=n;
    while(n > 0) {
       i++;
       k=k+i;
       n--;
    }
    document.write('n=',m,' k=',k);
 //-->
 </script>
</body>
</html>
Copyright (c) Yasuo Musashi 2003, All Rights Reserved