
ex) 논리 연산자가 뭐예요? 왜 써요? 종류는요?For loopis used to repeat a specific block of code a known number of times. For 반복문은 특정 코드블록을 알려진 횟수만큼 반복해서 사용하는 것을 말한다.
Imagine you're helping someone plant seeds in a garden.
They might say...
For each of these 4 seeds:
make a hole
place the seed
move five inches forward
To write a for loop,
use for and include the number of times the loop will run.
ex)
for eachSeed in 1 ... 4 {
make a hole
place the seed
move five inches forward
}
Add the commands to repeat within the curly braces'{}'.
For loops part words
A block of code that's repeated a certain number of times(ex. a for loop) or until a condition is met(ex. a while loop) 특정 횟수대로 반복되거나(예: For 반복문) 조건을 만날 때까지 반복되는(예: while 반복문) 코드 블럭(예제 문제들 중 더 클린하게 작성할 수 있을 지 고민이 필요한 코드 모음)
Branch Out

func goToggle() {
for i in 1 ... 7 {
moveForward()
}
}
func turnAround() {
turnLeft()
turnLeft()
}
func goForward() {
moveForward()
moveForward()
}
for i in 1 ... 3 {
goForward()
turnRight()
goToggle()
toggleSwitch()
turnAround()
goToggle()
turnRight()
}
Gem Farm

func goToggleSwitch() {
for i in 1 ... 2 {
moveForward()
toggleSwitch()
}
}
func goCollectGem() {
for i in 1 ... 2 {
moveForward()
collectGem()
}
}
func turnAndForward() {
turnLeft()
turnLeft()
moveForward()
moveForward()
}
func nextStep() {
turnRight()
moveForward()
turnLeft()
}
turnLeft()
for i in 1 ... 3 {
goToggleSwitch()
turnAndForward()
goCollectGem()
turnAndForward()
nextStep()
}
Four Stash Sweep

func moveAndCollect() {
moveForward()
collectGem()
}
func turnAround() {
turnLeft()
turnLeft()
}
func collectForGems() {
moveAndCollect()
moveAndCollect()
turnAround()
moveForward()
turnRight()
moveAndCollect()
turnAround()
moveForward()
moveAndCollect()
moveForward()
}
for i in 1 ... 4 {
collectForGems()
}
For loops(반복문)는 특정 코드를 정해진 횟수대로 반복해서 사용하는 것이다.
ex) 저 캐릭터를 앞으로 네 번 움직이게 해줘!
22.08.06 TIL 끝!
썸네일은 Banner Maker로 제작하였습니다.