MOCKSTACKS
EN
Questions And Answers

More Tutorials








C# If ... Else If ... Else

C# support the following conditional statements:

  • if to specify a block of code to be executed, if a specified condition is met and return true
  • else to specify a block of code to be executed, if the same condition was not met and return false
  • else if to specify a new condition other than if, this condition will tested after first one
  • switch to specify many alternative blocks of code to be executed

C# If..Else if..else Syntax

if (condition1) 
{

  // block of code to be executed if the condition1 is True

} else if(condition2) {

  // block of code to be executed if the condition2 is True

} else if(condition3) {

  // block of code to be executed if the condition3 is True

} else {

  // block of code to be executed if non of the above conditions was met

}

C# if..else if..else Example

using System;

namespace MyApplication
{
  class Program
  {
    static void Main(string[] args)
    {
      int dayOfWeekNumber = 1;
      
      if (dayOfWeekNumber  == 1) 
      {
        Console.WriteLine("It's Monday");
      } 
      else if (dayOfWeekNumber  == 2) 
      {
        Console.WriteLine("It's Tuesday");
      } 
      else if (dayOfWeekNumber  == 3) 
      {
        Console.WriteLine("It's Wednesday");
      } 
      else if (dayOfWeekNumber  == 4) 
      {
        Console.WriteLine("It's Thursday");
      } 
      else if (dayOfWeekNumber  == 5) 
      {
        Console.WriteLine("It's Friday");
      } 
      else if (dayOfWeekNumber  == 6) 
      {
        Console.WriteLine("It's Sturday");
      } 
      else if (dayOfWeekNumber  == 7) 
      {
        Console.WriteLine("It's Sunday");
      } else {
        Console.WriteLine("Wrong day number!");
      }
    }
  }
}

Output

It's Monday

Conclusion

In this page (written and validated by ) you learned about C# If ... Else If ... Else . What's Next? If you are interested in completing CS tutorial, your next topic will be learning about: CS Switch Statement.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.