In this post, we will try to explore the if, if-elseif-else, if-else statement of C# language by focusing on its syntax and showing more examples in the latter part. Practically, these statements are used for logical or conditional testing within a block of code. To elaborate further, if you have to check for a certain value and evaluate it to either true or false and then from the result of the evaluation, you can do some actions. Expression, in technical terms, is also known as “Boolean expression” which can be evaluated to true or false.

About the examples below, you might notice the usage of WriteLine-method directly, it is because of the using static directive.
  1. using static System.Console;
Now, let's get started with the syntaxes of the if, if-else, if-elseif-else statements. Please, see the syntaxes below.
If-statement syntax
  1. var booleanExpression = true;
  2. if (booleanExpression)
  3. {
  4. //write your statement here
  5. }
If-else syntax
  1. if(booleanExpression)
  2. {
  3. //write your statement here
  4. }
  5. else
  6. {
  7. //write your statement here
  8. }
If-elseif-else statement syntax
  1. if(booleanExpression)
  2. {
  3. //write your statement here
  4. }
  5. else if (booleanExpression)
  6. {
  7. //write your statement here
  8. }
  9. else
  10. {
  11. //write your statement here
  12. }

Now that we have a good grasp of the basic syntax. Let’s raise the bar a bit by showing examples based on the syntax shown above. Let’s get started.

If-statement syntax
  1. var dayOfWeek = DateTime.Now.DayOfWeek; //|Lets check if the day of week is Friday|
  2. string statement = "Thank God Its Friday!";
  3. if (dayOfWeek == DayOfWeek.Friday) //|When it is Friday let's celebrate yehey!
  4. {
  5. WriteLine(statement);
  6. }
If-else syntax
  1. const int MY_BIRTH_YEAR = 1982;//|Year I was born|
  2. int birthYear = DateTime.Now.AddYears(-37).Year;
  3. bool booleanExpression = (birthYear == MY_BIRTH_YEAR);
  4. string statement = "";
  5. if (booleanExpression) //| If the 'booleanExpression' is true this will be executed.|
  6. {
  7. statement = $"Yes I was born in the year {MY_BIRTH_YEAR}";
  8. WriteLine(statement);
  9. }
  10. else //| If the 'booleanExpression' is equivalent to false this will be executed.|
  11. {
  12. statement = $"Sorry I was born in the year {MY_BIRTH_YEAR}";
  13. WriteLine(statement);
  14. }
If-elseif-else statement syntax
  1. int operatingSystemVersion = Environment.OSVersion.Version.Major;
  2. if (operatingSystemVersion == 10)
  3. {
  4. WriteLine("You are probably on Windows 10|2016|2019");
  5. }
  6. else if (operatingSystemVersion == 6)
  7. {
  8. WriteLine("You are probably on Windows 8|7|Vista|2008");
  9. }
  10. else if (operatingSystemVersion == 5)
  11. {
  12. WriteLine("You are probably on Windows XP|2000|2003");
  13. }
  14. else
  15. {
  16. WriteLine("Your computer is a dinosaur");
  17. }
Note: Just remember that curly braces {} are optional for single statements. However; if you are still new in programming it is a good habit to always use curly braces even if you are just using one statement. Once you have become an expert the decision is yours. Let us try to see an example below.
  1. int operatingSystemVersion = Environment.OSVersion.Version.Major;
  2. if (operatingSystemVersion == 10)
  3. WriteLine("You are probably on Windows 10|2016|2019");
  4. else if (operatingSystemVersion == 6)
  5. WriteLine("You are probably on Windows 8|7|Vista|2008");
  6. else if (operatingSystemVersion == 5)
  7. WriteLine("You are probably on Windows XP|2000|2003");
  8. else
  9. WriteLine("You're computer is a dinosaur");
Now, that we have a good understanding of the basic syntax. It is now appropriate to introduce logical operators.

Condition Logical Operator Condition Result
true & true true
true & false false
false & true false
false & false false
true | true true
true | false true
false | true true
false | false false
See the code equivalent of the truth table below.
  1. WriteLine("True and True => {0}", true & true);
  2. WriteLine("True and False =>{0}", true & false);
  3. WriteLine("False and True=> {0}", false & true);
  4. WriteLine("False and False =>{0}", false & false);
  5. WriteLine("True or True => {0}", true | true);
  6. WriteLine("True or False =>{0}", true | false);
  7. WriteLine("False or True=> {0}", false | true);
  8. WriteLine("False or False =>{0}", false | false);
One more operator that we need to learn or be familiar with to utilize the if-else statements. The comparison operator ==, <, >, <=, and >=.
In this last example, we will see the combinations of the if, if-else, if-elseif-else, nested if-else, with logical and comparison operators.
  1. WriteLine("****** Welcome to this program ******");
  2. WriteLine("Please enter your age:");
  3. int ageResult = 0;
  4. int.TryParse(ReadLine(), out ageResult);
  5. if(ageResult < 0)
  6. {
  7. WriteLine("You are not born yet!");
  8. }
  9. else if(ageResult>=1 & ageResult <= 10)
  10. {
  11. WriteLine("You are still young");
  12. if(ageResult >=7 & ageResult <= 10)
  13. {
  14. WriteLine("You are starting to have a wisdom");
  15. }
  16. }
  17. else if(ageResult>=11 & ageResult <= 19)
  18. {
  19. if (ageResult == 11 || ageResult == 12)
  20. {
  21. WriteLine("Are you preparing to become a teenager?");
  22. }
  23. else
  24. {
  25. WriteLine("You are a teenager.");
  26. }
  27. }
  28. else if(ageResult >=20 & ageResult <= 120)
  29. {
  30. WriteLine("You are an adult");
  31. if(ageResult >= 100 & ageResult<=120)
  32. {
  33. WriteLine("Are you still alive?");
  34. }
  35. }
  36. else
  37. {
  38. WriteLine("Unable to determine if you are still alive!");
  39. }
Till next time, happy programming!