Operators
In C#, operators are symbols or keywords that perform operations on one or more operands to produce a result. They can be used to perform mathematical, logical, and relational operations.
Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations such as addition, subtraction, multiplication, division, and modulus.
Here are some examples:
1int a = 5;
2int b = 3;
3
4int sum = a + b;
5int difference = a - b;
6int product = a * b;
7int quotient = a / b;
8int remainder = a % b;
9
10Console.WriteLine("Sum: " + sum);
11Console.WriteLine("Difference: " + difference);
12Console.WriteLine("Product: " + product);
13Console.WriteLine("Quotient: " + quotient);
14Console.WriteLine("Remainder: " + remainder);
The output of the above code will be:
1Sum: 8
2Difference: 2
3Product: 15
4Quotient: 1
5Remainder: 2
Logical Operators
Logical operators are used to perform logical operations such as AND, OR, and NOT. They are often used in conditional statements to control the flow of execution.
Here are the three logical operators in C#:
- && (AND): Returns true if both operands are true, otherwise returns false.
- || (OR): Returns true if at least one of the operands is true, otherwise returns false.
- ! (NOT): Reverses the logical state of the operand.
Relational Operators
Relational operators are used to compare two values and determine the relationship between them. They return a boolean value (true or false) based on the comparison result.
Here are some relational operators in C#:
- == (Equality): Returns true if the two operands are equal, otherwise returns false.
- != (Inequality): Returns true if the two operands are not equal, otherwise returns false.
- > (Greater than): Returns true if the left operand is greater than the right operand, otherwise returns false.
- < (Less than): Returns true if the left operand is less than the right operand, otherwise returns false.
- >= (Greater than or equal to): Returns true if the left operand is greater than or equal to the right operand, otherwise returns false.
- <= (Less than or equal to): Returns true if the left operand is less than or equal to the right operand, otherwise returns false.
Assignment Operators
Assignment operators are used to assign values to variables. They combine the assignment operator (=) with other arithmetic or logical operators to perform the assignment with an operation.
Here are some examples:
1int x = 10;
2
3x += 5; // equivalent to x = x + 5
4x -= 3; // equivalent to x = x - 3
5x *= 2; // equivalent to x = x * 2
6x /= 4; // equivalent to x = x / 4
7x %= 3; // equivalent to x = x % 3
8
9Console.WriteLine("x: " + x); // Output: 0
xxxxxxxxxx
int a = 5;
int b = 3;
int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;
Console.WriteLine("Sum: " + sum);
Console.WriteLine("Difference: " + difference);
Console.WriteLine("Product: " + product);
Console.WriteLine("Quotient: " + quotient);
Console.WriteLine("Remainder: " + remainder);