A loop is a programming tool that repeats a set of instructions until a specified condition, called a stopping condition is reached. As a programmer, you’ll find that you rely on loops all the time! You’ll hear the generic term iterate when referring to loops; iterate simply means “to repeat”.
[The For Loop}
The typical for loop includes an iterator variable(반복기 변수) that usually appears in all three expressions. The iterator variable is initialized, checked against the stopping condition, and assigned a new value on each loop iteration. Iterator variables can have any name, but it’s best practice to use a descriptive variable name.
A for loop contains three expressions separated by ; inside the parentheses:
for (let counter = 0; counter < 4; counter++) {
console.log(counter);
}
initialization(초기선언문)
let counter = 0
정지조건은 ```counter < 4```임. 4보다 작을때까지 반복이 됨.
반복문은 ```counter++```. 각 루프에서 1씩 증가하는것을 뜻함. 첫번째에서는 0일테고, 두번째 반복에서는 1, …
{} 안의 코드블럭은 값이 ‘false’일때까지 실행되게 됨.
This for loop makes it possible to write 0, 1, 2, and 3 programmatically.
[Looping in Reverse]
도 응용할 수 있음.
[Looping through Arrays]
for loops are very handy for iterating over data structures.
반복적인 데이터 구조에서 매우 유용한 ```for```루프.
array의 각각의 요소에 반복적인 operation을 작동시킬때 사용할 수 있습니다. array는 ‘고객이름’, ‘제품정보’같은 데이터 리스트’를 나타냅니다.
array의 각 요소를 반복하려면 for 루프가 해당 조건에서 배열의 .length 속성을 사용해야합니다.
const animals = ['Grizzly Bear', 'Sloth', 'Sea Lion'];
for (let i = 0; i < animals.length; i++){
console.log(animals[i]);
}
Grizzly Bear
Sloth
Sea Lion
I(index의 약자).
중지 조건이 i가 animals.length보다 작을때임.
Remember that arrays are zero-indexed, the index of the last element of an array is equivalent to the length of that array minus 1.
const vacationSpots = ['Bali', 'Paris', 'Tulum'];
// Write your code below
for (let i = 0; i < vacationSpots.length; i++){
console.log('I would love to visit ' + vacationSpots[i])
}