Operators

In this lesson we will go on all types of operators that any developer must know (not only dart programmers). To be a good developer you need to master these operators so read them carefully and try to take notes before moving to exercises.

  1. Arithmetic Operators: (easy)

  2. 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)


  3. Assignment Operators: (shorter)
  4. Assignment operators assign values to dart variables.


    Operator Description Example Same As
    = assign x=y
    += add & assign x+=y x=x+y
    -= subtract & assign x-=y x=x-y
    *= multiply & assign x*=y x=x*y
    /= divide & assign x/=y x=x/y
    ~/= divide & assign (integer result) x~/=y x=x~/y
    %= mod & assign x%=y x=x%y


  5. Unary Operators: ( practical )

  6. Operator Description Example
    ++x the value of x after increment x=5; y=++x; //x = 6 & y= 6
    x++ the value of x before increment x=10; y=x++; //x=11 & y=10
    --x the value of x after decrement x=20 ; y=--x ; //x=19 & y=19
    x-- the value of x before decrement x=50 ; y=x-- ; // x=49 & y50;


  7. Relational Operators : (easy)

  8. Operator Meaning
    == Equal
    != Not Equal
    > Greater Than
    < Less Than
    >= Greater than or equal to
    <= Less than or equal to


  9. Loogical Operators : (catch the syntax)

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


  11. Type Test Operators : (you will get familiar with them sooner)
  12. The as, is, and is! operators are handy for checking types at runtime


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


  13. Conditional Operators:(you will get familiar with them sooner)

    • condition ? expr1 : expr2

    • If condition is true, evaluates expr1 (and returns its value); otherwise, evaluates and returns the value of expr2.


    • expr1 ?? expr2

    • If expr1 is non-null, returns its value; otherwise, evaluates and returns the value of expr2.