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

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

If Else Statement

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

If Else-If Ladder Statement

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

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.