In this Problem, you’ll learn about different types of operators in C#, their syntax and how to use them with the help of examples.
Operators are symbols that perform operations on variables and values. For example, + is an operator used for addition, while * is also an operator used for multiplication.
Operators in C# can be classified into 6 types:
- Arithmetic Operators that perform arithmetic operations with numeric operands
- Comparison operators that compare numeric operands
- Boolean logical operators that perform logical operations with bool operands
- Bitwise and shift operators that perform bitwise or shift operations with operands of the integral types
- Equality operators that check if their operands are equal or not
In C#, Operators can also categorized based upon Number of Operands :
- Unary Operator: Operator that takes one operand to perform the operation.
- Binary Operator: Operator that takes two operands to perform the operation.
- Ternary Operator: Operator that takes three operands to perform the operation.
We will discuss about arithmetic operators and comparision operators and rest we will study in next task.
Arithmetic Operators:
Arithmetic operators are used to perform arithmetic operations on variables and data. For example,
a + b;
Here, the + operator is used to add two variables a and b. Similarly, there are various other arithmetic operators in C#.
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo Operation (Remainder after division)
The ones falling into the category of Unary Operators are:
- Increment: The ‘++’ operator is used to increment the value of an integer. When placed before the variable name (also called pre-increment operator), its value is incremented instantly. For example, ++x.
And when it is placed after the variable name (also called post-increment operator), its value is preserved temporarily until the execution of this statement and it gets updated before the execution of the next statement. For example, x++.
- Decrement: The ‘--‘ operator is used to decrement the value of an integer. When placed before the variable name (also called pre-decrement operator), its value is decremented instantly. For example, --x.
And when it is placed after the variable name (also called post-decrement operator), its value is preserved temporarily until the execution of this statement and it gets updated before the execution of the next statement. For example, x--.
Comparison Operators
Comparison operators are used for comparison of two values. Let’s see them one by one:
- ‘==`(Equal To) operator checks whether the two given operands are equal or not. If so, it returns true. Otherwise it returns false. For example, 5==5 will return true.
- ‘!=`(Not Equal To) operator checks whether the two given operands are equal or not. If not, it returns true. Otherwise it returns false. It is the exact boolean complement of the ‘==’ operator. For example, 5!=5 will return false.
- ‘>`(Greater Than) operator checks whether the first operand is greater than the second operand. If so, it returns true. Otherwise it returns false. For example, 6>5 will return true.
- ‘<‘(Less Than) operator checks whether the first operand is lesser than the second operand. If so, it returns true. Otherwise it returns false. For example, 6<5 will return false.
- ‘>='(Greater Than Equal To) operator checks whether the first operand is greater than or equal to the second operand. If so, it returns true. Otherwise it returns false. For example, 5>=5 will return true.
- ‘<='(Less Than Equal To) operator checks whether the first operand is lesser than or equal to the second operand. If so, it returns true. Otherwise it returns false. For example, 5<=5 will also return true.
Example
// C# program to demonstrate the working
// of Arithmetical and Comparison Operators
using System;
class Interviewbit {
// Main Function
static void Main(string[] args)
{
int res;
int x = 10, y = 5;
// Addition
res = (x + y);
Console.WriteLine("Addition Operator: " + res);
// Subtraction
res = (x - y);
Console.WriteLine("Subtraction Operator: " + res);
// Multiplication
res = (x * y);
Console.WriteLine("Multiplication Operator: "+ re);
// Division
res = (x / y);
Console.WriteLine("Division Operator: " + res);
// Modulo
res = (x % y);
Console.WriteLine("Modulo Operator: " + res);
bool result;
x = 5;
y = 10;
// Equal to Operator
result = (x == y);
Console.WriteLine("Equal to Operator: " + result);
// Greater than Operator
result = (x > y);
Console.WriteLine("Greater than Operator: " + result);
// Less than Operator
result = (x < y);
Console.WriteLine("Less than Operator: " + result);
// Greater than Equal to Operator
result = (x >= y);
Console.WriteLine("Greater than or Equal to: "+ result);
// Less than Equal to Operator
result = (x <= y);
Console.WriteLine("Lesser than or Equal to: "+ result);
// Not Equal To Operator
result = (x != y);
Console.WriteLine("Not Equal to Operator: " + result);
}
}
Output
Addition Operator: 15
Subtraction Operator: 5
Multiplication Operator: 50
Division Operator: 2
Modulo Operator: 0
Equal to Operator: False
Greater than Operator: False
Less than Operator: True
Greater than or Equal to: False
Lesser than or Equal to: True
Not Equal to Operator: True
Boolean logical operators
- Logical AND: The ‘&&’ operator returns true when both the conditions in consideration are satisfied. Otherwise it returns false. For example, a && b returns true when both a and b are true (i.e. non-zero).
- Logical OR: The ‘||’ operator returns true when one (or both) of the conditions in consideration is satisfied. Otherwise it returns false. For example, a || b returns true if one of a or b is true (i.e. non-zero). Of course, it returns true when both a and b are true.
- Logical NOT: The ‘!’ operator returns true the condition in consideration is not satisfied. Otherwise it returns false. For example, !a returns true if a is false, i.e. when a=0.
- & (bitwise AND) Takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.
- | (bitwise OR) Takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 any of the two bits is 1.
- ^ (bitwise XOR) Takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.
- << (left shift) Takes two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift.
- >> (right shift) Takes two numbers, right shifts the bits of the first operand, the second operand decides the number of places to shift.
Example
// C# program to demonstrate the working
// of Logical and Boolean Operators
using System;
class Interviewbit {
// Main Function
static void Main(string[] args)
{
bool a = true,b = false, result;
// AND operator
result = a && b;
Console.WriteLine("AND Operator: " + result);
// OR operator
result = a || b;
Console.WriteLine("OR Operator: " + result);
// NOT operator
result = !a;
Console.WriteLine("NOT Operator: " + result);
int x = 5, y = 10;
// Bitwise AND Operator
result = x & y;
Console.WriteLine("Bitwise AND: " + result);
// Bitwise OR Operator
result = x | y;
Console.WriteLine("Bitwise OR: " + result);
// Bitwise XOR Operator
result = x ^ y;
Console.WriteLine("Bitwise XOR: " + result);
// Bitwise AND Operator
result = ~x;
Console.WriteLine("Bitwise Complement: " + result);
// Bitwise LEFT SHIFT Operator
result = x << 2;
Console.WriteLine("Bitwise Left Shift: " + result);
// Bitwise RIGHT SHIFT Operator
result = x >> 2;
Console.WriteLine("Bitwise Right Shift: " + result);
}
}
Output:
AND Operator: False
OR Operator: True
NOT Operator: False
Bitwise AND: 0
Bitwise OR: 15
Bitwise XOR: 15
Bitwise Complement: -6
Bitwise Left Shift: 20
Bitwise Right Shift: 1
Task:
You are given two integers as input a and b
You need to perform several task in the editor below:
- In the variable named "add" store the sum of a and b.
- In the variable named "sub" store the difference of a with b.
- In the variable named "multi" store the multiplication of a and b.
- In the variable named "div" store the division of a by b.
NOTE: You don’t need to print anything.