본문 바로가기
Flutter/Dart의 정석

[Dart의 정석] Ch2.5. 연산자(Operators)

by Couldi 2022. 5. 15.
반응형

22. 5. 15.

- Could - 

이 글은 다트를 공부하고자 하는 모든 사람들을 위한 글이 아니다. 다트라는 언어의 전문가가 되기를 위한 사람들을 위한 글이기 때문에, 프로그래밍을 처음 공부하는 사람들이 보기에는 부적절할 수 있다. 프로그래밍의 초심자라면 본 블로그에 'Dart 입문'을 살펴보거나, 다른 컨텐츠를 찾아 공부하는 편이 더 나을 것이다.
가능한 이해하기 쉽고 일목요연하게 정리하고자 하지만, 다소 설명이 난해하거나 이해하기 어려울 수 있다. 그런 경우 댓글을 통해 궁금한 사항을 남겨두면 필자가 언젠가 보고 좀 더 상세한 설명을 덧붙일 수 있으니 많은 댓글을 바란다.
추가적으로 이 글은 Flutter(플러터)에 대해 다루지 않는다. Dart에 대한 내용만을 다룰 예정이다.

1. 연산자(Operator)

프로그래밍에서 연산이란 데이터를 처리하는 것을 말한다. 우리가 일반적으로 아는 덧셈, 뺄셈, 곱셈, 나눗셈 같은 것도 연산자이다. Dart의 연산자는 상당히 많다.

https://dart.dev/guides/language/language-tour#operators

 

A tour of the Dart language

A tour of all the major Dart language features.

dart.dev

Description Operator
unary postfix expr++    expr--    ()    []    ?[]    .    ?.    !
unary prefix -expr    !expr    ~expr    ++expr    --expr      await expr   
multiplicative *    /    %  ~/
additive +    -
shift <<    >>    >>>
bitwise AND &
bitwise XOR ^
bitwise OR |
relational and type test >=    >    <=    <    as    is    is!
equality ==    !=   
logical AND &&
logical OR ||
if null ??
conditional expr1 ? expr2 : expr3
cascade ..    ?..
assignment =    *=    /=   +=   -=   &=   ^=   etc.

전부 알아야하는가? 라고 물어본다면.. 굳이 전부를 알필요는 없지만 어차피 프로그래밍을 계혹하다보면 거의다 알게 될꺼다.

조금 더 편하게 머리속에 집어 넣기 위해 그룹화를 시켜서 알아보자.

 

1.1 산술연산자(Arithmetic operator)

우리에게 가장 익숙한 연산자다. +, -, x, ÷ 기호를 사용해, 산수할때 사용하는 연산자를 의미한다.

대충 위 내용을 보고 각 연산자의 기능을 추리해보자.

Operator Meaning
+ Add
Subtract
-expr Unary minus, also known as negation (reverse the sign of the expression)
* Multiply
/ Divide
~/ Divide, returning an integer result
% Get the remainder of an integer division (modulo)

각 연산자의 기능은 위의 표와 같다.

 

증감연산자

Operator Meaning
++var var = var + 1 (expression value is var + 1)
var++ var = var + 1 (expression value is var)
--var var = var – 1 (expression value is var – 1)
var-- var = var – 1 (expression value is var)

변수를 1씩 증가시키거나 1씩 감소시키는 연산자이다. 전위 증감연산자, 후위 증감연산자로 나뉘고 이 둘이 작동하는데 있어 미묘한 차이가 있지만, 나중에 프로그래밍을 하다가 알아볼 일 생겼을때 알아보는게 훨씬 정신건강에 좋다. 지금은 앞에 기호를 붙이느냐, 뒤에 기호를 붙이느냐에 따라 차이가 존재한다는 사실만 알고 넘어가자.

 

 

1.2. 비교연산자(relational operator)

영어를 직역해 관계연산자라고 이야기도 하지만, 비교연산자라고 하는편이 좀 더 이해하기 편하므로 한글로는 비교연산자라고 하겠다. 연산자 앞뒤로 있는 항을 비교하여 참인지 거짓인지 알려주는 연산자이다.

결과는 true, false로 나오게 된다.

Operator Meaning
== Equal; see discussion below
!= Not equal
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

각 뜻은 저렇다. 앞서 변수를 할당할 때 = 기호를 사용했으므로, 같다라는 의미를 표현하기 위해서 =을 두개 연달아 ==로 사용한다. !는 프로그래밍에서 일반적으로 not의 의미를 가진다. 따라서 !=는 같지 않다는 뜻이 된다.

 

 1.3. 논리연산자(logical operation)

Operator Meaning
!expr inverts the following expression (changes false to true, and vice versa)
|| logical OR
&& logical AND

위에서 부터 순서대로, not, or, and를 의미하는 연산자이다. true나 false의 결과값을 가지는 항을 다룰때 사용한다.

// true와 false 앞에 !를 붙이면
!true // false
!false // true

// ||로 true와 false를 나타내면..
true || true // true
true || false // true
false || true // true
false || false // false

// &&로 true와 false를 나타내면..
true && true // true
true && false // false
false && true // false
false && false // false

 

1.4. 타입테스트 연산자(Type test operators)

Operator Meaning
as Typecast (also used to specify library prefixes)
is True if the object has the specified type
is! True if the object doesn’t have the specified type

각 변수나 객체의 타입을 변환하거나 타입 확인시 사용하는 연산자이다.

 

1.5. 삼항연산자(Ternary access operator)

https://couldi.tistory.com/38?category=927475 

 

[Dart] 15. 삼항연산자 - Ternary Operator

21. 11. 15. - Could - 이 글은 프로그래밍 입문을 Flutter 때문에 Dart로 시작하는 사람들을 위한 글입니다. 프로그래밍 언어가 가지고 있는 기본 컨셉 자체를 Dart라는 언어를 통해 설명하고, 많은 분들

couldi.tistory.com

위 글로 설명을 대체한다.

 

1.6. 기타

본 글에서 설명하지 않은 연산자들도 많다. 위의 연산자들에 대한 설명이 이해가 갔다면, 한번 dart doc의 operators 부분을 정독할 힘이 생겼을 것이니 나머지 내용은 아래 링크를 통해 확인해보길 바란다.

https://dart.dev/guides/language/language-tour#operators

 

A tour of the Dart language

A tour of all the major Dart language features.

dart.dev

그리고 Dart를 학습하다 많은 사람들이 어려움을 겪는 NullSafety와 관련해서도 몇몇 연산자들이 존재한다. 이부분은 여기서 눈도장만 찍어두고, 추후 NullSafety를 설명한 이후 설명하도록 하겠다.

 

Operator  Name Meaning
() Function application Represents a function call
[] Subscript access Represents a call to the overridable [] operator; example: fooList[1] passes the int 1 to fooList to access the element at index 1
?[] Conditional subscript access Like [], but the leftmost operand can be null; example: fooList?[1] passes the int 1 to fooList to access the element at index 1 unless fooList is null (in which case the expression evaluates to null)
. Member access Refers to a property of an expression; example: foo.bar selects property bar from expression foo
?. Conditional member access Like ., but the leftmost operand can be null; example: foo?.bar selects property bar from expression foo unless foo is null (in which case the value of foo?.bar is null)
! Null assertion operator Casts an expression to its underlying non-nullable type, throwing a runtime exception if the cast fails; example: foo!.bar asserts foo is non-null and selects the property bar, unless foo is null in which case a runtime exception is thrown
??=
??
Null-aware operator  

반응형

댓글