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#:
Operator | Description | Example |
---|---|---|
+ | Addition | int sum = 5 + 10; // sum is 15 |
– | Subtraction | int diff = 10 – 5; // diff is 5 |
* | Multiplication | int prod = 5 * 10; // prod is 50 |
/ | Division | int quot = 10 / 5; // quot is 2 |
% | Modulus | int 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#:
Operator | Description | Example |
---|---|---|
== | Equal to | int a = 5, b = 5; bool result = (a == b); |
!= | Not equal to | int a = 5, b = 10; bool result = (a != b); |
< | Less than | int a = 5, b = 10; bool result = (a < b); |
> | Greater than | int a = 10, b = 5; bool result = (a > b); |
<= | Less than or equal to | int a = 5, b = 5; bool result = (a <= b); |
>= | Greater than or equal to | int 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#
Operator | Description | Example |
---|---|---|
&& | 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
- C# Part 1: Setup and Introduction
- C# PART 2: UNDERSTANDING BASIC DATA TYPES AND VARIABLES IN C#
- C# PART 3: OPERATORS AND EXPRESSIONS IN C#
- C# PART 4: CONTROL FLOW STATEMENTS IN C#
- C# Part 5: Methods In C#
- C# Part 6: Arrays In C#
- C# Part 7: String Manipulation In C#
- C# Part 8: Object-Oriented Programming In C# (Classes and Objects)
- C# Part 9: Object-oriented Programming In C# (OOP Pillars)
- C# Part 10: Exception Handling in C#