Saturday, August 24, 2013

C# - Conditions(IF and Else)

Conditions help you to control the code flow of your software based on the condition result (True OR False).

When the compiler runs your code he need to decide the “Code Directions” , means that in some cases the compiler should start in point A and move to point B  and in another cases he need to start in point A and move to point C .

Conditions can be configured with ‘IF’ and ‘Else’ statements, Example:
Example 1:

Class Program
    {
        static void Main(string[] args)
        {
            int a = 1 , b =2 ;
            if (a > b )
            {
                //Execute This code                
            }
            else
            {
                //Execute This code               
            }
        }
    }

As you can see it’s very simple, the compiler start processing your code and when he gets to the ‘IF’ statement he checks the result (True / False) and then decide to execute the relevant code.
Example 2:

In some cases we want to execute specific code part only  if the ‘IF’ statement return ‘true’ but if the statement return false we doesn’t want to execute any  another code  .
class Program
    {
        static void Main(string[] args)
        {
            int a = 1 , b =2 ;
            if (a > b ) //If True
            {
                //Execute This code               
            }
            //If 'False'
     
            //Additional Code that doesn’t relevant for the 'IF' statement
        }
    }

Example 3:
In some cases we want to use ‘Nested’ conditions logic to resolve a complicated issues.

class Program
    {
        static void Main(string[] args)
        {
            int a = 2, b = 1;
            if (a > b) //If True
            {
                if (a < 3)
                {
                    //This code executed
                }
                if (a > 3)
                {
                    //This code executed
                }
                if (a == 3)
                {
                    //This code executed
                }
            }
            else
            {
                //Executed this code
            }
        }
    }

In this code you can see that if a>b we executing the code in this block based on 3 different conditions, if a <= b the compiler will executed the code in the ‘Else’ bulk.

Example 4:
In some cases we can use “Else If” statement when we know a specific range of the variable initialization

In this example a can be set with the range of 1, 2 and 3, therefore we can use “Else if” OR ‘switch’ condition

class Program
    {
        static void Main(string[] args)
        {
            Random a = new Random();
            int number = a.Next(1,3);
            if (number == 1)
            {
             //Ececute this code
            }
            else if (number == 2)
            {
            //Ececute this code  
            }
            else if (number == 3)
            {
             //Execute this code
            }
        }
    }

Complex conditions  
In some cases we want to execute our code after more the one verification, therefore we can use 2 different operators (And / OR) that could be used alone OR with combination.

Example 1 (AND):
As you can see, the code will be executed only when 2 different condition return ‘True’, if one of the conditions return ‘false’ the code will not be executed.

 Class Program
   {
        static void Main(string[] args)
        {
            Random a = new Random();
            int number = a.Next(0,2);
            if ((number == 0) && (number < 2))
            {
             //Ececute this code
            }                       
        }
    }

Example 2 (OR):
In this case the code will be executed when only one condition return ‘True’.
Class Program
    {
        static void Main(string[] args)
        {
            Random a = new Random();
            int number = a.Next(0,2);

            if ((number == 0) || (number < 2))
            {
             //Ececute this code
            }                       
        }
    }

Example 3 (Or / And):
In this case I created a combination of the two operators to give an example for complex statement.

class Program
    {
        static void Main(string[] args)
        {
            Random a = new Random();

            int number = a.Next(0,100);
            int number2 = a.Next(0,100);

            if (((number < 10) || (number2 < 10)) && ((number < 5 || (number2 == 10))))
            {
             //Ececute this code
            }
            else if (((number < 10) && (number2 < 10)) || ((number < 5 && (number2 == 10))))
            {
               //Ececute this code
            }
            else if (((number < 10) && (number2 < 10)) && ((number < 5 && (number2 == 10))))
            {
                //Ececute this code
            }
            else if (((number < 10) && (number2 < 10)) && ((number < 5 || (number2 == 10))))
            {
                //Ececute this code
            }
        }
    }


Using short conditions
After reviewing the conditions statement,
I now can explain how to create the same statement with less syntax.
To write conditions with less syntax, you can use the following format:

Variable = (Boolean Condition)? “Positive”: “Negative”
Example:

class Program
    {
        static void Main(string[] args)
        {
            Random a = new Random();
            int number =  5;
            int number2 = 4;

            //Short Condition :
            string ConditionResult = (number > number2) ? "Bigger" : "Lower";
            Console.WriteLine(ConditionResult);
                     //Result = "Bigger"
           
            //Regular Condition:
            string B = "Bigger";
            string L = "Lower";
            if (number > number2)
            {
                Console.WriteLine(B);
            }
            else
            {
                Console.WriteLine(L);
            }
            //Result = "Bigger"
        }
    }

No comments:

Post a Comment

My Presentations