[Swift] Playgrounds Learn to Code 1-05. Logical Operators

BOMY·2022년 8월 26일

Playgrounds

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

👉 Logical Operators (논리연산자)

  • Logical Operators isa type of an operator that you can use to make your conditional code more specific. 조건문을 더 명확하게 만드는 부호(기호)이다.

    Earlier, you used an if statement to plan for different situations.
    But what if you could make your conditional code even more powerful?
    	if lightIsGreen {
      		moveForward()
        } else {
          	wait()
        }
     
     In code, an operator is a symbol which represents an action.
    
    The Logical AND (&&) Operator
    This is code runs only if all conditions are true.
    
    if blocked AND on a gem:
        collect gem
        turn around
      
    if isBlocked && isOnGem {
        collectGem()
        turnAround()
    }
    
    The Logical OR (||) Operator
    This code runs if at least one condition is true.
    
    if blocked AND on a gem:
        collect gem
        turn around
      
    if isBlocked && isOnGem {
        collectGem()
        turnAround()
    }
    
    The Logical NOT (!) Operator
    This operator changes a condition to its opposite.
    For example, if onGem is true, !onGem is false.
    
    if NOT blocked:
    	fly away
      
    if !isBlocked {
    	flyAway()
    }
    
  • Each of these three operators changes conditions in its own specific way:

    • AND > &&
    • OR > ||
    • NOT > !
  • The Logical NOT (!) Operator reversed a Boolean(A type that has a value of either true or flase. For example 9<7 returns a Boolean value of false because 9 is not less than 7) Value.

  • Logical Operators part words

    • logic: reasoning conducted or assessed according to strict principles of validity 강력한 적합성의 원칙에 따라 수행하거나 평가된 추론, 논리
    • logical: of or according to the rules of logic or formal argument 논리 규칙이나 정해진 논쟁에 따르는 것, 논리적
    • reasoning: the action of thinking about something in a logical, sensible way 어떤 것에 대해 논리적이고 현명한 방법으로 생각하는 행위, 추론
    • conduct: organize and carry out 조직화하고 실행하는 것, 수행하다
    • assess: evaluate or estimate the nature, ability, or quality of 성질, 능력, 품질을 평가하거나 추정하는 것, 평가하다
    • sensible: chosen in accordance with wisdom or prudence; likely to be of benefit 지혜나 ~를 이용하여 선택하는 것, 이익이 되도록, 현명하다
    • accordance: conformity or agreement 순응 혹은 동의
    • conformity: compliance with standards, rules, or laws 관습, 규칙이나 법에 순응하는
    • compliance(동사 comply): the action or fact of complying with a wish or command 요청이나 명령을 따르는 사실이나 행동, 준수
    • organize: arrange into a structured whole; order 구조화된 것을 배열하는 것, 조직화하다
    • arrange: put (things) in a neat, attractive, or required order 어떤것을 정돈되거나, 특징이나, 주어진 조건에 의해 놓는 것, 배열하다
    • neat: (of a place or thing) arranged in an orderly, tidy way 순서나 방법에 맞춰서 배열된 것, 정돈된
    • attractive: (of a thing) having beneficial qualities or features that induce someone to accept what is being offered 어떤 사람이 제공된 것을 수용하도록 설득하는 특별한 특성 혹은 특징, 표면적인 특징?(흥미있는, 매력있는)

👉 예제문제 - Logical Operators

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

  • Using the NOT Operator
  func comeBack() {
    turnLeft()
    moveForward()
    moveForward()
    collectGem()
    turnLeft()
    turnLeft()
    moveForward()
    moveForward()
    turnLeft()
}

for i in 1 ... 4 {
    moveForward()
    if !isOnGem {
        comeBack()
    } else {
        collectGem()
    }
}

for i in 1 ... 7 {
    moveForward()
    if isOnGem {
        collectGem()
    }
    if isBlockedLeft {
        turnRight()
        while !isBlocked {
            moveForward()
        }
        if isBlocked && isOnClosedSwitch {
            toggleSwitch()
        }
        turnLeft()
        turnLeft()
        while !isBlocked {
            moveForward()
        }
        turnRight()
    }
}

for i in 1 ... 8 {
	moveForward()
    if isOnClosedSwitch && isOnGem {
    	collectGem()
        toggleSwitch()
        turnRight()
        moveForward()
        moveForward()
        collectGem()
        turnRight()
        turnRight()
        moveForward()
        moveForward()
        turnRight()
    } else if !isOpenSwitch {
    	toggleSwitch()
        turnLeft()
    } else if isOnGem {
    	collectGem()
    }
}

👉 한 줄로 요약한 Logical Operators

Logical Operators(논리 연산자)조건문의 조건을 세부하게 나타낼 수 있는 부호 이다.
ex)




22.09.22, 23, 26, 27 Playgrounds TIL 끝!


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

profile
한 걸음 한 걸음!

0개의 댓글