Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
March 31, 2022 10:01 am GMT

How To Differentiate If, Else if, Else (but with my mother as example)

Hello everyone! My name is Lidia, glad to publish in Dev.to.

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 (dont worry, now you will understand everything).

Understanding IF Statement

Lets start by understanding the if statement. Lets 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. Lets look at the example structure:

if (condition){  // code executed if condition is true}

What does a condition mean? Lets 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.

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

Lets 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, its real):

One day I didnt do my homework and because I didnt do it, my mother (besides getting angry) didnt 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 hadnt done my homework and hadnt helped my brother? Well, lets look at the last conditional.

Understanding Else Statement

Lets 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 mothers 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. Lets 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.

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

And remember: Now you can try for free our .NET obfuscator. You can also protect your applications directly from Visual Studio with the .NET Obfuscator for Visual Studio


Original Link: https://dev.to/dotnetsafer/how-to-differentiate-if-else-if-else-but-with-my-mother-as-example-588l

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To