Conditional Statement With C#

Introduction

Conditional statements are used in C# or any other object-oriented programming language to check the condition for a particular output. In other words when any program has some logic to identify which one is to begin that means conditional statements produce the output according to the checked condition.

In a C# are used four types of conditional statements,

  1. if()
  2. else
  3. else if()
  4. switch()

if()

The if() conditional statement is always used for checking only true conditions.

syntax for if() conditional statements,

if (condition)
{
  //statements
}

For example,

if (20 > 10)
{
  Console.WriteLine(“20 is greater than 10”);
}

else

The else conditional statements have two different blocks of statements, one is true and other is false.

if(condition)
{
  //true statements;
}
else
{
  //false statements;
}

For example,

int n=24;
if (n % 2 == 0)
{
    Console.WriteLine(“{0} is Even Number”, n);
}
else
{
   Console.WriteLine(“{0} is Odd Number”, n);
}

else if()

The else if() conditional statements are used to check in between conditions, which means condition about conditional is called else if()

if (condition1)
{
  //statement1;
}
else if(condition2)
{
  //statements2;
}
else
{
  //statements3;
}

For example,

if (20 < 10) {
    Console.WriteLine("20 is greater");
}
else if (10 < 20)
{
    Console.WriteLine("20 is greater");
}
else
{
    Console.WriteLine("10 is Less");
}

switch()

The switch statements have multiple choices but execute anyone at a time.

switch (condition)
{
    case1:
        //statements1;
        break;
    Case2:
        //statements2;
        break;
    Case3:
        //statements3;
        break;
    Case4:
        //statements4;
        break;
    Case5:
        //statements5;
        break;
    default:
        //default statements;
        break;
}

For example,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Exception {
    internal class Program {
        static void Main(string[] args) {
            int day;
            Console.WriteLine("Enter the day number : ");
            day = Convert.ToInt32(Console.ReadLine());
            switch (day) {
                case 1:
                    Console.WriteLine("Monday");
                    break;
                case 2:
                    Console.WriteLine("Tuesday");
                    break;
                case 3:
                    Console.WriteLine("Wednesday");
                    break;
                case 4:
                    Console.WriteLine("Thursday");
                    break;
                case 5:
                    Console.WriteLine("Friday");
                    break;
                case 6:
                    Console.WriteLine("Saturday");
                    break;
                case 7:
                    Console.WriteLine("Sunday");
                    break;
                default:
                    Console.WriteLine("Invalid input");
                    break;
            }
            Console.ReadLine();
        }
    }
}

Output

Conclusion

Conditional statement is used in our project to check the condition as per the requirements of the users. Conditional statement is used to reduce the line of code in a program.


Similar Articles