C Sharp - Decision Making or Conditional Statements


Decision Making:Decision Making statements like if..else, if..else..if, switch etc. helps us to to execute specific part of program. Each statement is used to evaluate the specific test. If tests are determined to be true or false, specific statement will be execued .



 

if statement:If statement works only when the condition it true or false. Sometimes we reqiure to execute certain statement when specified condition true, there we use if statement. If the condition becomes true it enters (executes) into if block otherwise (if condition becomes fase) does not executed. The Single-line case statements do not require block braces  .



if statement Syntax:-

if (condition)  
{  
   //code that will run if the condition is true  
}  

Example: below example gives product only when the values are different.
using System;

class Program
{
    static void Main()
    {
        int a,b;
        Console.WriteLine("please enter the value of a");
        a = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("please enter the value of b");
      
        b = Convert.ToInt32(Console.ReadLine());
        if (a!=b)
        {
            Console.WriteLine("The product of a and b is = {0}",a*b);
            Console.ReadLine();
        }

        Console.WriteLine("both value are similar. please enter different value");
        Console.ReadLine();
      
    }
}

if..else Statement:
We need to use if..else statement when the possibility of  being true or false of any of the conditions. When specified condition true then if statement  gets executed. And when specified condition fail then else statement will get execute.

Syntax If..else Statement:
if (condition)  
{  
   //code that will run if the condition is true  
}  
else  
{  
  //code that will run if the condition is false  
}  

Example:
using System;

class Program
{
    static void Main()
    {
        int a, b;
        Console.WriteLine("please ener the value of a");
        a = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("please enter the value of b");

        b = Convert.ToInt32(Console.ReadLine());

        if (a > b)
        {
            Console.WriteLine("a is greater than b");
            Console.ReadLine();
        }
        else
        {
            Console.WriteLine("a is less than b");
            Console.ReadLine();
        }
    }
}
      
     
      
   
Nested if-statements:
We can put if-statements  inside of other if-statements to check more than one conditions.

Syntax Nested if-statements:
if (condition)  
{  
if (condition)  
{  
   //code that will run if the condition is true  
}  

   //code that will run if the condition is true  
}  



Example:

using System;

class Program
{
    static void Main()
    {
        int a, b;
        Console.WriteLine("please ener the value of a");
        a = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("please enter the value of b");

        b = Convert.ToInt32(Console.ReadLine());
        if (a != 0 && b!=0)
        {
            if (a > b)
            {
                Console.WriteLine("a is greater than b");
                Console.ReadLine();
            }
            Console.WriteLine("a is less than b");
            Console.ReadLine();
        }

        Console.WriteLine("Please eneter both the values greater than zero");
        Console.ReadLine();
    }
}
      
  

Nested if-else statement:-

An if statement or a  if-else statement only can allow for one or two possible actions. But Nested if-else statement make it possible to evaluate or handle  more conditions.    
      
Nested if-else statement Syntax:-

if(condition 1)
{
if(condition 2)
{
.....statement;
.........................
}
else
{
statement;
.................
}
}
else
{
statement;
...............
}
   

Example

using System;

namespace Example
{
    class ProgrammingExample
    {
        static void Main()
        {
            int x = 10;
            int y = 25;
            int z = 5;

            if (x >= 7)
            {
                if (y < 99)
                {
                    Console.WriteLine("x ({0}) is greater than or equal to 7 and y ({1}) is less than 99.",
                        x, y);
                }
                else if (y % z == 0)
                {
                    Console.WriteLine("x ({0}) is greater than or equal to 7 and " +
                            " y ({1}) divides evenly into z ({2}).",
                        x, y, z);
                }
                else
                {
                    Console.WriteLine("x ({0}) is greater than or equal to 7.", x);
                }
            }
        }
    }
}






else if statements:
If you want to check more than one conditions at the same time , you can use else if statement .
Syntax of else if statement:-
if(condition expression 1)
{
statement;
................
}
else if(condition 2)
{
statement;
..............
}
else if(condition 3)
{
statement;
.....................
}
else
{
statement;
.................
}


Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication20
{
    class Program
    {
        static void Main(string[] args)
        {
            string username = null;
            string password = null;

            Console.WriteLine("Enter username: ");
            username = Console.ReadLine();

            Console.WriteLine("Enter pasword: ");
            password=Console.ReadLine();

            string message = "Welcome " + username;
            if (username == "Suhail" && password == "12345")
            {
                Console.WriteLine(message);
            }
            else if (username == "Ayon" && password == "123")
            {
                Console.WriteLine(message);
            }
            else if (username == "admin" && password == "admin")
            {
                Console.WriteLine(message);
            }
            else
            {
                Console.WriteLine("Sorry Password and username incorrect!");
            }
            Console.ReadLine();
        }
    }
}



Switch Case:
It is used when there is multiple if condition in a program. In CSharp switch case acts like a multiple if / else if / else chain. Checks a value against a list of cases, and executes the first case that is true. It also includes a default value in Default statements. If no matching case found, it executes the default case. The break(optional) statements with case indicate to the interpreter to end the particular case.
Syntax Switch Case:
switch(expression)
{
case 1:
statement;
case 2:
statement;
case 3:
statement;
.
.
default:
statement;
}

There are some order of execution of switch statement.
  • The expression is evaluated first.
  • The expression value is compare with case value,if it become true then that case statement will be executed,if it become false then default value will be executed.
  • The break statement at the end of each block statement,cause an exit from switch statement.
  • The default is a optional case,when the value of expression does not match any of the case value then it will be executed.


Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace switch_case
{
    class Program
    {
        static void Main(string[] args)
        {
            int option, a, b;
            float result;

        label:

            Console.WriteLine("\n\tMenu");
            Console.WriteLine("\nPress 1 for add");
            Console.WriteLine("Press 2 for subtraction");
            Console.WriteLine("Press 3 for multiplication");
            Console.WriteLine("Press 4 for Division");

            Console.Write("\n\nEnter first number:\t");
            a = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter second number:\t");
            b = Convert.ToInt32(Console.ReadLine());

            Console.Write("\nEnter your option:\t");
            option = Convert.ToInt32(Console.ReadLine());

            switch (option)
            {
                case 1:
                    result = a + b;
                    Console.WriteLine("\n{0} + {1} = {2}", a, b, result);
                    break;

                case 2:
                    result = a - b;
                    Console.WriteLine("\n{0} - {1} = {2}", a, b, result);
                    break;
                case 3:
                    result = a * b;
                    Console.WriteLine("\n{0} * {1} = {2}", a, b, result);
                    break;
                case 4:
                    result = (float)a / b;
                    Console.WriteLine("\n{0} / {1} = {2}", a, b, result);
                    break;
                default:
                    Console.WriteLine("\nInvalid option. Please try again.");
                    goto label;
            }
            Console.ReadLine();
        }
    }
}



Comments

Post a Comment