Lesson 4.2

Nested IF Else Programming in C#


Objective

What is Nested If Else Statements?
How to use in c# programming?
What is the usage of Nested If Else?

What is Nested If Else Statements in C#?

When an If Else statement is used inside another If Else constructs is called Nested If. You can use nested If else at several layer you want. Nested IF Else Constructs in c# gives you more power to compare, filter or validate program.

Example

If Else

Nested IF Else Programming Example

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

namespace nested_if_else
{
    class Program
    {
        static void Main(string[] args)
        {
            string day, time;
            Console.WriteLine("Enter a day name: ");
            day = Console.ReadLine();

            Console.WriteLine("Enter AM or PM");
            time = Console.ReadLine();

            if (day == "sunday")
            {
                if (time == "AM")
                {
                    Console.WriteLine("It's morning");
                }
                if (time == "PM")
                {
                    Console.WriteLine("Hurreeyyyyyyy Its party time");
                }
            }
            else
            {
                Console.WriteLine("Go to work");
            }
            Console.ReadKey();
        }
    }
}

	

Output

Enter a day name: sunday
Enter AM or PM: PM
Hurreeyyyyyyy Its party time

__

Summary

In this chapter you have learned about nested if else statements. In the next chapter you will learn Switch Case Statements in C#.
BackNext