C# Part 3: Operators and Expressions in C#

C# PART 3: OPERATORS AND EXPRESSIONS IN C#


Please Subscribe Youtube| Like Facebook | Follow Twitter

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#:

OperatorDescriptionExample
+Additionint sum = 5 + 10; // sum is 15
Subtractionint diff = 10 – 5; // diff is 5
*Multiplicationint prod = 5 * 10; // prod is 50
/Divisionint quot = 10 / 5; // quot is 2
%Modulusint remainder = 10 % 3; // remainder is 1

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#:

OperatorDescriptionExample
==Equal toint a = 5, b = 5; bool result = (a == b);
!=Not equal toint a = 5, b = 10; bool result = (a != b);
<Less thanint a = 5, b = 10; bool result = (a < b);
>Greater thanint a = 10, b = 5; bool result = (a > b);
<=Less than or equal toint a = 5, b = 5; bool result = (a <= b);
>=Greater than or equal toint a = 10, b = 5; bool result = (a >= b);

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#

OperatorDescriptionExample
&&Logical AND (true only if both are true)int a = 5, b = 10; bool result = (a < 10 && b > 5);
||Logical OR (true if at least one is true)int a = 5, b = 10; bool result = (a < 10 || b < 5);
!Logical NOT (reverse the condition)int a = 5; bool result = !(a < 10);

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

Please Subscribe Youtube| Like Facebook | Follow Twitter


Leave a Reply

Your email address will not be published. Required fields are marked *