Decision Making

There are times when we would want some statements to be executed if and only if some conditions are met. C# includes 3 types of decision making structures. IF – IF ELSE – Nested IF

There are times when we would want some statements to be executed if and only if some conditions are met. C# includes 3 types of decision making structures.

IF – IF ELSE – Nested IF ELSE:

An IF condition consists of a boolean condition. IF boolean condition returns TRUE then it will execute the code in the IF block.
An IF ELSE consists of a boolean condition and if this condition is FALSE it will execute the else Block.

IF Syntax:

if(expression) //If expression returns true then below statements are executed
{
    //statements;
    //statements;
    //statements;
}

IF ELSE Syntax:

if(expression) //If expression returns flase then else block statements are executed
{
    //statements;
    //statements;
    //statements;
}
else
{
    //statements;
    //statements;
    //statements;
}

Example:

using System;

namespace DecisionMaking
{

    class Program
    {

        static void Main(string[] args)
        {
            int b;

            Console.WriteLine("please enter 2 numbers and i will return the greatest among them");
            Console.WriteLine("please enter first numbers");
            Int32.TryParse(Console.ReadLine(), out int a); //explicit conversion of string to int via tryparse

            Console.WriteLine("please enter second numbers");
            b = Convert.ToInt32(Console.ReadLine()); //explicit conversion of string to int via convert method

            if (a > b)
            {
                Console.WriteLine("First Number is the greatest {0}", a);
            }
            else
            {
                Console.WriteLine("Second Number is the greatest {0}", b);
            }

            Console.ReadKey();
        }
    }
}

Ternary Operator ?:

Similar to the IF, C# has a ternary operator which evaluates an expression for TRUE and if gets it then it returns expression 2 else it returns expression 3

(expression 1) ? (expression 2) : (expression 3) //Syntax

var result = x>y ? "x is greater" : "y is greater" //It is advised to store the output of the ternary operator in a var variable. As it could return values of any data type.

SWITCH:

A Switch statement allows an expression to be tested for equality again a list of values. Depending on the match that particular block will be executed.

Expressions used could be a value type, enum or object which returns a single value.

using System;

namespace DecisionMaking
{

    class Program
    {

        static void Main(string[] args)
        {
            Console.WriteLine("Please enter a value b/w 1 to 5 and we will give you a mystery meal");

            Int32.TryParse(Console.ReadLine(),out int value);

            switch (value)
            {
                case 1:
                    Console.WriteLine("Idly");
                    break;
                case 2:
                    Console.WriteLine("Dosa");
                    break;
                case 3:
                    Console.WriteLine("Poori");
                    break;
                case 4:
                    Console.WriteLine("Wada");
                    break;
                case 5:
                    Console.WriteLine("Upma");
                    break;
                default:
                    Console.WriteLine("you have selected a incorrect number.");
                    break;

            }
            
            Console.ReadKey();
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *