C# if Statements 101

Introduction to C# if statements: else if, else. If statement is part of C# conditional branching statements, as we explore this feature of the language will try to see a straight forward example that might be helpful to you as a beginner.

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.   
  3. if (booleanExpression)   
  4. {   
  5.     //write your statement here  
  6. }  
 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.   
  3. string statement = "Thank God Its Friday!";  
  4.   
  5. if (dayOfWeek == DayOfWeek.Friday) //|When it is Friday let's celebrate yehey!  
  6. {  
  7.     WriteLine(statement);  
  8. }  
If-else syntax
  1. const int MY_BIRTH_YEAR = 1982;//|Year I was born|  
  2.   
  3. int birthYear = DateTime.Now.AddYears(-37).Year;  
  4.   
  5. bool booleanExpression = (birthYear == MY_BIRTH_YEAR);  
  6.   
  7. string statement = "";  
  8.   
  9. if (booleanExpression) //| If the 'booleanExpression' is true this will be executed.|  
  10. {  
  11.     statement = $"Yes I was born in the year {MY_BIRTH_YEAR}";  
  12.   
  13.     WriteLine(statement);  
  14. }  
  15. else //| If the 'booleanExpression' is equivalent to false this will be executed.|  
  16. {  
  17.     statement = $"Sorry I was born in the year {MY_BIRTH_YEAR}";  
  18.   
  19.     WriteLine(statement);  
  20. }  
If-elseif-else statement syntax 
  1. int operatingSystemVersion = Environment.OSVersion.Version.Major;  
  2.   
  3. if (operatingSystemVersion == 10)  
  4. {  
  5.     WriteLine("You are probably on Windows 10|2016|2019");  
  6. }  
  7. else if (operatingSystemVersion == 6)  
  8. {  
  9.     WriteLine("You are probably on Windows 8|7|Vista|2008");  
  10. }  
  11. else if (operatingSystemVersion == 5)  
  12. {  
  13.     WriteLine("You are probably on Windows XP|2000|2003");  
  14. }  
  15. else  
  16. {  
  17.     WriteLine("Your computer is a dinosaur");  
  18. }  
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.   
  3. if (operatingSystemVersion == 10)  
  4.     WriteLine("You are probably on Windows 10|2016|2019");  
  5. else if (operatingSystemVersion == 6)  
  6.     WriteLine("You are probably on Windows 8|7|Vista|2008");  
  7. else if (operatingSystemVersion == 5)  
  8.     WriteLine("You are probably on Windows XP|2000|2003");  
  9. else  
  10.     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.   
  6. WriteLine("True or True => {0}"true | true);  
  7. WriteLine("True or False =>{0}"true | false);  
  8. WriteLine("False or True=> {0}"false | true);  
  9. 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.   
  3. WriteLine("Please enter your age:");  
  4.   
  5. int ageResult = 0;  
  6.   
  7. int.TryParse(ReadLine(), out ageResult);  
  8.   
  9. if(ageResult < 0)  
  10. {  
  11.     WriteLine("You are not born yet!");  
  12. }  
  13. else if(ageResult>=1 & ageResult <= 10)  
  14. {  
  15.     WriteLine("You are still young");  
  16.   
  17.     if(ageResult >=7 & ageResult <= 10)  
  18.     {  
  19.         WriteLine("You are starting to have a wisdom");  
  20.     }  
  21. }  
  22. else if(ageResult>=11 & ageResult <= 19)  
  23. {  
  24.     if (ageResult == 11 || ageResult == 12)  
  25.     {  
  26.         WriteLine("Are you preparing to become a teenager?");  
  27.     }  
  28.     else  
  29.     {  
  30.         WriteLine("You are a teenager.");  
  31.     }  
  32. }  
  33. else if(ageResult >=20 & ageResult <= 120)  
  34. {  
  35.     WriteLine("You are an adult");  
  36.   
  37.     if(ageResult >= 100 & ageResult<=120)    
  38.     {  
  39.         WriteLine("Are you still alive?");  
  40.     }  
  41. }  
  42. else  
  43. {  
  44.     WriteLine("Unable to determine if you are still alive!");  
  45. }  
 Till next time, happy programming!