Welcome to Lesson 4! In this lesson, we’ll explore the various operators in C#, which are essential for performing operations on variables and values. Operators in C# can be categorized into several types, including arithmetic, relational, logical, and more. Understanding these operators will help you manipulate data and control the flow of your programs.
1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations:
1.1. Addition (+
)
- Adds two operands.
int a = 5;
int b = 3;
int sum = a + b; // sum is 8
1.2. Subtraction (-
)
- Subtracts the second operand from the first.
int difference = a - b; // difference is 2
1.3. Multiplication (*
)
- Multiplies two operands.
int product = a * b; // product is 15
1.4. Division (/
)
- Divides the numerator by the denominator.
int quotient = a / b; // quotient is 1 (integer division)
1.5. Modulus (%
)
- Returns the remainder of division.
int remainder = a % b; // remainder is 2
2. Relational Operators
Relational operators are used to compare two values and determine their relationship:
2.1. Equal to (==
)
- Checks if two operands are equal.
bool isEqual = (a == b); // isEqual is false
2.2. Not equal to (!=
)
- Checks if two operands are not equal.
bool isNotEqual = (a != b); // isNotEqual is true
2.3. Greater than (>
)
- Checks if the first operand is greater than the second.
bool isGreater = (a > b); // isGreater is true
2.4. Less than (<
)
- Checks if the first operand is less than the second.
bool isLess = (a < b); // isLess is false
2.5. Greater than or equal to (>=
)
- Checks if the first operand is greater than or equal to the second.
bool isGreaterOrEqual = (a >= b); // isGreaterOrEqual is true
2.6. Less than or equal to (<=
)
- Checks if the first operand is less than or equal to the second.
bool isLessOrEqual = (a <= b); // isLessOrEqual is false
3. Logical Operators
Logical operators are used to perform logical operations on Boolean values:
3.1. Logical AND (&&
)
- Returns true if both operands are true.
bool aTrue = true;
bool bTrue = false;
bool andResult = aTrue && bTrue; // andResult is false
3.2. Logical OR (||
)
- Returns true if at least one of the operands is true.
bool orResult = aTrue || bTrue; // orResult is true
3.3. Logical NOT (!
)
- Inverts the Boolean value of the operand.
bool notResult = !aTrue; // notResult is false
4. Assignment Operators
Assignment operators are used to assign values to variables, often with an operation:
4.1. Simple Assignment (=
)
- Assigns the right-hand operand to the left-hand variable.
int x = 10; // x is assigned 10
4.2. Add and Assign (+=
)
- Adds the right-hand operand to the left-hand variable and assigns the result to the left-hand variable.
x += 5; // Equivalent to x = x + 5; x is now 15
4.3. Subtract and Assign (-=
)
- Subtracts the right-hand operand from the left-hand variable and assigns the result to the left-hand variable.
x -= 3; // Equivalent to x = x - 3; x is now 12
4.4. Multiply and Assign (*=
)
- Multiplies the left-hand variable by the right-hand operand and assigns the result to the left-hand variable.
x *= 2; // Equivalent to x = x * 2; x is now 24
4.5. Divide and Assign (/=
)
- Divides the left-hand variable by the right-hand operand and assigns the result to the left-hand variable.
x /= 4; // Equivalent to x = x / 4; x is now 6
4.6. Modulus and Assign (%=
)
- Applies the modulus operator and assigns the result to the left-hand variable.
x %= 4; // Equivalent to x = x % 4; x is now 2
5. Increment and Decrement Operators
These operators are used to increase or decrease the value of a variable by 1:
5.1. Increment (++
)
- Increases the value of the operand by 1.
int y = 5;
y++; // y is now 6
5.2. Decrement (--
)
- Decreases the value of the operand by 1.
int z = 10;
z--; // z is now 9
5.3. Prefix and Postfix
- Prefix (
++x
,--x
): Increments or decrements before using the variable.
int a = 5;
int result = ++a; // a is 6, result is 6
- Postfix (
x++
,x--
): Increments or decrements after using the variable.
int b = 5;
int result = b++; // result is 5, b is 6
6. Example: Using Operators in a Program
Here’s a complete example demonstrating various operators in a C# program:
Code Example:
using System;
class Program
{
static void Main()
{
int a = 10;
int b = 5;
// Arithmetic Operators
Console.WriteLine("Addition: " + (a + b)); // 15
Console.WriteLine("Subtraction: " + (a - b)); // 5
Console.WriteLine("Multiplication: " + (a * b)); // 50
Console.WriteLine("Division: " + (a / b)); // 2
Console.WriteLine("Modulus: " + (a % b)); // 0
// Relational Operators
Console.WriteLine("Equal to: " + (a == b)); // False
Console.WriteLine("Not equal to: " + (a != b)); // True
Console.WriteLine("Greater than: " + (a > b)); // True
Console.WriteLine("Less than: " + (a < b)); // False
// Logical Operators
bool condition1 = true;
bool condition2 = false;
Console.WriteLine("Logical AND: " + (condition1 && condition2)); // False
Console.WriteLine("Logical OR: " + (condition1 || condition2)); // True
Console.WriteLine("Logical NOT: " + !condition1); // False
// Assignment Operators
int c = 15;
c += 5; // c is now 20
Console.WriteLine("Add and Assign: " + c);
c -= 5; // c is now 15
Console.WriteLine("Subtract and Assign: " + c);
// Increment and Decrement Operators
int d = 10;
Console.WriteLine("Prefix Increment: " + ++d); // 11
Console.WriteLine("Postfix Increment: " + d++); // 11
Console.WriteLine("After Postfix Increment: " + d); // 12
}
}
Explanation:
- The program demonstrates arithmetic, relational, logical, assignment, and increment/decrement operations. Each operation is executed and its result is printed to the console.
Summary
In this lesson, you learned about the various operators available in C#, including arithmetic, relational, logical, assignment, and increment/decrement operators. These operators are fundamental to manipulating data and controlling program flow.
Happy coding!
0 Comments