Relational, Logical, Conditional operators
Notes:
I. C# Relational operators : [Starts At: 01min:44sec]
II. C# Logical operators : [Starts At: 07min:46sec]
III. C# Conditional operator : [Starts At: 18min:56sec]
I. C# Relational operators : [Starts At: 01min:44sec]
Relational operators compare values and result in true or false.
Relational operators are also called as comparison operators.
Example code:
using UnityEngine;
public class OperatorsDemo : MonoBehaviour {
// Use this for initialization
void Start ()
{
Debug.Log ("Relational Operators");
Debug.Log (3 < 4); // true
Debug.Log (4 <= 4); // true
Debug.Log (2 > 3); // false
Debug.Log (2 >= 3); // false
// Should not use space inbetween operators
// Use double equal to for comparison
Debug.Log (5 == 5); // true
Debug.Log (5 != 5); // false
Debug.Log ('A' == 'A'); // true
}
}
II. C# Logical operators : [Starts At: 07min:46sec]
Logical operators compare two Boolean expressions and result in true or false.
Logical operators are also called as logical connectives.
They are used to connect one more relational expressions.
Example Code:
using UnityEngine;
public class OperatorsDemo : MonoBehaviour {
// Use this for initialization
void Start ()
{
Debug.Log ("Logical Operators");
// if both LHS and RHS are true then only the result will be true, otherwise false
Debug.Log ((3 < 4) && (4 < 5)); // True
Debug.Log ((3 > 4) && (4 < 5)); // False
// if both LHS and RHS are false then only the result will be false, otherwise true
Debug.Log ((3 > 4) || (4 > 5)); // False
Debug.Log ((3 < 4) || (4 > 5)); // True
// if true then false vice versa
Debug.Log (!(2 < 3)); // False
}
}
III. C# Conditional operator (?:): [Starts At: 18min:56sec]
Returns one of two values depending on the value of a Boolean expression.
Is the only ternery operator, which accepts 3 operands
It used in alternative to if else conditional statement.
Example Code:
using UnityEngine;
public class OperatorsDemo : MonoBehaviour {
// Use this for initialization
void Start ()
{
Debug.Log ("Conditional Operator");
int num = 11;
string result = (num % 2 == 0) ? "even" : "odd";
Debug.Log (num + " is " + result + " number"); // 11 is odd number
}
}