2023.3.16 알고리즘

Junghan Lee·2023년 3월 16일
0

Algorithm

목록 보기
2/2

반복문이란?

기본 포맷
for(let i = 0; i < 5 ; i++){
   반복실행할 코드
}

  • i 가 5 보다 작으면 true -> 로직을 계속 수행
  • i ++ (증감식) 을 통해 몇 번 반복할 것인지 조절
  • 무한루프만 피하면 된다.

break & continue

for (let i = 0; i < 5; i = i + 1){
consolelog(i)
if( i=2){
//break;(중단) continue;(2는 중단하고 패스)

for - in

for ( let key in obj,arr,str){
consolelog(obj,etc)
}
각각의 요소값 반환
배열과 문자열 - 인덱스값, 객체 - 키값 가져옴

for - of

각각의 요소들을 가져올 수 있다. (문자열, 배열)
let arr = [1,2,3,4]
for(let key of arr){consolelog(arr)}
result : 1 , 2 , 3, 4

forEach

배열에만 사용 arr.forEach(function())
문법이라기보다는 메서드에 가깝다
const arr = [ 'a', 'b', 'c','d']
arr.forEach(function(el){console.log(el)})
결과 : a, b, c, d
arr.forEach( el => {console.log(el)} )
arr.forEach((el, i) => {console.log(el})
요소, 인덱스값 뽑아오기(el,i순서 못바꿈)

while

최초식, 조건식, 증감식 분할 사용, for과 동일
while()
let count = 0; 최초식
while( count !== 10 ){ 조건식
count = count + 1; 증감문
console.log(count)
} //결과 : 1,2,3,4,5,6,7,8,9,10

사용: for문 이용했을 때 내가 몇 번 반복해야할지 모를 때 사용. 결과가 나올때까지 무한하게 수행하겠다.

profile
Strive for greatness

0개의 댓글