12/5 TIL 연산자

handaewon·2023년 12월 6일
0

연산자

1)산술 연산자

  • 덧셈
  • +=
    왼쪽 변수에 있는 값을 꺼낸 다음에 오른쪽 변수의 값을 더해서 왼쪽 변수에 넣어줍니다.
  • 뺄셈
  • -=
    왼쪽 변수에 있는 값을 꺼낸 다음에 오른족 변수에 값을 빼서 왼쪽 변수에 넣어줍니다.

곱셈
2*3

나눗셈
/

나머지
%

var result = 5 + 9
print(result)

result += 3
print(result)

result = 14 - 7
print(result)

result -= 3
print(result)

result = 9 * 8
print(result)

result = 32 / 4
print(result)

result = 19 % 2
print(result)

✅비교연산자
비교연산자는 비교한 값을 true와 false로 반환합니다.

  • 같다 / 같지않다
  • a == b
  • !=
  • 크다/작다
  • a > b (a가 b보다 크다)
  • a < b (a가 b보다 작다)
  • 크거나 같다 / 작거나 같다
  • a >= b (a가 b보다 크거나 같다)
  • a <= b (a가 b보다 작거나 같다)
var result = (3 == 4)
print(result)

result = (3 != 4)
print(result)

result = (3 > 4)
print(result)

result = (3 < 4)
print(result)

result = (3 >= 4)
print(result)

result = (7 <= 7)
print(result)코드를 입력하세요

✅논리 연산자
논리 연산자는 비교한 값을 true와 false로 반환합니다.

  • 논리 부정 NOT
    !a
    논리 부정 연산자는 true를 false로, false 를 true로 변환합니다.
    • 논리 곱 AND
      • a && b
        • 논리 곱 연산자는 두 값(a, b)이 모두 true일 때 true를 반환합니다.
        • 첫번째 값이 false라면 두 번째 값을 판단하지 않고 즉시 false를 반환합니다.
    • 논리 합 OR
      • a || b
        • 논리 합 연산자는 둘 중 하나가 true면 true를 반환합니다.
        • 첫번째 값이 true라면 두 번째 값을 판단하지 않고 true를 반환합니다.
var allowedEntry = false
allowedEntry = !allowedEntry
print(allowedEntry)

let enteredDoorCode = true
let passedRetinaScan = false
let permittedAccess = enteredDoorCode && passedRetinaScan
print(permittedAccess)

let enter = allowedEntry || permittedAccess
print(enter)
  • ☑️ 범위 연산자
    • (a…b)
      • a 이상 b 이하
    • (a..<b)
      • a이상 b 미만
      • b는 범위에 포함되지 않습니다.
    • a... ...a
      • 범위의 시작 혹은 끝만 지정하여 사용하는 범위 연산자입니다.
      • a는 범위에 포함됩니다.
let names = ["Elsa", "Miley", "Sonny", "Gabi"]

for name in names[3...] {
    print(name)
} //Gabi

for name in names[..<2] {
    print(name)
} //Elsa, Miley

☑️ 삼항 조건 연산자

  • a ? b : c
    • question ? answer1 : answer2
    • 삼항 연산자는 3가지 부분으로 이루어진 연산자입니다.
    • question이 true 또는 false인지에 따라 2개의 표현식 중 하나를 나타내는 식입니다.
    • question의 답이 true이면 answer1을, false면 answer2 값을 사용합니다.
let contentHeight = 70
let hasHeader = true
let rowHeight: Int
if hasHeader {
    rowHeight = contentHeight + 60
} else {
    rowHeight = contentHeight + 40
}
profile
iOS Developer

0개의 댓글

Powered by GraphCDN, the GraphQL CDN