#1 Loop

공부의 기록·2022년 1월 31일
0

JavaScript

목록 보기
6/16
post-thumbnail

Introduce

본 문서는 2022년 1월 31일 에 작성되었습니다.

Array.prototype.forEach() 와 같은 클래스 내부 순회 함수는 언급하지 않습니다.
관련 내용은 Array, Set, Map 을 참고해주세요.

Syntax

반복문은
조건문이 참인 동안 반복문을 실행하는 것이 골자입니다.

일반적으로 for 문과 while 문으로 나뉩니다.

for

for (let index=0; index<4; index++) {
  console.log(index);
}

1,2,3,4 출력

for ... in

const texts="hello";

for (const text in texts) {
  console.log(text);
}

0,1,2,3,4 출력

for ... of

const texts="hello";

for (const text of texts) {
  console.log(text);
}

"h","e","l","l","o" 출력

while

let count=0;
while (count++<10) {
  console.log(count);
}

0,1,2,3,4,5,6,7,8,9,10 출력

do ... while

let count=0;
do {
  console.log(count);
} while(count++<10);

0,1,2,3,4,5,6,7,8,910 출력

✅ break, continue;

for (대충 조건문A) {
  for (대충 조건문B) {
    break; // B 중단
  }
  break; // A 중단
}
for (대충 조건문A) {
  for (대충 조건문B) {
    continue; // B 다시 시작
  }
  continue; // A 다시 시작
}

✅ Naming Loop

위와 같은 경우에 조건문 B 내부에서 분기점을 만들어 전체 반복문을 종료하고 싶다면 Naming Loop를 사용할 수 있다.

hello : for (대충 조건문A) {
  for (대충 조건문B) {
    break hello; // hello 반복문 중단
  }
}
profile
2022년 12월 9일 부터 노션 페이지에서 작성을 이어가고 있습니다.

0개의 댓글