 C# Part 3: Operators and Expressions in C#

# C# PART 3: OPERATORS AND EXPRESSIONS IN C#

## Operators and Expressions in C#

In this article, we’ll cover the most commonly used operators and expressions in C#, along with their syntax, examples, and outputs.

To perform various tasks in C#, you need to use different operators and expressions. In this article, we will discuss the different types of operators and expressions available in C#, and how to use them in your programs.

## Arithmetic Operators

Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, and division. The following table shows the arithmetic operators available in C#:

Here is an example code that demonstrates the use of arithmetic operators in C#:

``````using System;

class Program
{
static void Main(string[] args)
{
int num1 = 10;
int num2 = 3;

int sum = num1 + num2;
Console.WriteLine(\$"Sum: {sum}");

int difference = num1 - num2;
Console.WriteLine(\$"Difference: {difference}");

int product = num1 * num2;
Console.WriteLine(\$"Product: {product}");

int quotient = num1 / num2;
Console.WriteLine(\$"Quotient: {quotient}");

int remainder = num1 % num2;
Console.WriteLine(\$"Remainder: {remainder}");
}
}
``````

### Output

```Sum: 13
Difference: 7
Product: 30
Quotient: 3
Remainder: 1
```

## Comparison Operators

Comparison operators are used to compare two values and return a Boolean value (true or false). The following table shows the comparison operators available in C#:

Here is an example code that demonstrates the use of comparison operators in C#:

``````using System;

class Program
{
static void Main(string[] args)
{
int num1 = 5;
int num2 = 10;

bool result = num1 == num2;
Console.WriteLine(\$"{num1} == {num2}: {result}"); // Output: 5 == 10: False

result = num1 != num2;
Console.WriteLine(\$"{num1} != {num2}: {result}"); // Output: 5 != 10: True

result = num1 < num2;
Console.WriteLine(\$"{num1} < {num2}: {result}"); // Output: 5 < 10: True

result = num1 > num2;
Console.WriteLine(\$"{num1} > {num2}: {result}"); // Output: 5 > 10: False

result = num1 <= num2;
Console.WriteLine(\$"{num1} <= {num2}: {result}"); // Output: 5 <= 10: True

result = num1 >= num2;
Console.WriteLine(\$"{num1} >= {num2}: {result}"); // Output: 5 >= 10: False
}
}
``````

### Output

```5 == 10: False
5 != 10: True
5 < 10: True
5 > 10: False
5 <= 10: True
5 >= 10: False
```

## Logical Operators

Logical operators are used to combine two or more conditions and return a Boolean value (true or false). The following table shows the logical operators available in C#

Here is an example code that demonstrates the use of logical operators in C#:

``````using System;

class Program
{
static void Main(string[] args)
{
int a = 5;
int b = 10;

bool result = (a < 10 && b > 5);
Console.WriteLine(\$"{a} < 10 && {b} > 5: {result}"); // Output: 5 < 10 && 10 > 5: True

result = (a < 10 || b < 5);
Console.WriteLine(\$"{a} < 10 || {b} < 5: {result}"); // Output: 5 < 10 || 10 < 5: False

result = !(a < 10);
Console.WriteLine(\$"!({a} < 10): {result}"); // Output: !(5 < 10): False
}
}
``````

### Output

```5 < 10 && 10 > 5: True
5 < 10 || 10 < 5: False
!(5 < 10): False
```

## Conclusion

In conclusion, operators and expressions are fundamental concepts in C# programming language. They allow us to perform various operations on data and manipulate it according to our needs. Arithmetic Operators allow us to perform arithmetic calculations on numerical data types. Comparison Operators help us in comparing data and making decisions based on the result. Logical Operators are used to combine and manipulate boolean values.

C# Beginner Tutorial Series