250x250
반응형
05-12 12:59
Today
Total
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Notice
Recent Posts
Recent Comments
Link
Archives
관리 메뉴

Bill Kim's Life...

[Dart] Operators(연산자) 본문

CS(컴퓨터 과학)/Dart

[Dart] Operators(연산자)

billnjoyce 2020. 8. 19. 15:35
728x90
반응형
Dart 언어에서의 Operators(연산자)에 대하여 살펴봅니다.

 

 

#. 구독 대상

  • Dart 언어를 처음 접하시면서 공부해보고 싶으신 분
  • 플러터(Flutter) 개발에 관심이 있어나 해보고 싶으신 분
  • 멀티 플랫폼 모바일 앱 개발을 시작하고 싶으신 분
  • 기타 소프트웨어 개발과 지식에 관심이 있으신 모든 분들

 

 


 

Operatoras

 

Dart에서도 다양한 형태의 연산자를 제공합니다.

아래의 표는 Dart에서 제공되는 모든 연산자들에 대해서 나타낸 표입니다.

 

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.

 

이번 강의에서는 위의 표에 있는 연산자 중에서 대표적인 연산자들에 대해서 코드 예시와 함께 살펴보도록 하겠습니다.

 

 

 


 

 

Arithmetic operators(산술 연산자)

 

assert(2 + 3 == 5);
assert(2 - 3 == -1);
assert(2 * 3 == 6);
assert(5 / 2 == 2.5); // Result is a double
assert(5 ~/ 2 == 2); // Result is an int
assert(5 % 2 == 1); // Remainder

assert('5/2 = ${5 ~/ 2} r ${5 % 2}' == '5/2 = 2 r 1');

 

 

var a, b;

a = 0;
b = ++a; // Increment a before b gets its value.
assert(a == b); // 1 == 1

a = 0;
b = a++; // Increment a AFTER b gets its value.
assert(a != b); // 1 != 0

a = 0;
b = --a; // Decrement a before b gets its value.
assert(a == b); // -1 == -1

a = 0;
b = a--; // Decrement a AFTER b gets its value.
assert(a != b); // -1 != 0

 

 

 


 

 

비교(관계) 연산자(Equality and releational operators)

assert(2 == 2);
assert(2 != 3);
assert(3 > 2);
assert(2 < 3);
assert(3 >= 3);
assert(2 <= 3);

 

 

 


 

 

Type test operators(타입 검사 연산자)

(emp as Person).firstName = 'Bob';	

if (emp is Person) {
  // Type check
  emp.firstName = 'Bob';
}

 

 

 


 

 

Assignmet operators(할당 연산자)

// Assign value to a
a = value;
// Assign value to b if b is null; otherwise, b stays the same
b ??= value;

var a = 2; // Assign using =
a *= 3; // Assign and multiply: a = a * 3
assert(a == 6);

 

 

 


 

 

Logical operators(논리 연산자)

if (!done && (col == 0 || col == 3)) {
  // ...Do something...
}

 

 

 


 

 

Bitwise and shift operators(비트 연산자)

final value = 0x22;
final bitmask = 0x0f;

assert((value & bitmask) == 0x02); // AND
assert((value & ~bitmask) == 0x20); // AND NOT
assert((value | bitmask) == 0x2f); // OR
assert((value ^ bitmask) == 0x2d); // XOR
assert((value << 4) == 0x220); // Shift left
assert((value >> 4) == 0x02); // Shift right

 

 


 

 

Conditional expressions(조건 연산자)

var visibility = isPublic ? 'public' : 'private';

String playerName(String name) => name ?? 'Guest';

// Slightly longer version uses ?: operator.
String playerName(String name) => name != null ? name : 'Guest';

// Very long version uses if-else statement.
String playerName(String name) {
  if (name != null) {
    return name;
  } else {
    return 'Guest';
  }
}

 

 


 

 

Cascade notation(...)

 

Dart에서는 계단식 (..) 표기법을 사용하여 동일한 객체에 대해 일련의 작업을 수행할 수 있도록 해주는 특별한 기능이 있습니다.  함수 호출 외에도 동일한 객체의 필드에 액세스 할 수 있습니다. 이것은 종종 임시 변수를 만드는 단계를 생략하고보다 유동적 인 코드를 작성할 수 있도록합니다.

var button = querySelector('#confirm');
button.text = 'Confirm';
button.classes.add('important');
button.onClick.listen((e) => window.alert('Confirmed!'));


final addressBook = (AddressBookBuilder()
      ..name = 'jenny'
      ..email = 'jenny@example.com'
      ..phone = (PhoneNumberBuilder()
            ..number = '415-555-0100'
            ..label = 'home')
          .build())
    .build();
    
    
var sb = StringBuffer();
sb.write('foo')
  ..write('bar'); // Error: method 'write' isn't defined for 'void'.

 

 


 

 

기타 연산자

Dart에서는 아래의 기타 기능을 하는 연산자를 제공합니다.

  • () : 함수 호출
  • [] : 목록(LIST) 접근, 목록의 인덱스로 접근
  • . : 맴버 접근, foo.bar 와 같이 특정 속성(foo)의 값(bar)에 접근 할 수 있다.
  • ?. : 선택적 맴버 접근 위와 유사하나 foo?.bar 와 같이 접근하면, 만약 foo 가 null 인경우 오류를 발생하지 않고 null을 반환한다.

 

 

 


 

 

 

지금까지 Dart 언어에서의 다양한 연산자(Operators)에 대해서 살펴보았습니다.

 

감사합니다.

 

 

 

 

 

 

 

 

 


[참고 자료(References)]

 

[1] A tour of the Dart language : dart.dev/guides/language/language-tour

[2] 다트 연산자(Dart Operator) : brunch.co.kr/@mystoryg/120

[3] [FLUTTER] DART 언어 기초과정 - 2 / A Tour of the Dart Language : steemit.com/dart/@wonsama/flutter-dart-2-a-tour-of-the-dart-language

[4] [Dart] 다트 튜토리얼 - 연산 : m.blog.naver.com/dsz08082/221853958113

[5] 다트(Dart) 언어 기본기 다지기 : blog.doldol.me/dart/2019-11-16-Dart-기본기-다지기/

728x90
반응형

'CS(컴퓨터 과학) > Dart' 카테고리의 다른 글

[Dart] Functions(함수)  (0) 2020.08.20
[Dart] Control flow statements(제어문)  (0) 2020.08.20
[Dart] Collection : List & Set & Map  (0) 2020.08.19
[Dart] Variables(변수)  (0) 2020.08.19
[Dart] 기초 문법  (0) 2020.08.19
Comments