Tuesday 29 December 2020

If-Else statements

                                                                                                                               #15 minutes of reading


In this article, I show you decision making in c#. C# program making decision based on logical condition provided.

Before going into code lets understand this with an example. Suppose you want to go from location1 to location 2. To find the route you search the location in google map and Google map shows you shortest route to reach destination. How google map found shorted route from 3 or 4 routes? It means in background Google map checks distance in kilometer and shows you shortest route. Here distance in kilometer is condition (logical condition) to find shortest route.

We will do same example in C# for finding shortest route.

Source location: Pune

Destination Location: Delhi

Two routes found

1.       Route 1: 1441km (shorted route)

2.       Route 2: 1470km

Let’s understand this with example. Create console application using visual studio 2019 community version.


C# support operator in logical condition like above > (greater than operator) used. Below are few examples of operator C# support in logical condition.

Operator

Description

Greater than

Less than

==

Equality operator

!

Not

!=

Not equal

  

If else condition:

if (condition)

{

    execute code inside green color braces if condition becomes true

}

else

{

    execute code inside red color braces if condition becomes false

}

 

Nested if else:  

if (condition)

{

    //execute code inside green braces if condition becomes true

        if (condition)

    {

  //execute code inside orange color braces if condition becomes true

    if (condition)

  {

     //execute code inside pink color braces if condition becomes true

  }

    }

}

else

{

    execute code inside red color braces if all condition fails

}

 

If else ladder:

if (condition1)

{

    execute code inside green color braces if condition1 becomes true

}

Else if(condition2)

{

    execute code inside orange color braces if condition2 becomes true

}

Else if(condition3)

{

    execute code inside red color braces if condition3 becomes true

}

 


Below program implemented all if-else scenarios discussed above.

static void Main(string[] args)

        {

            Console.WriteLine("Please select Below country to check list of state in country by writing Below                                name");

            Console.WriteLine("1. India");

            Console.WriteLine("2. USA");

            string counrty = Console.ReadLine();

 

            if(counrty.ToUpper() == "INDIA")

            {

                Console.WriteLine("Please select Below State to check list of city in country by writing Below                                 name");

                Console.WriteLine("1. Maharashtra");

                Console.WriteLine("2. Telangana");

                string state = Console.ReadLine();

                Console.WriteLine("List of cities in " + state);

                if (state.ToUpper() == "MAHARASHTRA")

                {                   

                    Console.WriteLine("1. Mumbai");

                    Console.WriteLine("2. Pune");

                    Console.WriteLine("3. Nagpur");

                }

                if (state.ToUpper() == "TELANGANA")

                {

                    Console.WriteLine("1. Hyderabad");

                    Console.WriteLine("2. Nalgonda");

                    Console.WriteLine("3. Nizamabad");

                }

            }

            else if(counrty.ToUpper() == "USA")

            {

                Console.WriteLine("Please select Below State to check list of city in country by writing Below                                 name");

                Console.WriteLine("1. California");

                Console.WriteLine("2. Texas");

                string state = Console.ReadLine();

                Console.WriteLine("List of cities in " + state);

                if (state.ToUpper() == "CALIFORNIA")

                {

                    Console.WriteLine("1. Los Angeles");

                    Console.WriteLine("2. San Jose");

                    Console.WriteLine("3. Oakland");

                }

                if (state.ToUpper() == "TEXAS")

                {

                    Console.WriteLine("1. Austin");

                    Console.WriteLine("2. Houston");

                    Console.WriteLine("3. Dallas");

                }

            }

            Console.ReadLine();

        }

 

 

 

Executing above program will show you below output in console.



Type India/USA and press enter to see list of states.

Now type Maharashtra/Telangana and press enter to see list of cities.


 

Same program shows different list when you select country as USA. Program decides output based on input condition and show results to end user.

 

 

 

 

 

 

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home