By 22/07/2015 12 Comments

C# if-else Statement

  • It is a statement which uses a bool condition true or false, if that condition becomes true than anything (can be any expression, operation or a print statement etc) inside if condition’s body will be executed, if that condition becomes false than nothing will be executed inside if’s body and compiler will bypass that if condition.
  • An optional else statement can also be used with if statement, this can be used in a situation for example when if statement becomes false than else statement will be executed and when if statement becomes true else will not be executed and it will be bypassed by compiler.

OR

  • In this statement an else statement is optional, when if statement becomes true than this else statement will not be executed, if if statement becomes false than else statement will be executed.

Syntax:

if (bool_condition)
{
Statement 1;
}
else
{
Statement 2;
}

STATEMENT DESCRIPTION
if (bool_condition) if is a statement keyword, (bool_condition) can be any bool value such as (x == y) ,( x <= y||x != y) etc.
{ This indicates the starting scope or code block of if’s body.Anything can be written inside it.
Statement1; This can be anything (expression, print statement or something else etc) in if’s body or scope.
} This indicates the closing scope or code block of if’s body.
else else statement declaration, when if condition becomes false than this else statement will be executed.
{ This indicates the starting scope or code block of else body. Anything can be written inside it.
Statement 2; This can be anything (expression, print statement or something else etc) in else body or scope.
} This indicates the closing scope or code block of else body.


Example – 1, use of if statement:

using System;
namespace csharpBasic
{
    // Start class definition / declaration.
    class Program
    {
        // Static main method void type declaration.
        static void Main(string[] args)
        {
            // An int type variable is initialized.
            int number = 10;

            // Statement (if) that checks the condition true or false.
            if (number == 10)
            {
                // Print the output.
                Console.WriteLine("Number is: {0}", number);
            }
            
            Console.ReadKey();

        } // End of main method definition. 
    } // End of class. 
    
    /*
    The Output will be:
    Number is: 10
   */
}


Example – 2:

using System;
namespace csharpBasic
{
    // Start class definition / declaration.
    class Program
    {
        // Static main method void type declaration.
        static void Main(string[] args)
        {
            // An int type variable is initialized.
            int number = 10;

            // Statement (if) that checks the condition true or false.
            if (number > 10)
            {
                // Print the output.
                Console.WriteLine("Number is: {0}", number);
            }

            Console.WriteLine("Exit from if because it is false and has been bypassed.");
            
            Console.ReadKey();

        } // End of main method definition. 
    } // End of class. 

    /*
   The Output will be:
   Exit from if because it is false and has been bypassed.
  */
}


Example – 3, use of if…else:

using System;
namespace csharpBasic
{
    // Start class definition / declaration.
    class Program
    {
        // Static main method void type declaration.
        static void Main(string[] args)
        {
            // An int type variable is initialized.
            int number = 10;

            // Statement (if) that checks the condition true or false.
            if (number != 10)
            {
                // Print the output.
                Console.WriteLine("if condition is true.");
            }

            // The else statement is declared.
            else
            {
                Console.WriteLine("if condition is false, else condition is true.");
            }

            Console.ReadKey();
        } // End of main method definition. 
    } // End of class. 

    /*
   The Output will be:
   if condition is false, else condition is true.
  */
}


REMEMBER:

The if…else Statement can also be used as following ways:

  • if…else…if Ladder Statements
  • if then if…if Statements
  • Nested if Statements






← Previous Tutorial


Posted in: C# Basics, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .