### Intro to Elixir | Lesson 18: Using `if` and `unless`

Elixir
##### Transcript

English (Auto-generated)

friends today we will learn to use if and unless to control the code flow of our elixir applications. Um many programming languages if and else are usually the primary way that you do flow control. Um I intentionally left it to the very end in our electoral series because I generally and I think a lot of developers as well in the electoral community prefer the other approaches um that um we learned things starting with pattern matching and then going to case and conned especially that if isn't actually built as sophisticated as some of the other ones. So we could use if um if we when we want to check a single condition and this is probably why um it's not used as much as some of the other ones because really it's you don't have else f for example, so the way it works we could do something like this if Um maybe I'll use list 1, 2 3 if in um account list Equals equals three new. The list has three elements else I owed up. This is a list Does not have three elements. Yeah the syntax is very straightforward. You put if you give it a condition that should return true or false. You could use truthfully or falsely as well. So for example nail would be false. E and most other values would be truc and then you put do and then some block of code that will execute if this is true and then the else will have the block of code that will execute if this is false if this is not true. So let's go ahead and try this here in I. X. As you might have expected we will see the list has three elements. So what's in here we'll execute. So notice here we are um checking a single condition. We don't have else if so we can't have like multiple checks in this particular case we really have just a single condition that we're checking and we have the option to put an else as well. The else is optional. So if you want if you could just admit it like this if you only want to do one thing in here. Um But yeah like we basically uh you know it's it's handy when you want to do a single a single check. Uh We could use unless as the opposite of if meaning that it will execute the code in its block if the condition returns falls or falsity. Okay so using similar examples as the one above if I have list 123 okay and I can say unless you know dot com Blessed his three weeks, yep the list was not half three parliament. So in this case as you might expect nothing will happen because it does actually have three elements. So if I put four elements and I type it the list does not have three elements. So it's the same as saying something like the above his equivalent to the wing if but then you have to invert the condition so this will be not equals three. Um Yeah, so it's honestly, you know, I mean um if and unless it's again um and they're they're implemented using macros, which is something we may learn about later in the series but they're really just a way to have a simple, convenient way to um Two do like a very simple single check kind of flow control in our applications. Um What's also interesting about if and else is that you could use it to assign variables. So for example, I could say something like this, we can use it to assign variables. I mean not just to assign variables. Just keep in mind that if whatever is in the block will be returned. So you could do something like this, I can say something like this. Um My word equals if dot count list. Okay, it is greater than three. Do the list is big else? Your list is small, so I could assign a variable conditionally using the if else or unless. Um this way, let's try it. So my word now, as you might guess the list is big because it has four elements, it's greater than three. Um We can also do it in a different syntax similar to the function where you put something like this, you and then Alice this will give you the same result as this one above. So you put the comma and you could do you put what you want if the condition is true and then in the else you put what the condition will be, if it is false? Yeah, in many other languages you would use the tannery operator. We don't have it in elixir, but this can, it would be the closest I would say to the tannery operator. Turn every operator, you may not be familiar with it, but you may have seen it before where saying javascript, you would be able to do something like this and then you put stolen and you do something like that. And many other languages, you were able to do something like that, son, at least in this current version of elixir is not available, but you are able to use this in line if analysis to accomplish the same thing. Um lastly this is not particularly related to if else, but you know it's you know, and and a good let's say a good diversion to kind of learn here, which is variable scoping, variable scoping. So mhm. Do it more as an exercise here or as a guessing game, let's call it because you haven't experienced this before and every language treats this differently. So if I have here X equals hello. And then let's let me bring my list back here. It's as if in um count greater than the list X equals villages, big X equals the list is small and then here I owe that puts what do you think X will be in this particular case, I mean as you might know like this condition will be the one that will execute. So will it be hello or will it be the list is big. What is your guest? Because it's actually hello because in elixir in this particular block, this variable is only scoped within this block, which means it's not really available outside of it. So keep that in mind because if you actually want to make use of this X right here and by the way this happens in many other contexts such as functions and whatnot. So this is why be careful when you're expecting to assign a variable that's, I should say re assign a variable that's been defined outside a particular block. You may want to follow a different approach in this case it probably makes sense if you actually want to assign the ex to do something, like I showed you before or just basically assign the variable outside knowing that the if will actually return either this one or this one. Okay, awesome. So this is what I wanted to cover for this lesson and I will see you in the next one
36 Views 0 Likes 0 Comments

We learn using `if` and `unless` to control the flow of code in Elixir.