Parts 15 Common Operators
Assignment Operator =
Arithmetic Operators like +,-,*,/,%
Comparison Operators like ==, !=,>, >=, <, <=
Conditional Operators like &&, ||
Ternary Operator ?:
Null Coalescing Operator ??
Example: -
public class Program
{
public static void Main(string[] args)
{
// Assignment Operator
int i = 10;
bool b = true;
// For dividing 2 numbers
// % or / operators
int num = 10;
int num2 = 2;
// Arithmentic operator / returns quotient
int quotient = num / num2;
Console.WriteLine("Quotient = {0}", quotient);
// Arithmentic operator % returns remainder
int remainder = num % num2;
Console.WriteLine("Remainder = {0}", remainder);
// To compare if 2 numbers are
// equal use comparison operator ==
int number = 10;
if (number == 10)
{
Console.WriteLine("Number is equal to 10");
}
// To compare if 2 numbers are not
// equal use comparison operator !=
if (number != 5)
{
Console.WriteLine("Number is not equal to 5");
}
// When && operator is used all the conditions must
// be true for the code in the "if" block to be executed
int number1 = 20;
int number2 = 24;
if (number1 == 20 && number2 == 24)
{
Console.WriteLine("Both conditions are true");
}
// When || operator is used the code in the "if" block
// is excuted if any one of the condition is true
number1 = 16;
number2 = 24;
if (number1 == 10 || number2 == 20)
{
Console.WriteLine("Atleast one of the condition is true");
}
Console.ReadLine();
}
}