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.
- using static System.Console;
- var booleanExpression = true;
- if (booleanExpression)
- {
- //write your statement here
- }
- if(booleanExpression)
- {
- //write your statement here
- }
- else
- {
- //write your statement here
- }
- if(booleanExpression)
- {
- //write your statement here
- }
- else if (booleanExpression)
- {
- //write your statement here
- }
- else
- {
- //write your statement here
- }
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.
- var dayOfWeek = DateTime.Now.DayOfWeek; //|Lets check if the day of week is Friday|
- string statement = "Thank God Its Friday!";
- if (dayOfWeek == DayOfWeek.Friday) //|When it is Friday let's celebrate yehey!
- {
- WriteLine(statement);
- }
- const int MY_BIRTH_YEAR = 1982;//|Year I was born|
- int birthYear = DateTime.Now.AddYears(-37).Year;
- bool booleanExpression = (birthYear == MY_BIRTH_YEAR);
- string statement = "";
- if (booleanExpression) //| If the 'booleanExpression' is true this will be executed.|
- {
- statement = $"Yes I was born in the year {MY_BIRTH_YEAR}";
- WriteLine(statement);
- }
- else //| If the 'booleanExpression' is equivalent to false this will be executed.|
- {
- statement = $"Sorry I was born in the year {MY_BIRTH_YEAR}";
- WriteLine(statement);
- }
- 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");
- }
- 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");
| 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 |
- 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);
- 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!");
- }


Join the conversation! Your thoughts help the community grow.