Skip to content

How To Differentiate If, Else if, Else in C#

9 de March de 2022
How To Differentiate If, Else if, Else in Csharp

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).

Index

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:

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:

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:

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.

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 (ifelse if) 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:

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).

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:

📚 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?👇

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🥰.

Settings