When we learn to program, we start with the basics and as we learn, we move up the level and get into more advanced territory. This often makes us forget simple things and it is always good to refresh and reinforce learned concepts.
Using conditionals in our code is one of the first things we learn as we start to program. That is why many developers forget over time what is the function of each one and how to use them. This often is not the fault of the developer, it is the fault of the teacher or course where he learned it.
That is why I have decided to clarify once and for all the differences in the statements: if, else, else if.
As I have never liked theory and have always preferred to use good examples to learn things, this will be no exception. So I will use my mother as an example (don’t worry, now you will understand everything).
Understanding IF Statement
Let’s start by understanding the if
statement. Let’s start with the fact that it contains a boolean condition. And at the time of execution, if that condition evaluates to true, then it will proceed to execute the code block. Let’s look at the example structure:
if (condition)
{
// code executed if condition is true
}
What does a condition mean? Let’s think of a question with 2 possible answers:
- Yes
- No
Entering again into the context of programming, being boolean, Yes would be true
and No would be false
.
Let’s use my mother to explain it better.
I study from Monday to Friday and it is on Saturdays when I like to rest and go out with my friends at night, but my mother only lets me go out if I do the things she asks me to do.
So, on Saturdays when I ask my mother if she lets me go out with my friends, she always asks me if I have done my homework. So the condition would be like this:
if (Lidia.FinishedHomework)
{
Lidia.PartyWithFriends()
}
As we can see, I will only be able to go out with my friends if I have done my homework — Yes, I have. So I can go out with my friends.
Let’s move on to else if
and later we will see if several can be used, how they can be mixed and in what way.
Understanding Else IF Statement
To resume in the simplest way else if
is used to specify a new condition in case the previous condition returns false
. The basic structure would be as follows:
if (condition 1)
{
// code executed if condition is true
}
else if {condition 2}
{
// code executed if condition is true
}
To situate ourselves again with the example of what my mother would do (all this has happened to me, it’s real):
One day I didn’t do my homework and because I didn’t do it, my mother (besides getting angry) didn’t let me go out with my friends but, besides that, she asked me if I helped my brother (yep, I have a little brother) with his homework. This is where the else if
condition comes in:
If I helped my little brother with his homework (condition), my mother would let me go out for dinner.
if (Lidia.FinishedHomework)
{
Lidia.PartyWithFriends()
}
else if {Lidia.helpedBrother}
{
Lidia.GoDinner()
}
In this case we can see that I have not done my homework (the first condition is not complied and the code it contains is not executed), but having helped my brother (the second condition is fulfilled), so the code it contains would be executed.
In this case, going out to dinner.
Now you may be wondering… What if I hadn’t done my homework and hadn’t helped my brother? Well, let’s look at the last conditional.
Understanding Else Statement
Let’s understand else
as the end. This statement can only go after an if
or an if else
and can only be used once in if-else
statements. This statement cannot contain any type of condition, it will always be executed when all other previous conditions (if
/ else if
) are false
.
if (condition 1)
{
// code executed if condition is true
}
else if {condition 2}
{
// code executed if condition is true
}
else
{
// code exectuted if the previous conditions are false
}
Now is where we come to the most important question. If we go back to my mother’s example, what happened the day I neither did my homework nor helped my brother? I would like to know if you can find out:
if (Lidia.FinishedHomework)
{
Lidia.PartyWithFriends()
}
else if {Lidia.helpedBrother}
{
Lidia.GoDinner()
}
else
{
Lidia.punished()
}
Explaining it, if the first condition (within if
) is not fulfilled and the second condition (within else if
) is not fulfilled, the final else
will be executed. Result? I am punished🥺.
This is not the only structure that can be made. Actually statements can be nested inside each other. Let’s take a closer look now.
Understanding (nested) IF Statements
C# if else
tatements can be nested inside other if else
statements. This allows for more readable code (especially with if
statements).
if (condition 1)
{
if (condition 2)
{
// executed if conditions 1 & 2 are true
}
else
{
// executed if condition 1 is true & condition 2 is false
}
}
else
{
if (condition 3)
{
// executed if condition 1 is false & condition 3 is true
}
else
{
// executed if condition 1 is false & condition 3 is false
}
At first glance it may seem like a lot of code for a novice, but if you try to read it you will see that it is much simpler than it looks.
Let’s take a more practical example for your reference:
if (Lidia.FinishedHomework)
{
if (Lidia.hasWorkout)
{
Lidia.GoGym()
}
else
{
Lidia.GoCinema();
}
}
else
{
if (Lidia.HasExam)
{
Lidia.StartStudying();
}
else
{
Lidia.DoHomework();
}
}
📚 Apply what you have learned
Now you are going to see a small example of nested statements, it may seem complex but it is relatively easy. I would like you to analyze it and if you have learned well, answer me in the comments to this question:
What do I do just before going to sleep?👇
Lidia = {
FinishedHomework: false
hasWorkout: false
hasExam: true
hasBirthday: false
hasStudied: false
}
while(!Lidia.FinishedHomework || Lidia.hasWorkout)
{
if (Lidia.FinishedHomework)
{
if (Lidia.hasWorkout)
{
Lidia.GoGym()
Lidia.hasWorkout = false;
}
else if (!Lidia.hasBirthday)
{
Lidia.GoCinema();
}
else {
Lidia.PrepareGifts();
}
}
else
{
if (Lidia.HasExam && !Lidia.hasStudied)
{
Lidia.StartStudying();
Lidia.hasStudied = true;
}
else
{
Lidia.DoHomework();
Lidia.FinishedHomework = true;
}
}
}
Lidia.GoSleep()
Now I would like to ask you to answer what you think would be the answer to the above code (What do I do just before going to sleep?), let me know in the comments. And also, I would like to know your doubts or topics you would like to understand more about. I read you and answer you all🥰.