for(i=0; i<11; i++){ //(초기식; 조건식; 증감식)
console.log(i); //실행문 1~10 입력
}
문제1) for문을 이용해서 배열 cost의 값을 모두 더해 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
let i=0; //초기식
while(i<11){ //while(조건식)
console.log(i); //실행문 i~10출력
i++; //증감식 i는 10까지 반복하고 종료함
}
문제1) for 문제1을 while문 형식을 이용해서 배열 cost의 값을 모두 더해 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 (조건식);
문제1) 1~10까지 출력하시오
var i = 0; //(초기식)
do {
i++; //(증감식)
console.log(i); //(실행문) 1~10출력
}
while (i<10); //(조건식)