The If else Statement in C#

The if else statement in C# is used to compare the two or more values and execute the true condition. The if else statement is commonly used along with the logical and comparison operators. Let's consider we have X and Y variables having certain values, then we can write condition for as.

  • If x is less than y : x > y 
  • If x is greater than y : x > y
  • If x is greater than or equals to y : x >= y 
  • If x is no equals to y : x != y 
  • If x is equals to y : x = = y 
  • If x is less than or equals to 10 and y is equals to 20 : x <= 10 && y = = 20 
  • If x is less than or equals to 20 or y is equals to 10 : x <= 20 || y = = 10

The preceding scenarios are some of the scenarios where if the condition is applicable, we can compare any values which are supported by C# datatypes.

The C# if Statement

The if statement can also be used without the else condition. In this scenario only the true code block is evaluated.

Syntax

if (condition) 
{
  // write code logic here if the condition is True
}

Example with integer value

if (x==10) 
{
Console.WriteLine("x is equals to 10");
}

Example with string value
 
if (x=="www.compilemode.com") 
{
Console.WriteLine("x is equals to www.compilemode.com");
}

The else Statement

The else statement is used to execute the False block if the given condition is not True. The if else statement gives more flexibility to perform the action in both the scenarios, whether either condition is true or false.

Syntax

if (condition)
{
  //Write logic here if the condition is True
} 
else 
{
   //Write logic here if the condition is False
}

Example

if (x=="www.compilemode.com") 
{
Console.WriteLine("x is equals to www.compilemode.com");
}
else
{
Console.WriteLine("x is not equals to www.compilemode.com");
}

The Multiple If-else If Statement

if (condition)
{
  //Write logic here if the condition is True
} 
else if(condition)
{
   //Write logic here if the condition is False
}
else
{
   //Write logic here if the condition is False
}

Example Multiple If-else If Statement C#

if (x=="Red")
{
 Console.WriteLine("The colour is red");
} 
else if(x=="Pink")
{
Console.WriteLine("The colour is pink");
}
else
{
   Console.WriteLine("The colour is not matching");
}

Let's write a simple C# console program by creating the console project using Visual Studio to demonstrate the above examples.

using System;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            //The if statement 
            int x = 100;
            int y = 90;

            if (x > y)
            {
                Console.WriteLine("The x is greater than y");
            }

            //The if else statement

            string Name = "www.compilemode.com";

            if (Name== "www.compilemode.com")
            {
                Console.WriteLine("The name is www.compilemode.com");
            }
            else
            {
                Console.WriteLine("The is not www.compilemode.com");
            }

            //The if- else if statement

            string a = "Red";
           
            if (a=="Pink")
            {
                Console.WriteLine("The colour is Pink");
            }
            else if (a=="Red")
            {
                Console.WriteLine("The colour is Red");
            }
            else
            {
                Console.WriteLine("The colour is not matching");
            }

            Console.ReadLine();
        }
    }
}

After running the above program, then the following output will be shown.

Summary


I hope this article is useful to understand the C# if else statement, Explore the www.compilemode.com C# tutorial category to learn basic about the C# language.

Post a Comment

www.CodeNirvana.in

Protected by Copyscape
Copyright © Compilemode