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.
- 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
- var booleanExpression = true;
-
- if (booleanExpression)
- {
-
- }
If-else syntax- if(booleanExpression)
- {
-
- }
- else
- {
-
- }
If-elseif-else statement syntax
- if(booleanExpression)
- {
-
- }
- else if (booleanExpression)
- {
-
- }
- else
- {
-
- }
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
- var dayOfWeek = DateTime.Now.DayOfWeek;
-
- string statement = "Thank God Its Friday!";
-
- if (dayOfWeek == DayOfWeek.Friday)
- {
- WriteLine(statement);
- }
If-else syntax
- const int MY_BIRTH_YEAR = 1982;
-
- int birthYear = DateTime.Now.AddYears(-37).Year;
-
- bool booleanExpression = (birthYear == MY_BIRTH_YEAR);
-
- string statement = "";
-
- if (booleanExpression)
- {
- statement = $"Yes I was born in the year {MY_BIRTH_YEAR}";
-
- WriteLine(statement);
- }
- else
- {
- statement = $"Sorry I was born in the year {MY_BIRTH_YEAR}";
-
- WriteLine(statement);
- }
If-elseif-else statement syntax
- int operatingSystemVersion = Environment.OSVersion.Version.Major;
-
- if (operatingSystemVersion == 10)
- {
- WriteLine("You are probably on Windows 10|2016|2019");
- }
- else if (operatingSystemVersion == 6)
- {
- WriteLine("You are probably on Windows 8|7|Vista|2008");
- }
- else if (operatingSystemVersion == 5)
- {
- WriteLine("You are probably on Windows XP|2000|2003");
- }
- else
- {
- WriteLine("Your computer is a dinosaur");
- }
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.
- int operatingSystemVersion = Environment.OSVersion.Version.Major;
-
- if (operatingSystemVersion == 10)
- WriteLine("You are probably on Windows 10|2016|2019");
- else if (operatingSystemVersion == 6)
- WriteLine("You are probably on Windows 8|7|Vista|2008");
- else if (operatingSystemVersion == 5)
- WriteLine("You are probably on Windows XP|2000|2003");
- else
- 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.
- WriteLine("True and True => {0}", true & true);
- WriteLine("True and False =>{0}", true & false);
- WriteLine("False and True=> {0}", false & true);
- WriteLine("False and False =>{0}", false & false);
-
- WriteLine("True or True => {0}", true | true);
- WriteLine("True or False =>{0}", true | false);
- WriteLine("False or True=> {0}", false | true);
- 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.
- WriteLine("****** Welcome to this program ******");
-
- WriteLine("Please enter your age:");
-
- int ageResult = 0;
-
- int.TryParse(ReadLine(), out ageResult);
-
- if(ageResult < 0)
- {
- WriteLine("You are not born yet!");
- }
- else if(ageResult>=1 & ageResult <= 10)
- {
- WriteLine("You are still young");
-
- if(ageResult >=7 & ageResult <= 10)
- {
- WriteLine("You are starting to have a wisdom");
- }
- }
- else if(ageResult>=11 & ageResult <= 19)
- {
- if (ageResult == 11 || ageResult == 12)
- {
- WriteLine("Are you preparing to become a teenager?");
- }
- else
- {
- WriteLine("You are a teenager.");
- }
- }
- else if(ageResult >=20 & ageResult <= 120)
- {
- WriteLine("You are an adult");
-
- if(ageResult >= 100 & ageResult<=120)
- {
- WriteLine("Are you still alive?");
- }
- }
- else
- {
- WriteLine("Unable to determine if you are still alive!");
- }
Till next time, happy programming!