instructional systems
Index:
[Session12] JavaScript 2 (Basic syntax, control structure (if, for, etc.))
1 Chapter1
2 Chapter2
3 Chapter3
4 Chapter4
5 Chapter5
Repetition Control Statement (for)
[The purpose of this section]
The syntax directing the repetition includes for and while statements. In this section, the for repetition control statement is explained.

The Basics of “for Repetition Control Statement”

This is either called “for repetition control statement” or “for loop repetition control statement.” This is frequently used in repetition works. The outstanding feature of this statement is that there are two statements and one evaluation expression.

for (statement 1; evaluation expression value; statement 2) {
     statement 3; statement 4,...;
}

The statement 1; is a “for statement” to be executed first, and only once. The evaluation expression value demands the evaluation value each time the “for statement” is repeated. This is repeated until the evaluation expression takes the value FALSE. If this value is FALSE from the beginning, neither statement 2 nor “statement 3;statement 4;...” are executed. The statement 2 in the above “for statement” works mainly as the place to calculate increment/decrement.

The following shows a very common way to use the “for statement.”

Sample (for.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 for statement</title>
</head>
<body>
<h3>Confirming for statement</h3>
 <script type="text/javascript">
 <!--
    j=0;k=0;
    n=25;m=n;
    for(i=0;i<n;i++) {
       j++;
       k=k+j;
    }
    document.write('n=',m,' k=',k);
 //-->
 </script>
</body>
</html>

Actually there is another syntax of “for statement.” This control statement executes statement 1;statement 2:..;statement against all the values of the object. At this time we skip the explanation. The next section will deal with “while statement.”

for (variable in object name) {
    statement 1; statement 2,...;
}
Copyright (c) Yasuo Musashi 2003, All Rights Reserved