Skip to main content

C# - Decision Making

 <<PreviousPage    NextPage>>

Decision Making


If Else Statement

If Else statement is used for decision making in c# . In the below example we will see how we can use if else statement in C# to great effect.  





Nested If Else Statement

We can improve upon the above example by adding nest if else statement. Nested if else statement helps to add extra if else block with in an if or else block.







Switch Statement


In place where we need multiple if else statements to satisfy a criteria we can add switch statement instead of if else statement. 

We can create an application which would answer our question and let us know about the Date and Time. This will be a small AI sort of idea which would recognize your command and perform accordingly. 

Code - 


  Console.WriteLine("................Welcome to Mini AI................");

           string input = "a";
           while(input!=string.Empty)
           {
               Console.WriteLine("Enter Input : ");
               input = Console.ReadLine();

               switch (input)
               {           
                   case "Hi":
                       Console.WriteLine("how can I help you?");
                       continue;
                   case "hi":
                       Console.WriteLine("how can I help you?");
                       continue;
                   case "Hello":
                       Console.WriteLine("how can I help you?");
                       break;
                   case "hello":
                       Console.WriteLine("how can I help you?");
                       break;
                   case "What time is it":
                       Console.WriteLine("Time is "+ DateTime.Now.TimeOfDay);
                       break;
                   case "what time is it":
                       Console.WriteLine("Time is " + DateTime.Now.TimeOfDay);
                       break;
                   case "What is the time":
                       Console.WriteLine("Time is " + DateTime.Now.TimeOfDay);
                       break;
                   case "what is the time":
                       Console.WriteLine("Time is " + DateTime.Now.TimeOfDay);
                       break;
                   case "Tell me the time":
                       Console.WriteLine("Time is " + DateTime.Now.TimeOfDay);
                       break;
                   case "tell me the time":
                       Console.WriteLine("Time is " + DateTime.Now.TimeOfDay);
                       break;
                   case "Time":
                       Console.WriteLine("Time is " + DateTime.Now.TimeOfDay);
                       break;
                   case "time":
                       Console.WriteLine("Time is " + DateTime.Now.TimeOfDay);
                       break;
                   case "Time please":
                       Console.WriteLine("Time is " + DateTime.Now.TimeOfDay);
                       break;
                   case "time please":
                       Console.WriteLine("Time is " + DateTime.Now.TimeOfDay);
                       break;
                   case "Good bye":
                       Console.WriteLine("Thanks for your time, have a nice day!!");
                       break;
                   case "Bye":
                       Console.WriteLine("Thanks for your time, have a nice day!!");
                       break;
                   case "bye":
                       Console.WriteLine("Thanks for your time, have a nice day!!");
                       break;
                   case "Tchau":
                       Console.WriteLine("Thanks for your time, have a nice day!!");
                       break;
                   case "bye bye":
                       Console.WriteLine("Thanks for your time, have a nice day!!");
                       break;           
               }
           }

Out Put







Nested Switch Statement

We can improve upon the code by adding nested switch statement where we can multiple switch statements with in a switch statement

we need to change the code 


case "Good bye":
                       Console.WriteLine("Thanks for your time, have a nice day!!");

                       break;

to

case "Good bye":
                        {
                            Console.WriteLine("Are you sure, you wanna leave?");
                           
                            string sure = Console.ReadLine();
                            switch (sure)
                            {
                                case "Yes":
                                    Console.WriteLine("Thanks for your time, have a nice day!!");
                                    break;
                                case "No":
                                    Console.WriteLine("Please continue");
                                    continue;
                            }
                            break;
                        }

                       
and add the same to change other switch statements like below

 Console.WriteLine("................Welcome to Mini AI................");


           string input = "a";
           while(input!=string.Empty)
           {
               Console.WriteLine("Enter Input : ");
               input = Console.ReadLine();

               switch (input)
               {           
                   case "Hi":
                       Console.WriteLine("how can I help you?");
                       continue;
                   case "hi":
                       Console.WriteLine("how can I help you?");
                       continue;
                   case "Hello":
                       Console.WriteLine("how can I help you?");
                       break;
                   case "hello":
                       Console.WriteLine("how can I help you?");
                       break;
                   case "What time is it":
                       Console.WriteLine("Time is "+ DateTime.Now.TimeOfDay);
                       break;
                   case "what time is it":
                       Console.WriteLine("Time is " + DateTime.Now.TimeOfDay);
                       break;
                   case "What is the time":
                       Console.WriteLine("Time is " + DateTime.Now.TimeOfDay);
                       break;
                   case "what is the time":
                       Console.WriteLine("Time is " + DateTime.Now.TimeOfDay);
                       break;
                   case "Tell me the time":
                       Console.WriteLine("Time is " + DateTime.Now.TimeOfDay);
                       break;
                   case "tell me the time":
                       Console.WriteLine("Time is " + DateTime.Now.TimeOfDay);
                       break;
                   case "Time":
                       Console.WriteLine("Time is " + DateTime.Now.TimeOfDay);
                       break;
                   case "time":
                       Console.WriteLine("Time is " + DateTime.Now.TimeOfDay);
                       break;
                   case "Time please":
                       Console.WriteLine("Time is " + DateTime.Now.TimeOfDay);
                       break;
                   case "time please":
                       Console.WriteLine("Time is " + DateTime.Now.TimeOfDay);
                       break;
                   case "Good bye":
                        {
                            Console.WriteLine("Are you sure, you wanna leave?");
                           
                            string sure = Console.ReadLine();
                            switch (sure)
                            {
                                case "Yes":
                                    Console.WriteLine("Thanks for your time, have a nice day!!");
                                    break;
                                case "No":
                                    Console.WriteLine("Please continue");
                                    continue;
                            }
                            break;
                        }
                       
                   case "Bye":
                        {
                            Console.WriteLine("Are you sure, you wanna leave?");

                            string sure = Console.ReadLine();
                            switch (sure)
                            {
                                case "Yes":
                                    Console.WriteLine("Thanks for your time, have a nice day!!");
                                    break;
                                case "No":
                                    Console.WriteLine("Please continue");
                                    continue;
                            }
                            break;
                        }
                    case "bye":
                        {
                            Console.WriteLine("Are you sure, you wanna leave?");

                            string sure = Console.ReadLine();
                            switch (sure)
                            {
                                case "Yes":
                                    Console.WriteLine("Thanks for your time, have a nice day!!");
                                    break;
                                case "No":
                                    Console.WriteLine("Please continue");
                                    continue;
                            }
                            break;
                        }
                    case "Tchau":
                        {
                            Console.WriteLine("Are you sure, you wanna leave?");

                            string sure = Console.ReadLine();
                            switch (sure)
                            {
                                case "Yes":
                                    Console.WriteLine("Thanks for your time, have a nice day!!");
                                    break;
                                case "No":
                                    Console.WriteLine("Please continue");
                                    continue;
                            }
                            break;
                        }
                    case "bye bye":
                        {
                            Console.WriteLine("Are you sure, you wanna leave?");

                            string sure = Console.ReadLine();
                            switch (sure)
                            {
                                case "Yes":
                                    Console.WriteLine("Thanks for your time, have a nice day!!");
                                    break;
                                case "No":
                                    Console.WriteLine("Please continue");
                                    continue;
                            }
                            break;
                        }

                }

output 






Ternary Operator

We have already see the example of a ternary operator under condition operator section. 

Please refer to condition operator here 



 <<PreviousPage   NextPage>>

Comments