05 Feb C# Operators
Operators perform operations by taking one or more values, to give another value. These operations are performed on variables and values. Operators in C# are discussed here. For example:
1 2 3 |
a + b |
The following are the operators in C#:
- Arithmetic Operators
- Assignment Operators
- Relational Operators
- Logical Operators
Let us understand the operators one by one:
C# Arithmetic Operators
Arithmetic operators in C# perform arithmetical operations, such as addition, subtraction, division, multiplication, etc.
Let us see an example of Arithmetic Operators in C#:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
using System; namespace Demo { class Studyopedia { static void Main(string[] args) { int a, b, c; a = 50; b = 100; c = 150; Console.WriteLine("Arithmetic Operators in C#:\n"); Console.WriteLine("Addition Arithmetic Operator"); Console.WriteLine("Value of (a + b) is "+(a+b)); Console.WriteLine("\nSubtract Arithmetic Operator"); Console.WriteLine("Value of (b - a) is "+(b-a)); Console.WriteLine("\nDivision Arithmetic operator"); Console.WriteLine("Value of (c / a) is "+(c/a)); Console.WriteLine("\nMultiplication Arithmetic operator"); Console.WriteLine("Value of (a * b) is "+(a*b)); Console.WriteLine("\nModulus Arithmetic operator"); Console.WriteLine("Value of (c % b) is "+(c%b)); Console.WriteLine("\nIncrement operator"); Console.WriteLine("Value of (a++) is "+(a++)); Console.WriteLine("\nDecrement operator"); Console.WriteLine("Value of (a--) is "+(a--)); } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
Arithmetic Operators in C#: Addition Arithmetic Operator Value of (a + b) is 150 Subtract Arithmetic Operator Value of (b - a) is 50 Division Arithmetic operator Value of (c / a) is 3 Multiplication Arithmetic operator Value of (a * b) is 5000 Modulus Arithmetic operator Value of (c % b) is 50 Increment operator Value of (a++) is 50 Decrement operator Value of (a--) is 51 |
C# Assignment Operators
Use the Assignment Operator when you need to assign values to a variable. It also includes the Arithmetic Assignment Operators:
Let us see an example of Assignment Operators in C#:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
using System; namespace Demo { class Studyopedia { static void Main(string[] args) { int a, b, c, d, e; a = 5; b = 10; c = 15; d = 20; e = 30; Console.WriteLine("Assignment Operators in C#:\n"); c = a +b; Console.WriteLine("Assignment operator"); Console.WriteLine("Value of c: = "+c); a += 5; Console.WriteLine("\nAdd AND assignment operator"); Console.WriteLine("Value of a: = "+a); b -= 5; Console.WriteLine("\nSubtract AND assignment operator"); Console.WriteLine("Value of b: = "+b); c *= 5; Console.WriteLine("\nMultiple AND assignment operator"); Console.WriteLine("Value of c: = "+c); d /= 10; Console.WriteLine("\nDivide AND assignment operator"); Console.WriteLine("Value of d: = "+d); e %= 9; Console.WriteLine("\nModulus AND assignment operator"); Console.WriteLine("Value of e: = "+e); } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
Assignment Operators in C#: Assignment operator Value of c: = 15 Add AND assignment operator Value of a: = 10 Subtract AND assignment operator Value of b: = 5 Multiple AND assignment operator Value of c: = 75 Divide AND assignment operator Value of d: = 2 Modulus AND assignment operator Value of e: = 3 |
C# Relational Operators
Compare two values with C# relational operators, which are also known as Comparison Operators. Let us say a = 3, b =5;
Let us see an example of Relational Operators in C#:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
using System; namespace Demo { class Studyopedia { static void Main(string[] args) { int a, b, c, d; a = 5; b = 10; c = 15; Console.WriteLine("Relational Operators in C#:\n"); Console.WriteLine("Value of (a == b) is " + (a == b) ); Console.WriteLine("Value of (a != b) is " + (a != b) ); Console.WriteLine("\nValue of (b > c) is " + (b > c) ); Console.WriteLine("Value of (c < a) is " + (c < a) ); Console.WriteLine("\nValue of (c >= b) is " + (c >= b) ); Console.WriteLine("Value of (b <= c) is " + (b <= c) ); } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 |
Relational Operators in C#: Value of (a == b) is False Value of (a != b) is True Value of (b > c) is False Value of (c < a) is False Value of (c >= b) is True Value of (b <= c) is True |
C# Logical Operators
Logical operators combine conditional statements. Considering Boolean variables, a and b,
Let us see an example of Logical Operators in C#:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
using System; namespace Demo { class Studyopedia { static void Main(string[] args) { bool a, b, c, d; a = true; b = false; c = true; d = false; Console.WriteLine("\nRelational Operators in C#:"); Console.WriteLine("Value of (a && b) = " + (a&&b)); Console.WriteLine("Value of (b || c) = " + (b||c) ); Console.WriteLine("Value of !(a && d) = " + !(a && d)); } } } |
Output
1 2 3 4 5 6 |
Relational Operators in C#: Value of (a && b) = False Value of (b || c) = True Value of !(a && d) = True |
No Comments