Website development and design blog, tutorials and inspiration

C# Conditional Statements

How to use IF statements in C#

By , 6th November 2007 in C#

Conditional statements allow different sections of code, or actions, to be executed depending on a condition being met.
 

This article relates to an old version of the .Net Framework. While the contents are still relevant, future versions of the framework may change, or improve upon, the contents of this article.

Conditions can be used to validate user input, display certain data depending on date or day of the week, or any one of thousands of conditions.

If...Else

The most commonly used conditional statement is the If...Else block, in which a statement is evaluated to true or false and depending on the result, a different section of code is executed.

In this sample, the statement checks if the condition is true if it is it executes the first block of code. If the condition is false then it executes the second.

Conditions in the if statement must ALWAYS evaluate to TRUE or FALSE.

  1. int x = 5;
  2.  
  3. if (x >= 3)
  4. {
  5. Console.WriteLine("X is greater than or equal to 3.");
  6. }
  7. else
  8. {
  9. Console.WriteLine("X is less than 3.");
  10. }

Have a play by changing the value of X and seeing which code block is executed.

If you only have ONE statement to be executed, like our example, you can omit the braces, but if you have more than one statement you will need the braces. This can make the code a little easier to read, but some consider it bad form.

  1. int x = 5;
  2.  
  3. if (x >= 3)
  4. Console.WriteLine("X is greater than or equal to 3.");
  5. else
  6. Console.WriteLine("X is less than 3.");

You don't need to have an else statement. If all you want is to output something on Monday for example:

  1. if (dayOfWeek == Monday)
  2. Console.WriteLine("Today is Monday");

This will write out "Today is Monday" only if dayOfWeek is Monday, otherwise the code is skipped over.

Nested If Statements

If you have multiple scenarios, you can nest if... else statements:

  1. if (dayOfWeek == Monday)
  2. Console.WriteLine("Today is Monday");
  3. else if (dayOfWeek == Tuesday)
  4. Console.WriteLine("Today is Tuesday");
  5. else if (dayOfWeek == Wednesday)
  6. Console.WriteLine("Today is Wednesday");
  7. else if (dayOfWeek == Thursday)
  8. Console.WriteLine("Today is Thursday");
  9. else if (dayOfWeek == Friday)
  10. Console.WriteLine("Today is Friday");

This can get a little complicated for many conditions, so a much easier and efficient method would be to use a switch statement.

Switch Statement

In the previous example with the multiple daysOfWeek, by the time Friday is tested the program has executed four if statements which are a very inefficient method. The code is also a little difficult to read and understand, so a much better method would be to use a switch case statement.

The above example can be simplified to this:

  1. switch(dayOfWeek)
  2. {
  3. case "Monday":
  4. Console.WriteLine("Today is Monday");
  5. break;
  6. case "Tuesday":
  7. Console.WriteLine("Today is Tuesday");
  8. break;
  9. case "Wednesday":
  10. Console.WriteLine("Today is Wednesday");
  11. break;
  12. case "Thursday":
  13. Console.WriteLine("Today is Thursday");
  14. break;
  15. case "Friday":
  16. Console.WriteLine("Today is Friday");
  17. break;
  18. }

As you can see, this is much easier to read, and there is only one conditional statement that gets executed so the code is more efficient and faster to execute. Each section of code to execute ends with a break keyword. This tells the compiler that the case has ended. Unlike some languages, such as PHP, you cannot "fall through" case blocks if the code exists. You can only fall through when one case directly follows another case statement:

  1. case "Saturday":
  2. Console.WriteLine("Today is a Weekend"); // This will ERROR!
  3. case "Sunday":
  4. Console.WriteLine("Today is a Weekend");
  5. break;
  1. case "Saturday":
  2. case "Sunday":
  3. Console.WriteLine("Today is a Weekend"); // This is OK!
  4. break;

Another useful part of the Switch...Case block is that of a default action. If none of the cases specified is met, then the default will be executed.

  1. switch(dayOfWeek)
  2. {
  3. case "Monday":
  4. Console.WriteLine("Today is Monday");
  5. break;
  6. case "Tuesday":
  7. Console.WriteLine("Today is Tuesday");
  8. break;
  9. case "Wednesday":
  10. Console.WriteLine("Today is Wednesday");
  11. break;
  12. case "Thursday":
  13. Console.WriteLine("Today is Thursday");
  14. break;
  15. case "Friday":
  16. Console.WriteLine("Today is Friday");
  17. break;
  18. default:
  19. Console.WriteLine("This the weekend!!!");
  20. break;
  21. }

Default must always be the last statement.

C# allows the use of other keywords as well as break to control the flow of the Switch statement. You can use goto, return and throw.

Goto

The use of Goto does not fall within the structured programming methodology and should be avoided at all costs.

Return

The return keyword is used to return a value back to the calling function.

Throw

The throw is used to raise an exception, which will be captured by your Try... Catch block. This will be covered in much more detail in the section about Exception Handling.

Examples

  1. switch(dayOfWeek)
  2. {
  3. case "Monday":
  4. Console.WriteLine("Today is Monday");
  5. break;
  6. case "Tuesday":
  7. return "Today is Tuesday";
  8. break;
  9. case "Wednesday":
  10. throw new Exception("Today is Wednesday");
  11. break;
  12. case "Thursday":
  13. goto case "Monday";
  14. break;
  15. }

Summary and Conclusions

We have seen how to use conditional statements to perform different actions based on whether the condition evaluates to true or false. We also saw how a switch statement can be used where there are multiple if else statements to improve readability and performance.

Comments

There are no comments for this post. Be the first!

Leave a Reply

Your email address will not be published.