We run tasks based on conditions. For example let’s say we want to go out for a walk; this task depends on a couple of conditions: For example if the weather was good or if there was sunlight we might run this task. But probably you won’t go for a walking in the middle of night!
Basically our tasks run on if-else conditions.
For example if I get my salary this month, I’ll buy a brand new TV otherwise (else) I need to wait for the next month.
Basically we use the word if and else in our lives in many different situations to emphasize a conditional outcome.
In the world of C# programming we have the same situation as well.
For example part of our program might depend on the input that it takes from a user! If the value was in the range, then that part of the program should run otherwise an error message should be returned to the user.
These types of conditions are called if condition and we use if else statements to handle them.
Here’s the structure of the if else statements:
if (condition){
//body of the if-statement where code is and will be run if result of the condition is true
}else{
//the code here will only run if result of the condition is not true.
}
-
`if`: To begin with, we use the `if` keyword to create an `if`
statement. -
`()`: after the `if` keyword comes the parentheses. This is where
we set the condition or conditions to be checked.
Note: The comparison operators are very handy here.
Here we can run a condition on the value of a variable and if the result of that condition was true then the body of the if statement will run.
For example the condition might be age == 21 which means check and see if the value of the age variable is equal to 21. If the result was true then instructions within the body of if-statement will run. Otherwise the body of the else statement is the one to run.
Note: the value of a variable of type Boolean can also be used in the parentheses of the if statement.
In this case the value true means the condition is true and so the body of the if statement can run. Also the value false means the condition is false and so the body of the if statement should be skipped instead.
-
`{}`: The open and close braces that comes after the parentheses,
declare the starting and closing point of the body of the `if`
statement. This is basically the scope of the `if` statement. Any code
here in this block will be run if the result of the condition set in the
parentheses is true. -
`else`: the else keyword comes after the closing brace `}` of the
`if-statement` and is used to set the instructions that should run if
only the condition’s result of the `if-statement` is false. Basically
the `else` statement is like a plan B!
Notes:
-
The body (AKA the scope) of `else-statement` is within the braces
that comes after the `else` keyword. Also this `else-statement` does not
have parentheses to put a new condition. -
The use of `else` is optional and we can simply skip it. In this
case if the condition of the `if-statement` was false then the
instructions that come after the scope of the `if-statement` will run
(of course if there’s any).
Example:
using System;
namespace quickExample
{
public class Program
{
public static void Main(string[] args)
{
int age = 20;
if (age > 21)
{
Console.WriteLine("Yes the age is greater than 21");
}
else
{
Console.WriteLine("The age is less than 21");
}
}
}
}
Output:
The age is less than 21
Here we have a variable named age and its value is 20. Inside the condition of the if statement we’ve compared this variable with the value 21 to see if it’s greater than 21 or not. But obviously the value of the age variable is less than 21 and so the body of the if statement will be skipped (because the result of the condition is false) and it’s the body of the else statement that will be run.
else if:
There are times that we have multiple conditions and want to run a different set of instructions depending on which of these conditions result true.
This is where the else if statement comes in.
The statement is an addition to the if-else statement and as a matter of fact, it will sit between if and else statements.
Take a look at the structure below:
if (condition_1){
//body of the if-statement where code is and will be run if the condition is true
}else if (condition_2){
//body of the else-if-statement where the code is and will be run if the condition_2 is true
}else if (condition_N){
//body of the else-if-statement where the code is and will be run if the condition_N is true
}else{
//the code here will only run if none of the conditions in `if` and `else if` result true.
}
else-if: if the condition of the if statement was not true, then the condition of the first else-if statement will be checked. If the first else-if condition’s result was false as well, the second one will be checked and this process will go on until one of the else if statements result true and in that case the instructions within its body will run. Otherwise if no-condition within the if or else if statements result true, it’s the instructions of the else statement to run.
Notes:
-
The use of `else if` statement is optional and depending on the
need of the program we can add as many as we want between the `if else`
statements. -
If the condition of the `if` statement was true, the rest of
`else if` statements and the `else` statement will be skipped.
Also the same is true for any else if statement (any else-if statement results true, the rest of else-if statements will be skipped and only the body of that else-if statement will run)
Example:
using System;
namespace quickExample
{
public class Program
{
public static void Main(string[] args)
{
int value = 200;
if (value == 100)
{
Console.WriteLine("The value is: 100");
}
else if (value == 200)
{
Console.WriteLine("The value is: 200");
}
else if (value == 150)
{
Console.WriteLine("The value is: 150");
}
else
{
Console.WriteLine("None of the conditions matched the valuen");
}
}
}
}
Output:
The value is: 200
Also we can only use one `else` statement.
using System;
namespace quickExample
{
public class Program
{
public static void Main(string[] args)
{
int money = 1000;
int age = 21;
if (age >= 21 && money > 5000)
{
Console.WriteLine("The person can enter to the barn");
}
else if (age >= 21 && money < 5000)
{
Console.WriteLine("The person can legally enter to a bar but she can't come to this bar because she doesn't have enough money");
}
else if (age < 21 && money > 5000)
{
Console.WriteLine("The person has the money but she is under legal age.");
}
}
}
}
Output:
The person can legally enter to a bar but she can’t come to this bar because she doesn’t have enough money