JavaScript_ 02-2.せいぎょぶん


≪制御文|Control文|emdw≫:条件文、繰返し文など、プログラム・シーケンス・フローを制御する実行文。


≪繰返し文|Repeat文|emdw≫:制御プログラム内で同じコマンドを繰り返し実行する実行文。(for / while / do-while)


📕 問題を解くと同時に概念を整理する


✔ for

for(i=0; i<11; i++){ //(초기식; 조건식; 증감식)
console.log(i); //실행문 1~10 입력
}
問題1)for文を用いて配列コストの値を加算しtotal cost変数に格納する.
let cost = [ 85, 42, 37, 10, 22, 8, 15 ];
let total_cost = 0;
for(i=0; i<cost.length; i++){ 
    total_cost+=cost[i];
}
console.log(total_cost); //219

✔ while

let i=0; //초기식
while(i<11){ //while(조건식) 
     console.log(i); //실행문 i~10출력
  i++; //증감식  i는 10까지 반복하고 종료함
      }
問題1)while文フォーマットを使用してfor problems 1を配列コストの値に加算し、total cost変数に格納してください.
let cost = [ 85, 42, 37, 10, 22, 8, 15 ];
let total_cost = 0;
let i=0; // 초기식
while(i<cost.length){ //while(조건식)
total_cost+=cost[i]; //실행문
b++; //증감식
}
console.log(total_cost); //219

✔ do while

do {
표현식의 결과가 참인 동안 반복적으로 실행하고자 하는 실행문;
} while (조건식);
問題1)1-10に印刷
var i = 0; //(초기식)
do {
   i++;  //(증감식)
   console.log(i);  //(실행문) 1~10출력
}
while (i<10);  //(조건식)