Sponsored Ad

Friday, September 24, 2010

How to use If Else in C# | If Else Example using C#

 

This is program of If Else complex and simple examples. This example shows the exact use of If Else statement in C#.

How to use If Else in C# | If Else Example using C#

Source Code of If Else Example:

using System;
using System.Text;
using System.Data;
namespace Console_App
{
    public class clsIfElase
    {
        public static void Main()
        {
            try
            {            
                int iInt;

                Console.Write("Enter a number for IF Else Example: ");            
                iInt = Int32.Parse(Console.ReadLine());
                if (iInt > 0)
                {
                    Console.WriteLine("{0} is greater than zero.", iInt);
                }

               if (iInt < 0)
                    Console.WriteLine("{0} is less than zero.", iInt);

                //simple example
                if (iInt != 0)
                {
                    Console.WriteLine("{0} is not equal to zero.", iInt);
                }
                else
                {
                    Console.WriteLine("{0} is equal to zero.", iInt);
                }
                //complex example
                if (iInt < 0 || iInt == 0)
                {
                    Console.WriteLine("{0} is less than or equal to zero.", iInt);
                }
                else if (iInt > 0 && iInt < 100)
                {
                    Console.WriteLine("{0} is between 1 and 100.", iInt);
                }
                else if (iInt > 101 && iInt < 200)
                {
                    Console.WriteLine("{0} is between 101 and 200.", iInt);
                }
                else if (iInt > 201 && iInt < 300)
                {
                    Console.WriteLine("{0} is between 201 and 300.", iInt);
                }
                else
                {
                    Console.WriteLine("{0} is greater than 300.", iInt);
                }
            }
            catch (Exception ex)
            {
                //handle exception here
            }
            Console.ReadLine();
        }
    }
}

No comments:

Post a Comment

Sponsored Ad

Development Updates