IT

day 4 - while 본문

카테고리 없음

day 4 - while

손님번호 2022. 4. 29. 12:54


Yes, I can use the condition after the while statement.

Quest 1

while !isBlocked {
moveForward()
if isOnOpenSwitch {
moveForward()
} else if isonClosedSwitch {
toggleSwitch ()
}
}

while 스테이트먼트 쓸때 첫 조건은 그냥 앞으로 나아가라고 해도 된다. 이 점을 생각해내기가 어려웠음.




Quest 2



func turnAndCollectGem() {
moveForward()
turnLeft()
moveForward()
collectGem()
turnRight()
}
while !isBlocked {
turnAndColllectGem ()
}

tip.
In coding, it's important to study a problem and choose from one of several available solutions. Sometimes, one approach can work just as well as another. Other times, an approach might end up being more efficent, reusable, or adaptable than others.


Quest 3

while !isBlocked {
moveForward()
if isOnClosedSwitch {
toggleSwitch()
turnRight()
} else if isOnOpenSwitch {
turnRight()
}
}


Quest 4


moveForward()
while isOnGem {
collectGem()
turnLeft()
moveForward()
if !isOnGem {
turnRight()
moveForward()
}
}

Right on!
Developing your own solutions for new problems enables you to figure out what types of tools work in certain situations. As your skills grow, you'll be able to choose those tools more quickly and intelligently. How awesome is that?

Quest 5


my answer )

for i in 1 ... 5 {
moveForward()
if isOnGem || isOnClosedSwitch {
collectGem()
toggleSwitch()
}
}
turnRight()
moveForward()
turnRight()
for i in 1 ... 5 {
moveForward()
if isOnGem || isOnClosedSwitch {
collectGem()
toggleSwitch()
}
}
turnLeft()
moveForward()
turnLeft()
for i in 1 ... 5 {
moveForward()
if isOnGem || isOnClosedSwitch {
collectGem()
toggleSwitch()
}
}



comment ) I hope it would be more simple but.. no time to think at this time.

Quest 6


while loop 를 두번이나 쓸 수 있다. 바깥 while, 안 while 이라고 불린다.

while !isBlocked {
while !isonGem {
moveForward()
}
collectGem()
turnLeft()
}
그러니까 첫번째 while에서 '막히지 않았다'면 다음 while을 진행한다.. - 이런 식으로 해석해야 할 것 같다.
그리고 이제 Gem이 나오면 'isonGem' 이 되니깐 식이 밑으로 갈 수가 있다. 결국 while + while 조건이라고 생각.
if 문과 쓰임이 같다.


tip) Be careful, though. if a while loop Boolean condition is never false, your code will run forever in an infinite loop, which can make your computer freeze.



Quest 7


while !isOnClosedSwitch {
while !isBlocked {
moveForward()
}
turnRight()
}
toggleSwitch()

tip) there was a problem at the end of this road. so I simply moved the code 'toggleSwitch()' to the end from the after the 'turnRight()'

Quest 8



Remember, it's okay if you don't get it on your first try! Professional coders hardly ever get things right the first time around.



solution )

while !isBlocked {
moveForward()
while isOnClosedSwitch || isOnGem {
toggleSwitch()
if isOnGem{
collectGem()
}
}

if isBlocked {
turnRight()
moveForward()
}

}






Comments