Saturday, August 17, 2013

C# - Operators review (Logical, Arithmetic and Comparing )

Arithmetic Operations

The ‘+’ Operator:
class Program
    {
        static void Main(string[] args)

        {
            // Example 1:

            int a = 5;
            int b = 5;
            int c = a + b;
            //c = 10

            // Example 2:

            int d = 5 + 3;
     //d = 8
        }
    }

The ‘-’ Operator:
static void Main(string[] args)
        {
            // Example 1:
            int a = 5;
            int b = 5;
            int c = a - b;
            //c = 0

            // Example 2:
            int d = 5 - 3;
            //d = 2
        }

The ‘/’ Operator:
static void Main(string[] args)
        {
            // Example 1:

            int a = 25;
            int b = 5;
            int c = a / b;
            //c =

            // Example 2:
            int d = 100 / 10;
            //d = 10
        }

The ‘%’ Operator:
static void Main(string[] args)
        {
            // Example 1:
             int a = 25;
            int b = 5;
            int c = a % b;
            //c = 0

            // Example 2:
            int d = 100 % 9;          
            //d = 1
        }

Logical Operations

F = False
T = True
Variable name
A
B
A AND B (&&)
A OR B (||)
Case 1
F
F
F
F
Case 2
T
T
T
T
Case 3  
F
T
F
T
Case 4  
T
F
F
T


The ‘OR’ Operator:

if ((a > b) || (a < c) )
            {
                //Use the 'OR' operation between two relational conditions
            }

The ‘AND’ Operator:

if ((a > b) && (a < c))

            {
                //Use the 'AND' operation between 2 relational conditions
            }

The ‘True’ Operator:

if ((a > b) == true)

            {
                //Perform operation if the condition return ‘false’      
            }

The ‘False’ Operator:

  if ((a > b) == false )

            {
                //Perform operation if the condition return ‘false’   
            }

The ‘!’ Operator:
if (!(a > b))

            {
                //Perform operation if 'a' is not bigger then 'b'          
            }

Comparing Operations

Operator No'1 – Equal (==):
static void Main(string[] args)
        {
            int a = 1;
            int b = 2;      

            if (a == b)

            {
                //Perform operation if 'a' is Equal to 'b'                  
            }        
        }

Operator No'2 – Smaller (<):
class Program
    {
        static void Main(string[] args)
        {
            int a = 1;
            int b = 2;    

            if (a < b)

            {
                //Perform operation if 'a' is smaller then 'b'                  
            } 
        }
    }

 Operator No'3 – Bigger (>):
class Program
    {
        static void Main(string[] args)
        {
            int a = 1;
            int b = 2;

            if (a > b)

            {
                //Perform operation if 'a' is bigger then 'b'  
            }   
        }
    }

Operator No'4 – Bigger or Equal (>=):
static void Main(string[] args)
        {
            int a = 1;
            int b = 2; 

            if (a >= b)

            {
               //Perform operation if 'a' is Bigger Or Equal to 'b'                  
            }        
        }

Operator No'5 – Smaller or Equal (<=):
 static void Main(string[] args)
        {
            int a = 1;
            int b = 2;

            if (a <= b)

            {
                //Perform operation if 'a' is Smaller Or Equal to 'b'                  
            }        
        }

Operator No' 6 – Not Equal (! =):
static void Main(string[] args)
        {
            int a = 1
            int b = 2;       

            if (a != b)

            {
                //Perform operation if 'a' is not Equal to 'b'                  
            }        
        }

Shortcuts operations 

This article will cover the assignments operators you can use to perform mathematical operation.

Operator No'1 (+=):
This operator can be used when you want to add number to a variable

Full:     a = a + 5;
Shortcut: a += 5;           

Operator No'2 (-=):
This operator can be used when you want to reduce a number from a variable
Full:     a = a - 5;
Shortcut: a -= 5;

Operator No'3 (*=):
This operator can be used when you want to multiply a variable with another number

Full:     a = a * 5;
Shortcut: a *= 5;

Operator No'4 (/=):
This operator can be used when you want to divide variable with a number  

Full:     a = a / 5;
Shortcut: a /= 5;

Increment and Decrement operators
Additional operators that you can use is decrement (--) and increment (++) that may be used on a single variable.

Increment Example:
Using this operator will cause an aggregation of +1 for the manipulated variable
Y = y + 1;
Y++;
Decrement Example:
Using this operator will cause a degradation of -1 for the manipulated variable
Y = y - 1;
Y--;

Note!
If you set the operator from the left of the variable (--X or ++Y) the operation will take place before committing the operation.

No comments:

Post a Comment

My Presentations