[Swift] Playgrounds Learn to Code 1-04. Conditional Code

BOMY·2022년 8월 5일

Playgrounds

목록 보기
4/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

👉 Conditional Code (조건문) - How do you plan for the unexpected?

  • Code that runs only when specific conditions are met. For example, if statements and while loops contain conditional code that runs only if or while a condition is true. 특정 조건을 만났을 때만 작동하는 코드. 예를 들면, if문과 while 반복문은 조건이 참인 경우에만 실행되는 조건문이 포함되어 있다.

    
    If you hit a traffic jam, you may need to change your route.
    
    In code, you plan for different conditions using an if statement.
    
    If the light is green, Byte walks forward across the street.
      if lightIsGreen {
      	Byte walks forward
        across the street
      }
      
    Here, lightIsGreen is true, so Byte walks forward.
       if lightIsGreen {
      	moveForward()
      } else {
      	wait
      }
    
  • To write an if statement, use if and add your condition, which can be true or false. Then add the if block-the commands that run if the condition is true. If condition is false, you can use else to specify other code to run.

  • Conditional Code part words
    • execute: To execute someone means to kill them as a punishment for a serious crime. But, if you execute a difficult action or movement, you successfully perform it. 사람의 경우 사형하다로 쓰이며, 행동이나 행위와 함께 쓰이는 경우 성공적으로 해내는 것(실행하다)
    • Boolean: A type that has a value of either true or false. For example, 9 < 7 returns a Boolean value of false because 9 is not less than 7 참 혹은 거짓 중 하나의 값을 가지는 자료형. 예를 들면 '9 보다 7이 크다'는 9는 7보다 크지 않으므로 거짓이라는 불리언 값을 반환한다.
    • wireframes: An outline of an item, such as a gem, switch, or block, in the puzzle world. A wireframe is used to indicate the possibility that the item could randomly appear each time the puzzle is run. 퍼즐 월드의 블록이나 스위치, 쳄과 같은 아이템의 외곽선을 말한다. 와이어프레임은 퍼즐이 작동할 때마다 아이템들이 무작위로 나타날 수 있는 가능성을 보여준다.

👉 Conditional Code - If statement

  • If statement is a code structure used to run code based on the value of one or more conditions. The first block of code in an if statement is the if block. An if statement might also contain other blocks that provide additional checks, such as an else block and else if block. 하나 혹은 그 이상의 조건의 값을 기준으로 작동하기 위해 사용하는 코드 구조. if문의 첫번째 코드블록은 if 블록이다. if문에는 또한 else 블록이나 eles if 블록처럼 추가적인 확인을 제공하는 다른 블록도 포함될 수 있다.

  • else if is a block of code in an if statement, used to check a condition that isn't checked by the if block or any other else if blocks. if 블록이나 else if 블록에서 확인하지 않은 조건을 확인하는 데 사용하는 코드 블럭


👉 예제문제 - Conditional Code

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

  • Defining Smarter Functions (젬과 스위치 랜덤으로 바뀜)
    func collectOrToggle() {
      moveForward()
      moveForward()
      if isOnClosedSwitch {
      	toggleSwitch()
       } else if isOnGem {
       	collectGem()
       }
    }
    
    collectOrToggle()
    TurnLeft()
    moveForward()
    moveForward()
    turnLeft()
    collectOrToggle()
    turnRight()
    moveForward()
    turnRight()
    collectOrToggle()

👉 한 줄로 요약한 Conditional Code

Conditional Code(조건문)특정한 조건을 마주했을 때 작동하는 코드 이다.
ex) if -> 이 조건을 만족한다면 다음 코드를 실행해! while -> 이 조건을 만족하는 동안 이 코드를 반복해!

  • 추가적으로, If statement(if 조건문)하나, 혹은 그 이상의 조건에 대한 값을 기준으로 작동하는 코드이다.



이사, 멋사자소서 준비로 좀 오래 걸린...
22.08.07, 10, 11, 26 TIL 끝!


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

profile
한 걸음 한 걸음!

0개의 댓글