Operators are a fundamental aspect of C# programming. They provide the means to manipulate data, make decisions, and control the flow of your programs. Operators are symbols or keywords that represent specific operations to be performed on operands. These operators are crucial for performing tasks like mathematical calculations, logical evaluations, and type conversions.
C# provides a rich set of operators that can be categorized into various groups, each serving a specific purpose.
- Arithmetic Operators
- Comparison Operators
- Logical Operators
- Assignment Operators
- Bitwise Operators
- Conditional Operator
- Unary Operator: The operator needs one operand to perform the operation.
- Binary Operator: The operator needs two operands to perform the operation.
- Ternary Operator: The operator needs three operands to perform the operation.
Arithmetic Operators.
- Addition (+): The addition operator is used to add two numeric values.
- Subtraction (-): The subtraction operator is used to subtract the right operand from the left operand.
- Multiplication (*): The multiplication operator is used to multiply two numeric values.
- Division (/): The division operator is used to divide the left operand by the right operand.
- Modulus (%): The modulus operator calculates the remainder when the left operand is divided by the right operand.
- Increment (++) and Decrement (--): Increment and decrement operators are used to increase or decrease the value of a variable by 1. These are also known as urinary operators as they require only one operand to perform the operation.
// C# Code to show the working of Arithmetic Operators using System; public class AlgoLesson { public static void Main(string[] args) { // Addition int sum = 5 + 3; Console.WriteLine ("Addition Operator: " + sum); // Subtraction int difference = 10 - 3; Console.WriteLine ("Subtraction Operator: " + difference); // Multiplication int product = 4 * 6; Console.WriteLine ("Production Operator: " + product); // Division int quotient = 15 / 3; Console.WriteLine ("Division Operator: " + quotient); int x = 5; // Increment Operator x++; // x becomes 6 Console.WriteLine ("Increment Operator: " + x); int y = 10; // Decrement Operator y--; // y becomes 9 Console.WriteLine ("Decrement Operator: " + y); } }
Addition Operator: 8
Subtraction Operator: 7
Production Operator: 24
Division Operator: 5
Increment Operator: 6
Decrement Operator: 9
Comparison Operators.
- Equal (==): The equal operator checks if two values are equal.
- Not Equal (!=): The not equal operator checks if two values are not equal.
- Greater Than (>): The greater than operator checks if the left operand is greater than the right operand.
- Less Than (<): The less than operator checks if the left operand is less than the right operand.
- Greater Than or Equal To (>=): The greater than or equal to operator checks if the left operand is greater than or equal to the right operand.
- Less Than or Equal To (<=): The less than or equal to operator checks if the left operand is less than or equal to the right operand.
// C# Code to show the working of Comparison Operators using System; public class AlgoLesson { public static void Main(string[] args) { int a = 5; int b = 7; bool result; // Equal Operator result = (a == b); Console.WriteLine("Equal to Operator: " + result); // Not Equal Operator result = (a != b); Console.WriteLine("Not Equal to Operator: " + result); // Greater Than Operator result = (a > b); Console.WriteLine("Greater Than Operator: " + result); // Less Than Operator result = (a < b); Console.WriteLine("Less Than Operator: " + result); // Greater Than or Equal To Operator result = (a >= b); Console.WriteLine("Greater Than or Equal To Operator: " + result); // Less Than or Equal To result = (a <= b); Console.WriteLine("Less Than or Equal To: " + result); } }
Equal to Operator: False Not Equal to Operator: True Greater Than Operator: False Less Than Operator: True Greater Than or Equal To Operator: False Less Than or Equal To: True
Logic Operators.
- Logical AND (&&): The logical AND operator returns true if both operands are true, otherwise, it returns false.
- Logical OR (||): The logical OR operator returns true if at least one of the operands is true, otherwise, it returns false.
- Logical NOT (!): The logical NOT operator returns the opposite of the operand. If the operand is true, it returns false, and vice versa.
// C# code to show to working of Logical Operators using System; public class AlgoLesson { public 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); } }
AND Operator: False OR Operator: True NOT Operator: False
// C# code to show to working of Logical Operators with Conditional Operators using System; public class AlgoLesson { public static void Main(string[] args) { int age = 25; bool isStudent = false; if (age >= 18 && !isStudent) { Console.WriteLine("You are eligible for a driver's license."); } } }
You are eligible for a driver's license.
Assignment Operators.
- Assignment (=): The assignment operator (=) is used to assign the value on the right-hand side to the variable on the left-hand side.
- Addition Assignment (+=): The addition assignment operator (+=) is used to add the value on the right-hand side to the variable on the left-hand side and update the variable with the result.
- Subtraction Assignment (-=): The subtraction assignment operator (-=) is used to subtract the value on the right-hand side from the variable on the left-hand side and update the variable with the result.
- Multiplication Assignment (*=): The multiplication assignment operator (*=) is used to multiply the variable on the left-hand side by the value on the right-hand side and update the variable with the result.
- Division Assignment (/=): The division assignment operator (/=) is used to divide the variable on the left-hand side by the value on the right-hand side and update the variable with the result.
- Modulus Assignment (%=): The modulus assignment operator (%=) is used to find the remainder when the variable on the left-hand side is divided by the value on the right-hand side, and then update the variable with the result.
// C# code to show the working of Assignment Operators using System; public class AlgoLesson { public static void Main(string[] args) { // Assigns the value 10 to the variable x int x = 10; Console.WriteLine("Assignment Operator: " + x); // Equivalent to y = y + 3, updates y to 8 int y = 5; y += 3; Console.WriteLine("Add Assignment Operator: " + y); // Equivalent to z = z - 4, updates z to 6 int z = 10; z -= 4; Console.WriteLine("Subtraction Assignment Operator: " + z); // Equivalent to a = a * 2, updates a to 12 int a = 6; a *= 2; Console.WriteLine("Multiplication Assignment Operator: " + a); // Equivalent to b = b / 4, updates b to 5 int b = 20; b /= 4; Console.WriteLine("Division Assignment Operator: " + b); // Equivalent to c = c % 7, updates c to 1 int c = 15; c %= 7; Console.WriteLine("Modulus Assignment Operator: " + c); } }
Assignment Operator: 10
Add Assignment Operator: 8
Subtraction Assignment Operator: 6
Multiplication Assignment Operator: 12
Division Assignment Operator: 5
Modulus Assignment Operator: 1
Bitwise Operators.
- Bitwise AND (&): The bitwise AND operator (&) compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the result is 1; otherwise, it's 0.
- Bitwise OR (|): The bitwise OR operator (|) compares each bit of the first operand to the corresponding bit of the second operand. If either bit is 1, the result is 1; otherwise, it's 0.
- Bitwise XOR (^): The bitwise XOR operator (^) compares each bit of the first operand to the corresponding bit of the second operand. If the bits are different (one is 0 and the other is 1), the result is 1; otherwise, it's 0.
- Bitwise NOT (~): The bitwise NOT operator (~) inverts each bit of the operand. It changes 0s to 1s and 1s to 0s.
- Left Shift (<<): The left shift operator (<<) shifts the bits of the first operand to the left by the number of positions specified by the second operand.
- Right Shift (>>): The right shift operator (>>) shifts the bits of the first operand to the right by the number of positions specified by the second operand. If the leftmost bit is 0, it fills with 0; if it's 1, it fills with 1.
// C# Code to explain working of Bitwise Operators using System; public class AlgoLesson { public static void Main(string[] args) { int a = 5; // Binary: 0101 int b = 3; // Binary: 0011 int result; // Bitwise AND Operator result = a & b; Console.WriteLine ("Bitwise AND Operator: " + result); // Bitwise OR Operator result = a | b; Console.WriteLine ("Bitwise OR Operator: " + result); // Bitwise XOR Operator result = a ^ b; Console.WriteLine ("Bitwise XOR Operator: " + result); // Bitwise NOT Operator result = ~a; Console.WriteLine ("Bitwise NOT Operator: " + result); // Left Shift Operator int leftShifted = a << 2; Console.WriteLine ("Bitwise Left Shift Operator: " + leftShifted); // Right Shift Operator int rightShifted = a >> 2; Console.WriteLine ("Bitwise Right Shift Operator: " + rightShifted); } }
Bitwise AND Operator: 1
Bitwise OR Operator: 7
Bitwise XOR Operator: 6
Bitwise NOT Operator: -6
Bitwise Left Shift Operator: 20
Bitwise Right Shift Operator: 1
Conditional Operators.
condition ? expression1 : expression2
// C# Code to explain working of Ternary Operators using System; public class AlgoLesson { public static void Main(string[] args) { int num = 10; // Conditional Operator string result = (num % 2 == 0) ? "Even" : "Odd"; Console.WriteLine(result); } }
Even
Null Coalescing Operator (??)
// C# Code to explain working of Null Coalescing Operator using System; public class AlgoLesson { public static void Main(string[] args) { int? nullableValue = null; int result = nullableValue ?? 10; // result will be 10 Console.WriteLine(result); } }
10
No comments:
Post a Comment