C# if-else

HTML
CSS
C#
SQL

If Else Statement in C#

In C# programming, the if statement is used to test the condition. There are various types of if statements in C#.

For Example:

  • if statement
  • if-else statement
  • if-else-if ladder statement

If Statement

Scenario 1: Write a C# program that checks if a person is eligible to create a bank account based on their age. If the age is greater than or equal to 18, it should print “You can create your BankAccount” on the console.

using System;

public class IfExample

{

    public static void Main(string[] args)

    {     

        int age = 20;




        if (age >= 18)

        {

        Console.WriteLine("You can create your BankAccount");

        }

    }

}

//Output: You can create your BankAccount

Code Explanation

using System;
public class IfExample
It defines a new class with named - IfExample.
{
     public static void Main(string[] args)
This is the entry point of the program
{   
        int age = 20;
It declares a variable named age of type integer and assigns it the value 20.
if (age >= 18)
If statement, checks if the value of the age variable is greater than or equal to 18. If the condition is true, the code inside if block will be executed; otherwise, it will not print anything in the console.
{
       Console.WriteLine("You can create your BankAccount");
The code is inside the if block. If the condition (age >= 18) is true, this line will be executed. It prints the message "You can create your BankAccount" to the console..
        }
   }
}

Output: You can create your BankAccount
You will see the above output in your console.

If Else Statement

Scenario 2: Write a C# program that checks if a person is eligible to create a bank account based on their age. The program should print a different message if the person is under 18.

If the condition is true, print “You can create your BankAccount” to the console.

If the condition is false, print “You can’t create your BankAccount, your age is less than 18” to the console.

using System;

public class IfElseExample
{

public static void Main(string[] args)
   {
       int age = 11;
       if (age >= 18)
       {
           Console.WriteLine("You can create your BankAccount");
       }
       else 
       {
           Console.WriteLine("You can't create your BankAccount, your age is less then 18");
        }

    }

}

//Output: You can't create your BankAccount, your age is less than 18

Code Explanation

using System;
public class IfElseExample
This line defines a new class with named - IfElseExample
{

    public static void Main(string[] args)
This line is the entry point of the program
{

        int age = 11;
This line declares a variable named age of type integer and initializes it with the value 11.
if (age >= 18)
If statement. It checks if the value of the age variable is greater than or equal to 18. If the condition is true, the code inside the if block will be executed; otherwise, the code inside the else block will be executed.
{
            Console.WriteLine("You can create your BankAccount");
If the condition (age >= 18) evaluates to true, this line will be executed. It prints the message "You can create your BankAccount" to the console otherwise it will execute else block because the age of 11 is less than 18.
}

        else  //
This line introduces the default block of code to be executed if the previous conditions are false.
{

            Console.WriteLine("You can't create your BankAccount, your age is less then 18");
If the condition (age >= 18) evaluates to false, this line will be executed. It prints the message "You can't create your BankAccount, your age is less than 18" to the console.

            }

      }

}

Output: You can’t create your BankAccount, your age is less than 18

You will see the above output in your console.

If Else-If Ladder Statement

Scenario 3: Write a C# program that checks a person’s age and categorizes them as a Senior Citizen, eligible to create a bank account, or too young to create a bank account by using if else ladder

If the age is 60 or older, print “You are a Senior Citizen.”

If the age is 18 or older but less than 60, print “You can create your BankAccount.”

If the age is less than 18, print “You can’t create your BankAccount. Your age is less than 18.”

using System;
public class IfElseifLadderExample

{
    public static void Main(string[] args)

    {
        int age = 30;
       if (age >= 60)

        {
            Console.WriteLine("You are a Senior Citizen.");

        }

        else if (age >= 18)

        {

            Console.WriteLine("You can create your BankAccount.");

        }

        else

        {             Console.WriteLine("You can't create your BankAccount. Your age is less than 18.");

        }

    }

}

//Output: You can create your BankAccount

Code Explanation

using System;
public class IfElseifLadderExample
This line defines a new class named - IfElseExample
{

    public static void Main(string[] args)
This line is the entry point of the program
{
       int age = 17;
This line declares a variable named age of type integer and initializes it with the value 30.
if (age >= 60)
if statement. It checks if the value of the age variable is greater than or equal to 60. If the condition is true, the code inside the if block will be executed
{

    Console.WriteLine("You are a Senior Citizen.");
If the condition (age >= 60) evaluates to true, this line will be executed. If not, the next condition will be checked. Because age 17 is less than 60 the line “You are a Senior Citizen will not be printed.
        }

        else if (age >= 18)
else-if statement, which allows for the evaluation of multiple conditions if the previous conditions are not met. It checks if the value of age is greater than or equal to 18 if yes then it will execute code under the else if block.
{

   Console.WriteLine("You can create your BankAccount.");
If the condition (age >= 18) evaluates to true, then this line will be executed. If not, the next condition will be checked. Because age 17 is less than 18 the line “You can create your BankAccount” will not be printed.
}
        else
This line introduces the default block of code to be executed if any previous values are not met.
{

            Console.WriteLine("You can't create your BankAccount. Your age is less than 18.");
else block. If previous conditions (age >= 60) and (age >= 18) are equal to false, then this line will be executed because the age is less than 18.
        }

    }

}

Output: You can't create your BankAccount. Your age is less than 18.
You will see the above output in your console.

Course Video

Practices Tasks

1. Do you want Health Insurance ?

a.If no then print Thank you

b.if yes then continue below 2nd question

c.if input is not equals to yes or no then show error.

2. a.If age is less then 21 then insurance cost is 1500

b.If age is between 21 and 50 then insurance cost is 2000

c. If age is more then 50 then insurance cost is 3000

3. In which Standard you are ?

a. if you are from 5th standard then  you have to go at 1st floor for exam.

b. if you are from 6th standard then you have to go at 2nd floor 

c. if you are from 7th standard then you have to go at 3rd floor.

d. other wise goto library for exam.

4.What’s your budget for car purchasing ?

a.if less then 50 lakhs you can take fortuner.

b.if 50 lakhs you can take Mercedes

c.if greater then 50 lakhs you can take BMW M4.

d.if 1Crore or greater then 1 Crore you can take ferrari.