## Operators, Arithmetic Operators

## Notes:

I. Introduction to operators : [ Starts At: 00min:00sec]

II. Arithmetic operators : [ Starts At: 03min:43sec]

I. Introduction to operators : [ Starts At: 00min:00sec]

Operators in C#:

C# has rich set of built-in operators used to perform various operations like arithmetic, logical, relational etc. on data of different types

Unary Operators: accept only one operand

Ex: !, sizeof, +, -, ++, --, ~ etc.

Binary Operators: accept two operands

Ex: Others are binary

Ternary Operators: accept three operands

Ex: ?:

Different types of operators in C#:

L - logical operators

A - arithmetic operators

R - relational operators

A - assignment operators

(C# Shorthand Arithmetic Assignment SAA operators )

B - bit-wise operators

I - increment and decrements operator

C - conditional operator

S - special operators

II. Arithmetic operators : [ Starts At: 03min:43sec]

Example Code:

using UnityEngine;

public class OperatorsDemo : MonoBehaviour {

// Use this for initialization

void Start ()

{

Debug.Log ("Arithmetic Operators");

Debug.Log (2 + 3); // 5 (sum) Movement

Debug.Log (2 - 3); //-1 (difference) Movement

Debug.Log (2 * 3); // 6 (product) scaling

Debug.Log (10 / 2); // 5 (quotient) descale

Debug.Log (10 % 2); // 0 (remainder) limitation

Debug.Log ('A' + 'A'); // 65 + 65 = 130

Debug.Log ("Hello " + "Unity!"); // Hello Unity! (Concatenation)

Debug.Log (9 / 2); // 4

Debug.Log ((float)9 / (float)2); // 4.5

Debug.Log (9.0f / 2.0f); // 4.5

// Debug.Log(9/0); // int/int = divided by zero exception

Debug.Log(9.0f/0.0f); // float/float = "Infinity" as the output

}

}

Limiting the result using % (mod) operator:

0 % 2 = 0

1 % 2 = 1

2 % 2 = 0

3 % 2 = 1

4 % 2 = 0

5 % 2 = 1

Note:

1. Try to reduce division operations,

If possible convert division expressions to multiplication

Because division operations take more time than multiplication.

2. Try to reduce floating point operations,

If possible convert floating point expression or variables to integer

Because floating point takes more time and space than integers