Javascript-for-loop
提供:Dev Guides
JavaScript-Forループ
「 for 」ループは、最もコンパクトなループ形式です。 それは次の3つの重要な部分が含まれています-
- カウンタを開始値に初期化する*ループ初期化*。 初期化ステートメントは、ループが始まる前に実行されます。
- 特定の条件が真かどうかをテストする* testステートメント*。 条件が真の場合、ループ内で指定されたコードが実行されます。そうでない場合、コントロールはループから出ます。
- カウンターを増減できる* iterationステートメント*。
3つの部分すべてをセミコロンで区切って1行に入れることができます。
フローチャート
JavaScriptの for ループのフローチャートは次のようになります-
構文
*for* ループの構文はJavaScriptです次のとおりです-
for (initialization; test condition; iteration statement) {
Statement(s) to be executed if test condition is true
}
例
JavaScriptで for ループがどのように機能するかを学ぶには、次の例を試してください。
<html>
<body>
<script type = "text/javascript">
<!--
var count;
document.write("Starting Loop" + "<br/>");
for(count = 0; count < 10; count++) {
document.write("Current Count : " + count );
document.write("<br/>");
}
document.write("Loop stopped!");
//-->
</script>
<p>Set the variable to different value and then try...</p>
</body>
</html>
出力
Starting Loop
Current Count : 0
Current Count : 1
Current Count : 2
Current Count : 3
Current Count : 4
Current Count : 5
Current Count : 6
Current Count : 7
Current Count : 8
Current Count : 9
Loop stopped!
Set the variable to different value and then try...