Operators in C#

Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 6

Opertaors

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:

 Addition operator (+): Adds two operands

 Subtraction operator (-): Subtracts one operand from another

 Multiplication operator (*): Multiplies two operands

 Division operator (/): Divides one operand by another

 Modulus operator (%): Returns the remainder of the division of two operands

Example:

int x = 10;

int y = 5;

int z = x + y; // z is 15 (can be used to calculate total price of an item in a shopping cart)

int w = x - y; // w is 5 (can be used to calculate discount on an item in a shopping cart)

int p = x * y; // p is 50 (can be used to calculate the total price of buying multiple items)

int q = x / y; // q is 2 (can be used to calculate the average of two numbers)

int r = x % y; // r is 0 (can be used to check if a number is even or odd)

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.

 AND operator (&&): Returns true if both operands are true

 OR operator (||): Returns true if either of the operands is true

 NOT operator (!): Returns the opposite of the operand

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:

These operators are used to assign a value to a variable.

 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:

int x = 10; //assigns right operand value(10) to left operand(x)

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:

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.

 Postfix increment/decrement (variable++, variable--): the variable is incremented/decremented


after it is used in an expression.

Example of prefix increment:

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;

// Another car passes by

++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.

Example of postfix 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[] arr = {1, 2, 3, 4, 5};

int index = 0;

while (index < arr.Length)

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:

int countdown = 10;

while (countdown > 0)

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.

Example of postfix decrement:

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;

while (num > 0)

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.

For example, in the following expression:

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.

You might also like