Decision Making In C Sharp


In C Sharp , using Conditional Statements we can check and evaluate expressions . Some times we need to check whether the expression turns out to be true or false. Depending on that , we can make decision what to do with that expression.

Syntax



          if(expression)        
         {
                      // statements to execute if expression is true.
          }
          else
         {
                        // statements to execute if expression is false.
         }


In above syntax , we pass the expression to the IF() statement. The IF() statement checks the condition/expression passed to it and determines if the expression is true or false. If the expression is true , the statements immediately following the IF() in the brackets are executed. And if the given expression turns out to be false, then the control flows to else and the statements in the else are executed.

Below program defines use of if...else structure



using System;
namespace conditional_program
{
    class Program
    {      
         static void Main(string[] args)
        {

            int a = 10;
            int b = 20;
            if (a < b)
            {
                Console.WriteLine("a is less than b");
            }
            else
            {
                Console.WriteLine("a is greater than b");
            }
         
        }
    }
}

Output
a is less than b

  • if..else..if
When there are multiple conditional statements that need to be checked simultaneously, then we can use if..else..if structure.This is called as else..if ladder.

using System;

namespace conditional_program
{
    class Program
    {
        static void Main(string[] args)
        {

            int a = 10;
            int b = 20;
            if (a > b)
            {
                Console.WriteLine("a is less than b");
            }
            else if (a<b)
            {
                Console.WriteLine("a is less than b");
                Console.ReadKey();
            }
            else
            {
                Console.WriteLine("both are same");
                Console.ReadKey();
            }
         
        }
    }
}

Output
"a is less than b
  • nested if..else
In some cases , you need to check more than one condition one after another then we can use nested if..else. Using nested if..else we can check two or more conditions simultaneously. The treatment given in nested if else is if one condition is true then go to next condition and check if it is also true or control transfers to the else condition of the first if statement.


using System;


namespace constant_var_program
{
    class Programv  
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter two values");
            int a = int.Parse(Console.ReadLine());
            int b = int.Parse(Console.ReadLine());

            if (a < b)
            {
                if (b > a)
                {
                    Console.WriteLine("a is less than b and b is greater than a ");
                    Console.ReadKey();
                }
                else
                {
                    Console.WriteLine("b is less then a ");
                    Console.ReadKey();
                }

            }
            else
            {
                Console.WriteLine("a is greater than b ");
                Console.ReadKey();
            }


        }
    }
}


Output
condition 1

Enter two values
10
20
a is less than b and b is greater than a

condition 2

Enter two values
20
10
a is greater than b


Decision Making In C Sharp Decision Making In C Sharp Reviewed by vishal on July 28, 2017 Rating: 5

No comments