CSharp (C#) Nested If..else loop Statement


Nested If is having an if condition within another if condition. When there are many conditions to be verified we could go for Nested..If

Using the similar example from If..Else

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

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            //If a number is positive only then find if its an even number or not.
            int number = Convert.ToInt32(Console.ReadLine());
            if (number > 0)
            {
                int value = number % 2; //If the remainder is zero its an even number.
                if (value == 0)
                {
                    Console.WriteLine("The number {0} is positive and is an even number.", number);
                }
                else
                {
                    Console.WriteLine("The number {0} is positive and is an odd number.", number);
                }
            }
            else
            {
                Console.WriteLine("The number {0} is not a positive number.", number);
            }
            Console.ReadLine();
        }
    }
}

Output :



C Sharp ( C# ) Control Statements

No comments: