Topic : Decision Making in C#: If Statement | If & Else statement | Nested If & Else statement

Decision Making:

In every day life, we are often making decision. We perform different tasks while taking decisions. For example: we say: "if You are interested in Programming, then Join infobrother", in this statement we make an decision.

on the other side our parents says: "if the milk shop is open, bring one liter of milk" then we will bring one liter of milk if the shop is open. and if the shop is closed, we will come back to home without milk. thus we making a decision on the condition that the shop is open. Computer Programs often seem smart because of their ability to use selections or make decision. in this lecture we will use if and else statement to make decision:



If statement:

If statements consists of a Boolean expression followed by one or more statements. If statement allows us to control whether a program enters a section of code or not based on whether a given condition is true or false.
by using if statement we can check whether a user entered a correct password, our program can decide whether a user is allowed access to the computer or not.



Basic Syntax for if Statement:


/*The operator like logical, Relational or any other operator
can be used in the brackets:*/
if (condition) { // body begin: statement... statement... statement... } // body close:


we can use several statement to executed if the condition is true.



if the statement is single then there is no need of curly braces. but if there are more statement then single, blocks is important. (the statement between two curly braces { } is know has block).



if statement

Let's have an example to understand the concept of IF statement: let's suppose we Need to create an Program which can tell us whether the student is pass or fail on the condition that if student percentage is greater than or equal to 60 then He/she will pass otherwise fail.

this Program will ask user to enter the percentage. user will enter their percentage. after press enter this Program will Make decision whether the user is pass or fail. if pass then code inside the block and outside the block will execute. otherwise only the code outside of block will be execute:



/*Example: If statement: InfoBrother.*/

using System;

namespace DecisionMaking
{
    class Program
    {
        static void Main(string[] args)
        {
            int percent;

            Console.WriteLine("Enter the Percentage of Student: ");
            percent = Convert.ToInt32(Console.ReadLine());

            if(percent >= 60) //if percentage is greater than or equal to 60.
            {                 //execute this statement.
                Console.WriteLine("Congratulation !");
            }

            Console.ReadKey();
        }
    }
}


If and else statement:

We have seen that the if structure executes its block of statement only when the condition is true, otherwise the statement are skipped. The if and else structure allows the Programmer to specify that a different block of statements is to be executed when the condition is false.



Basic Syntax for if and Else:


if (condition ) // any operator we can use to check condition:
{ 
  //if the condition is true then this block will be executed:
} 

else 
{ 
  //if the condition is false then this block will be executed.
}


in If and Else, only one block of statements will execute. if condition is true the if-block will be execute, if condition is false then else-block will be execute:



if statement

let's have an example of same Program that we discuss in if statement: here we will add "else" too in if statement: in this cause if percentage is greater then or equal to 60 then only the statement within the if block will execute. if percentage is less then 60. then only the statement within the else block will execute.



/*Example: If and else statement: InfoBrother.*/

using System;

namespace DecisionMaking
{
    class Program
    {
        static void Main(string[] args)
        {
            int percent;

            Console.WriteLine("Enter the Percentage of Student: ");
            percent = Convert.ToInt32(Console.ReadLine());

            if(percent >= 60) //if percentage is greater than or equal to 60.
            {                 //execute this statement.
                Console.WriteLine("Congratulation !");
            }

            else        //if percentage is less than 60.
            {           //Execute this Statement.
                Console.WriteLine("You are Fail: ");
            }

            Console.ReadKey();
        }
    }
}


Nested If-else statement:

in if-else statement, we can check whether the condition is true or false. if the condition is true, we can display a message, if the condition is false, then we can display another message. but what's about, if we Need to test Multiple condition.
well let's have an example to clear this Nested if-else statement:

Let's suppose we have an Program, which will allow User to enter their Gander. If user enter "M" then it will display an Message, if User Enter "F" then will display another Message. But if user enter another Character then M and F, then how our Program can Handle this. Here is the Uses of Nested if-else statement. C# allow us to check the Multiple condition at the same times using Nested if-else Statement:



Basic Syntax for Nested if-else.



 if(1st Condition)
  {
      /*If 1st Condition is true, Execute this block*/
  }
            
 else if(2nd Condition)
  {
      /*If 2nd condition is true, execute this block.*/
  }

 else
  {
    //If No one condition is true, execute this block.
  }



In Nested if-else Statement, only one statement can be executed. the Program will test, if the first condition is true, then it will show the statement of first block. if the first is not true, then the program will try next condition and so on.



Nested if-else statement

Let's have an Example here. in this example. Program will ask the user to enter their Gender. if user enter "F" then it will display an statement. if user enter "M" another statement will be executed. if user enter another character then it will display an error message.



/*Example: Nested If and else statement: InfoBrother.*/

using System;

namespace DecisionMaking
{
    class Program
    {
        static void Main(string[] args)
        {
            char gender;
            Console.WriteLine("Enter Your Gender: (M or F): ");
            gender = (char)Console.Read();
            
            if(gender == 'm' || gender == 'M')  //1st condition
            {
                Console.WriteLine("You are Male: ");
            }

            else if (gender == 'f' || gender == 'F')  //second condition.
            {
                Console.WriteLine("You are Female: ");
            }

            else        //if no condition is true.
            {
                Console.WriteLine(" Error: Try again: ");
            }
            
            Console.ReadKey();
        }
    }
}














I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement