C# if-else

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

Basic Structure

if (condition)
{
    //code for execution
}

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

Basic structure

if (condition)
{
       //code for execution
}
  else //previous if condition not met then we will execute else block
{
      //code execution
}

Scenario 1: 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

Basic Structure

if (condition)
{
      //code for execution
}
else if (condition) // previous if condition not met then we check else if conditon
{
      //code for executioin
}
else // if previous condition not met then we will execute else block
{
     //code execution
}

Scenario 1: 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

YouTube Reference

Task:

1. Do you want Health Insurance ?

a.If no then print Thank you

Output

b.if yes then continue below 2nd question

Output

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

Output

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

Output

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

Output

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

Output

3. In which Standard you are ?

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

Output

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

Output

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

Output

d. other wise goto library for exam.

Output

4.What’s your budget for car purchasing ?

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

Output

b.if 50 lakhs you can take Mercedes

Output

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

Output

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

Output

5. Class and If-Else:
Create a class named Person with properties Name, Age, and Gender.
In the main method, create an object of Person and set its properties.
Use an if-else statement to print “Adult” if the Age is 18 or older, otherwise print “Minor”.

6. Constructor and If-Else:
Create a class named Student with a constructor that initializes Name and Grade.
In the main method, create an object of Student.
Use an if-else statement to print “Pass” if the Grade is 50 or above, otherwise print “Fail”.

7. Static Members and If-Else:
Create a class named Utility with a static method IsEven(int number) that returns true if the number is even.
In the main method, use the IsEven method to check if a number (e.g., 10) is even. Print “Even” if true, otherwise print “Odd”.

8. Namespaces and If-Else:
Create a namespace Company.Employees with a class Employee having properties Name and Salary.
In the main method, create an object of Employee.
Use an if-else statement to print “High Salary” if the Salary is greater than 100,000, otherwise print “Low Salary”.

9. DateTime and If-Else:
Get the current date using DateTime.Now.
Use an if-else statement to print “Weekend” if today is Saturday or Sunday, otherwise print “Weekday”.

10. Class and Constructor with If-Else:
Create a class named Book with properties Title and Price, and a constructor to initialize them.
In the main method, create an object of Book.
Use an if-else statement to print “Expensive” if the Price is above 500, otherwise print “Affordable”.

11. Static Property and If-Else:
Create a class named Settings with a static property IsDarkModeEnabled.
In the main method, use an if-else statement to print “Dark Mode is On” if IsDarkModeEnabled is true, otherwise print “Dark Mode is Off”.

12. Namespace with DateTime and If-Else:
Create a namespace WeatherApp with a class Weather having a method GetTemperature() that returns the current temperature.
In the main method, use an if-else statement to print “Hot” if the temperature is above 30°C, otherwise print “Cool”.

13. DateTime and Static Method with If-Else:
Create a class named DateHelper with a static method IsLeapYear(int year) that returns true if the year is a leap year.
In the main method, use an if-else statement to check if the current year is a leap year and print “Leap Year” if true, otherwise print “Not a Leap Year”.