Operators in C#
Operators in C#
Operators in C#
Operators in C#
Operators are symbols that perform specific operations on one or more operands.
An operand is a value or a variable on which an operation is performed by an operator. Here are some
types of operators in C#:
Arithmetic Operators:
Arithmetic operators are used to perform mathematical operations such as addition, subtraction,
multiplication, division, and modulus on numerical data types. In C#, there are five arithmetic operators:
Modulus operator (%): Returns the remainder of the division of two operands
Example:
int x = 10;
int y = 5;
int p = x * y; // p is 50 (can be used to calculate the total price of buying multiple items)
Comparison Operators:
These operators are used to compare two values or variables and return a Boolean value (true or false)
based on the comparison.
Equals to operator (==): Checks whether two operands are equal
Not equals to operator (!=): Checks whether two operands are not equal
Greater than operator (>): Checks whether one operand is greater than another
Greater than or equal to operator (>=): Checks whether one operand is greater than or equal to
another
Less than operator (<): Checks whether one operand is less than another
Less than or equal to operator (<=): Checks whether one operand is less than or equal to another
exapmles:
int x = 10;
int y = 5;
bool a = (x == y); // a is false (can be used to check if two values are equal)
bool b = (x != y); // b is true (can be used to check if two values are not equal)
bool c = (x > y); // c is true (can be used to check if one value is greater than another)
bool d = (x >= y); // d is true (can be used to check if one value is greater than or equal to
another)
bool e = (x < y); // e is false (can be used to check if one value is less than another)
bool f = (x <= y); // f is false (can be used to check if one value is less than or equal to
another)
Logical Operators:
These operators are used to combine Boolean expressions and return a Boolean value based on the
result of the logical operation.
examples:
bool a = true;
bool b = false;
bool c = (a && b); // c is false (can be used to check if both conditions are true)
bool d = (a || b); // d is true (can be used to check if at least one condition is true)
bool e = !(a && b); // e is true (can be used to check the opposite of a condition)
Assignment Operators:
Assignmet operator (=): Assigns the value of the right operand to the left operand
Addition and assignment operator (+=): Adds the right operand to the left operand and assigns
the result to the left operand
Subtraction and assignment operator (-=): Subtracts the right operand from the left operand and
assigns the result to the left operand
Multiplication and assignment operator (*=): Multiplies the left operand by the right operand
and assigns the result to the left operand
Division and assignment operator (/=): Divides the left operand by the right operand and assigns
the result to the left operand
Modulus and assignment operator (%=): Calculates the remainder of the division of the left
operand by the right operand and assigns the result to the left operand
examples:
x += 5; // x is 15
x -= 5; // x is 10
x *= 2; // x is 20
x /= 2; // x is 10
x %= 3;
Increment and decrement operators are used to increase or decrease the value of a variable by one.
These operators are commonly used in loops. In C#, there are two types of increment and decrement
operators:
Prefix increment/decrement (++variable, --variable): the variable is incremented/decremented
before it is used in an expression.
Suppose you're counting the number of cars passing by on a highway. You start with a counter variable
called "count" initialized to 0. Every time a car passes by, you increment the count variable by one using
the prefix increment operator:
int count = 0;
// Car passes by
++count;
++count;
In this example, the count variable is incremented before it is used in the expression, so the value of
count becomes 1 after the first increment and 2 after the second increment.
Suppose you're printing the contents of an array to the console. You start with a variable called "index"
initialized to 0, and you use it to access each element of the array. After printing each element, you want
to increment the index variable using the postfix increment operator:
int index = 0;
Console.WriteLine(arr[index++]);
In this example, the index variable is used to access each element of the array, and then it is incremented
using the postfix increment operator. The value of index is incremented after it is used in the expression,
so the first element of the array is printed when index is 0, the second element is printed when index is
1, and so on.
Example of prefix decrement:
Suppose you're printing a countdown to the console. You start with a variable called "countdown"
initialized to 10, and you use it to print the numbers from 10 to 1. After printing each number, you want
to decrement the countdown variable using the prefix decrement operator:
Console.WriteLine(countdown);
--countdown;
In this example, the countdown variable is used to print the current number, and then it is decremented
using the prefix decrement operator. The value of countdown is decremented before it is used in the
expression, so the number 10 is printed when countdown is 10, the number 9 is printed when
countdown is 9, and so on.
Suppose you're calculating the factorial of a number. You start with a variable called "num" initialized to
the input value, and you use it to calculate the factorial by multiplying it with each decreasing integer
until 1. After each multiplication, you want to decrement the num variable using the postfix decrement
operator:
int num = 5;
int factorial = 1;
factorial *= num--;
Console.WriteLine(factorial);
In this example, the num variable is used to calculate the factorial by multiplying it with each decreasing
integer, and then it is decremented using the postfix decrement operator. The value of num is
decremented after it is used in the expression, so the factorial is calculated as 5 * 4 * 3 * 2 * 1 = 120.
Explanation about Operand
An operand is a term used in programming to refer to any object that can be manipulated by an
operator. In simpler terms, an operand is a value or a variable on which an operation is performed by an
operator.
int a = 5 + 3;
The operands are 5 and 3, and the operator is +. The operator adds the two operands together to
produce the result 8.
So, in simple terms, an operand is any value or variable that is being used with an operator to perform an
operation.