By 22/07/2015 12 Comments

C# if-else-if Ladder Statements

  • By using this form of if statement we can declare if…else…if…else… and so on like a chain.
  • In this form if first if statement becomes false than an else…if will be executed, if that if…else statement also becomes false than the next if…else will be checked by compiler, that process continue until any if statement becomes true.
  •  If any if statement inside if…else chain becomes true than that statement’s body will be executed and next if…else will not be checked by compiler, and execution exists from if…else chain immediately.
  • This form of (if…else) only executes single condition at a time and returns one result or one part of a program at a time. Mobile phone menu is a real example of it, because in mobile menu user can select only one option at a time.

Example:

using System;
namespace csharpBasic
{
    // Start class definition / declaration.
    class Program
    {
        // Static main method void type declaration.
        static void Main(string[] args)
        {
            // Initialize an int value
            int value = 10;

            if (value == 1)
            {
                Console.WriteLine("It is value 1.");
            }
            else
                if (value == 10 && value != 2)
                {
                    Console.WriteLine("It is value 10.");
                }
                else
                    if (value != 2 || value == 10)
                    {
                        Console.WriteLine("It is also value 10.");
                    }

                    else
                    {
                        Console.WriteLine("all are false.");
                    }

            Console.ReadKey();

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

    /*
   The Output will be:
   It is value 10.
  */
}





Next Tutorial →


← 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 .