0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

for文

Posted at

構文

for (初期化式; 条件式; 増分式) {
    実行する文;
}

1.初期化式で変数宣言
2.条件式の評価結果がtrueなら次のステップへ、falseなら終了
3.実行する文を実行
4.増分式で変数を更新
5.(2)に戻る

for(var i = 0; i <= 10; i++){
    console.log(i); //0~10を順番に出力
}

例文(配列)

const arr = ["りんご","バナナ","みかん"];
console.log("配列の長さ: " + arr.length); //コンソール画面上に配列の長さを出力する
for(var i = 0; i < arr.length; i++) { //arr.lengthで配列の長さの分だけループを実施する
    console.log(arr[i]); //コンソール画面上にarrのデータを出力する
}

break

  • ループ処理中に途中で抜け出したいとき
const arr = [1,2,3,4,5,6,7,8,9,10];
for (var i = 0; i < arr.length; i++){
    if(arr[i] > 5){
        break;//for文から処理を抜ける。
    }
console.log(arr[i]);//1~5出力
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?