[Swift] Playgrounds Learn to Code 1-03. For loops

BOMY·2022년 8월 4일

Playgrounds

목록 보기
3/6
post-thumbnail

<TIL 작성 목표>

  • Playgrounds Learn to Code 1, 2의 챕터, 목차를 내 언어로, 한 문장으로 설명할 수 있을 때까지 해보기! 초등학생한테 이해시킬 수 있을 정도로!
  • 주변 개념까지 정리하고 Playground의 설명으로 충분하지 않으면 검색해서 찾아보고 내 생각을 찾아서 정리해 보기!
    ex) 논리 연산자가 뭐예요? 왜 써요? 종류는요?


나의 iOS 개발 커리큘럼

  • Playgrounds Learn to Code 1
    - Commands ✔
    - Functions ✔
    - For loops ✔
    - Conditional Code
    - Logical Operators
    - While Loops
    - Algorithms
  • Playgroubds Learn to Code 2
    - Variables
    - Types
    - Initialization
    - Parameters
    - World Building
    - Arrays
  • iOS App Dev Tutorials - 3개월간 2~3회독
  • Boostcourse - 6개월


Playground Learn to Code 1

👉 For loops (반복문) - Repeating Yourself

  • 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

    • loop: 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 반복문) 코드 블럭

👉 예제문제 - For loops

(예제 문제들 중 더 클린하게 작성할 수 있을 지 고민이 필요한 코드 모음)

  • 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

For loops(반복문)특정 코드를 정해진 횟수대로 반복해서 사용하는 것이다.
ex) 저 캐릭터를 앞으로 네 번 움직이게 해줘!

  • 추가적으로, 반복문은 몇 번 반복할지 횟수가 정해져 있어야하고, 중괄호{} 안에 반복할 명령문을 넣어야 한다.



22.08.06 TIL 끝!


썸네일은 Banner Maker로 제작하였습니다.

profile
한 걸음 한 걸음!

0개의 댓글