Operators in C sharp Arithmetic Operators, Assignment Operators, Unary Operators, Comparison Operator, Logical Operator
Operator helps to solve the mathematical or logical problem in programming.To make an expression we normally use opearator symbol. To calculate the value of variable or performs operation in variable we need to make proper expression.
In C sharp, we use arithmetic operators, assignment operators,
unary operators, comparison operator, logical operator etc
C# Arithmetic Operators
Arithmetic Operators are used for basic mathematical calculation
in C# programming.
The C# arithmetic operator performs basic calculation as add,
subtraction, multiplication, division, and modulus.
Arithmetic Operators:
Operator
|
Description
|
Examples
|
+
|
Add numbers
|
A+B
|
–
|
Subtract numbers
|
A-B
|
*
|
Multiply numbers
|
A*B
|
/
|
Divide numbers
|
A/B
|
%
|
Divide two numbers and returns reminder
|
A%B
|
using System;
using System.Collections.Generic;
using System.Text;
namespace Program
{
class Program
{
static void Main(string[]
args)
{
int
Num1, Num2, Addition, Subtraction, Multiplication, Division;
Console.Write("Enter the First Number : ");
Num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the Second Number : ");
Num2 = Convert.ToInt32(Console.ReadLine());
Addition = Num1 + Num2;
Subtraction = Num1 - Num2;
Multiplication = Num1 * Num2;
Division = Num1 / Num2;
Console.WriteLine("The Addition is {0}", Addition);
Console.WriteLine("The Subtraction is {0}", Subtraction);
Console.WriteLine("The Multiplication is {0}",
Multiplication);
Console.WriteLine("The Division is {0}", Division);
Console.ReadLine();
}
}
}
Output
Enter first
number 15
Enter second number 12
Enter second number 12
Addition 27
Subtraction 3
Multiplication 180
Division 1.25
Subtraction 3
Multiplication 180
Division 1.25
C# Assignment Operators
The most used C# assignment operator is equal “=” operator.The symbol of c sharp assignment
operator is “=” without quotes. The assignment operator widely used with
C# programming. Let’s understand with a
simple example:
result=num1+num2;
In this
example, the equal to (=) assignment operator assigns the value of num1
+ num2 into result variable.
Types of assignment Operators:
Operator
|
Description
|
Example
|
=
|
Simple assignment operator, Assigns values
from right side operands to left side operand
|
Result = num1 + num2 assigns value of num1
+ num2 into Result
|
+=
|
Add AND assignment operator, It adds right
operand to the left operand and assign the result to left operand
|
Result += num1 is equivalent to Result = Result
+ num1
|
-=
|
Subtract AND assignment operator, It
subtracts right operand from the left operand and assign the result to left
operand
|
Result -= num1 is equivalent to Result = Result
- num1
|
*=
|
Multiply AND assignment operator, It
multiplies right operand with the left operand and assign the result to left
operand
|
Result *= num1 is equivalent to Result = Result
* num1
|
/=
|
Divide AND assignment operator, It divides
left operand with the right operand and assign the result to left operand
|
Result /= num1 is equivalent to Result = Result
/ num1
|
%=
|
Modulus AND assignment operator, It takes
modulus using two operands and assign the result to left operand
|
Result %= num1 is equivalent to Result = Result
% num1
|
<<=
|
Left shift AND assignment operator
|
Result <<= 2 is same as Result = Result
<< 2
|
>>=
|
Right shift AND assignment operator
|
Result >>= 2 is same as Result = Result
>> 2
|
&=
|
Bitwise AND assignment operator
|
Result &= 2 is same as Result = Result
& 2
|
^=
|
bitwise exclusive OR and assignment
operator
|
Result ^= 2 is same as Result = Result ^ 2
|
|=
|
bitwise inclusive OR and assignment
operator
|
Result |= 2 is same as Result = Result | 2
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace assignment_Operator
{
class Program
{
static void Main(string[]
args)
{
int
x1, X2;
x1 = 20;
X2 = 10;
Console.WriteLine("The assignment value is = {0}", x1);
x1 += X2;
Console.WriteLine(" Addition is= {0}", x1);
x1 -= X2;
Console.WriteLine(" Subtraction is = {0}", x1);
x1 *= X2;
Console.WriteLine(" Multiplication is ={0}", x1);
x1 /= X2;
Console.WriteLine("Division is
= {0}", x1);
x1 %= X2;
Console.WriteLine("Modulo is = {0}", x1);
Console.ReadLine();
}
}
}
Output
The assignment value is 20
Addition is = 30
Substraction is =10
Multiplication
= 200
Modulus = 0
C#
unary operator:
The C# unary operator is nothing
but Increment, decrement
operators. These oprators mostly
used in looping to increament and decrement the value by 1.
++ Increment Operator (increment operator): It is used for incrementing value by 1. It is
used in C# programming by two types: Pre-increment (++i) and Post-increment
(i++). In pre-increment, first it increments by 1 then loop executes
whereas in Post-increment, the loop executes then it increments by 1.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace run_csharp_code
{
class Program
{
static void Main(string[]
args)
{
int
i = 6; // initialization
i++;
// i incremented by one. It is post increment
Console.WriteLine("The
value of i is {0}", i);
Console.ReadLine();
}
}
}
Output 7
— Decrement
Operator (Decrement Operator): It is used
for decrementing the value by one. It has also two types: Pre-Decrement (–i)
and Post Decrement (i–). In pre-decrement the value is decremented by
one then loop executes whereas in post-decrement the loop executed then the
value decrements by one.
Examples:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Decrement_Operator
{
class Program
{
static void Main(string[]
args)
{
int
i = 5; // Initialization
Console.WriteLine("The Value of i is {0}", i);
i--; // i
decremented by one. It is post-decrement
Console.WriteLine("\nNow the value of i is {0}", i);
Console.ReadLine();
}
}
}
Output
The Value of
i is 5
C# Comparison Operators or Relational operators:
Relational
operators are used to compare values. These operators always returns
true or false based on comparison.
Symbol
|
Meaning
|
<
|
less than
|
<=
|
less than or equal to
|
>
|
greater than
|
>=
|
greater than or equal to
|
==
|
equal to
|
!=
|
not equal to
|
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Comparison_Operator
{
class Program
{
static void Main(string[]
args)
{
int
x1, x2;
//Accepting
two inputs from the user
Console.Write("Enter first number\t");
x1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter second number\t");
x2 = Convert.ToInt32(Console.ReadLine());
//Processing
comparison
//Check
whether x1 is greater than or not
if
(x1 > x2)
{
Console.WriteLine("{0} is greater than {1}", x1, x2);
}
//Check
whether x2 is greater than or not
else
if (x2 > x1)
{
Console.WriteLine("{0} is greater than {1}", x2, x1);
}
else
{
Console.WriteLine("{0} and {1} are equal", x1, x2);
}
Console.ReadLine();
}
}
}
Output
Enter first
number 4
Enter second number 6
6 is greater than 4
Enter second number 6
6 is greater than 4
C# Logical Operator:
Logical
Operator returns true or false by evaluating the given at least two values.
If variable A holds Boolean value true and
variable B holds Boolean value false, then:
Operator
|
Description
|
Example
|
&&
|
Called Logical AND operator. If both the
operands are non zero then condition becomes true.
|
(A && B) is false.
|
||
|
Called Logical OR Operator. If any of the
two operands is non zero then condition becomes true.
|
(A || B) is true.
|
!
|
Called Logical NOT Operator. Use to
reverses the logical state of its operand. If a condition is true then
Logical NOT operator will make false.
|
!(A && B) is true.
|
Example
The
following example demonstrates all the logical operators available in C#:
using System;
namespace OperatorsAppl
{
class Program
{
static void Main(string[]
args)
{
bool
a = true;
bool
b = true;
if
(a && b)
{
Console.WriteLine("Line 1 - Condition is true");
}
if
(a || b)
{
Console.WriteLine("Line 2 - Condition is true");
}
/* lets
change the value of a and b */
a = false;
b = true;
if
(a && b)
{
Console.WriteLine("Line 3 - Condition is true");
}
else
{
Console.WriteLine("Line 3 - Condition is not true");
}
if
(!(a && b))
{
Console.WriteLine("Line 4 - Condition is true");
}
Console.ReadLine();
}
}
}
When the
above code is compiled and executed, it produces the following result:
Line 1 - Condition is true
Line 2 - Condition is true
Line 3 - Condition is not true
Line 4 - Condition is true
ghgg
ReplyDelete