Assignment, increment & decrements operators
Notes:
I. C# Assignment Operators: [Starts At: 01min:21sec]
II. C# Increment & Decrements Operators: [Starts At: 13min:43sec]
I. C# Assignment Operators: [Starts At: 01min:21sec]
Assignment operator is used to assign a value to a variable or constant.
Assignment Operator :
=
SHA (Short Hand Assignment ) or AA (Arithmetic Assignment):
+=, -=, *=, /=, %=
Note:
Left hand side of an assignment operator must be a variable
Associativity: Left to right
Example Code:
using UnityEngine;
public class OperatorsDemo : MonoBehaviour {
// Use this for initialization
void Start ()
{
Debug.Log ("Assignment Operators");
int a = 10; // assignment or initialization
Debug.Log(a); // 10
a = 20; // assignment
Debug.Log(a); // 20
a = a + 10;
Debug.Log (a); // 30
// SHA - Short Hand Assignment operators
a += 10; // a = a + 10
Debug.Log(a); // 40
a -= 10; // a = a - 10
Debug.Log (a); // 30
}
}
II. C# Increment & Decrements Operators: [Starts At: 13min:43sec]
C# increment (++) operator:
increments the variable value by 1.
C# decrement (--) operator:
decrements the variable value by 1.
Example Code:
using UnityEngine;
public class OperatorsDemo : MonoBehaviour {
// Use this for initialization
void Start ()
{
Debug.Log ("Increment and decrement operators");
// Pre increment & pre decrement
int a = 10, b = 10;
Debug.Log (a); // a = 10
Debug.Log(++a); // a = a + 1; // a = 11
//a=a;
Debug.Log(a); // a = 11
Debug.Log (b); // b = 10
Debug.Log(--b); // b = b - 1; // b = 9
// b= b
Debug.Log(b); // b = 9
// Post increment & post decrement
int c = 10, d = 10;
Debug.Log (c); // c = 10
Debug.Log(c++); // c = c ; // c = 10
//c = c + 1;
Debug.Log(c); // c = 11
Debug.Log (d); // d = 10
Debug.Log(d--); // d = d // d = 10
// d = d - 1;
Debug.Log(d); // d = 9
int e=10;
e++; // or ++e; // when not in function call
Debug.Log(e); // e = 11 // not difference in output
char ch='A';
ch++; // with character type data
Debug.Log(ch); // B
Debug.Log((int)ch); // 66
}
}