hey everybody and welcome to the video on if statements this is chapter three section five so an if statement is just a conditional statement it's a very basic conditional statement but it's the type of thing we're going to see all of the time and we've already seen these basic if statements in our code and basically they determine whether or not we run a line of code so everything that we've seen so far is basically you know if this is true do this line of code if this is false don't do this line of code um so the very generic structure looks like this it's if condition is equal to true print run this code um so what i'm saying here is that a very generic if statement will actually go and check this particular condition here so the particular condition we see up there whatever that is and it says if this is true then it resolves the true and we do whatever's in line two and if it's false we skip line two simple as that now i would like to point out that we don't actually put condition is equal to true in our if statement there's no need to do that right what we're doing is we're actually taking a look at it and let's say for here we have x is less than 10. so we say x is less than 10. if that is true i do line six if that is false i don't do line six so i don't say if x less than 10 is equal to true i don't do that x less than 10 is going to resolve to be either true or false and whether or not we do line 6 will depend on whether or not that's true or whether or false and that's what we want to do there so the flowchart for that because we were talking a little bit about flowcharts last slide deck looks a little bit like this um i say if x is less than 10 if that is true then i follow this path i do this line of code and then i get down to the end of my program here what whereas if it's false then i just go straight down to the end of my program here either way i'm going to end up at the end of that program now in this particular very very simple if statement i had nothing more than setting the value of x my if statement and then that one print statement so there was nothing different there that i would do that's a very basic flowchart so basically do i do that line of code over here do i actually go ahead and and do this line of code or do i skip it right and the false would actually be just to skip that and to continue moving down so i want to talk a little bit now about indenting your code um because i've talked about indenting before and i talked about how in python indenting wants you to indent with four spaces um indenting is important because it specifies a block of code so what do i mean by a block of code well a block of code is a series of lines of code that should be treated as a group right that's what a block of code is so we can group code in blocks so that conditional statements know which code to run because an if statement doesn't necessarily just have to do the one line of code that's next it can do the block of lines of code that are next so with the conditional statement and previously we said if the statement's true we do the next line of code but what we really mean is is if the statement is true we do the next block of code so let's take a look at this particular example so in this example on line 5 we say if x is less than 10 and then on lines 6 and 7 that is treated as one block so if the statement on line 5 is true we do line 6 and seven if the statement on line five is false then we skip lines six and seven line eight is going to get run regardless because line eight is not part of the block so how do we determine a block well the block how we determine that is actually very very simple do you see this a colon here we start out with a colon and then we indent the four spaces so both of these lines are indented and as long as we continue the indent it's part of the block as soon as we break the indent and we go back to the indent being on the same level as the if as we see over there with the print on line eight then that's when the block is over right so the block is indented for and when the block is over the indentation goes back to what it was before the block started right so what it was before the block started in this case there was no indentation the if is right up against the side there it wasn't indented at all so the block gets indented four and once the block is over it goes back to no indentation um and there are cases where you're indenting in something that that is already indented and and so what that means is that your indented block goes four more spaces in and when your block is over you go four spaces back so in this example um if line five is true we do line six and seven um regardless line eight is going to get run and this is one of those cases where it would be a little bit clearer that the if statement was over if line 8 was blank and that line of code from line 8 was actually on line 9 it might make it more clear the end of the if statement but the reality is is that once your indentation finishes then you know your block is over so what does the flow chart look like for this one well the flow chart for this one is fairly simple we have a x is less than 10 if x is less than 10 is true then we come down here we do these two lines of code and then we continue down here and we do that block that is not part of the um that's not part of the do that last line of code that wasn't part of the block if it's false then we just skip right down to this block of code so this block of code is going to get run regardless that one line of code so that's what our flowchart looks like so how big can a code block be well it can be any size it can be one line it can be hundreds of lines it doesn't really make a difference to us as long as it's indented it's part of that block as long as we don't go back to the original indentation level then the block doesn't end but that leads next question can you actually nest blocks of code so can i have a block of code inside of another block of code and the answer that question is yes if you have another conditional statement or as we'll see later a loop or something like that then you can nest blocks of code inside of other blocks of code and here's a really quick example of that now if you don't look exactly what's written there and you just look at the indentation level then you can see that i have two levels of indentation here and i want to just i want you to stop and just look at that for a second um so this is my outside indentation level this is my second level and this is my third level so i have levels of indentation here right now to be fair i don't need to look at that first level of indentation until i come back to it so the fact that the print statement on line 17 is the same as the if statement on line 12. um since that's the farthest level of indentation out that doesn't concern me too much what does concern me is the level of indentation which we see on line 13 which is this particular column of indentation if you just want to imagine a line running up through there and then the one here for the print there's another line of indentation there so i'm drawing those lines on top of the code because i want you to visualize them and then i'm going to just take those out there so it doesn't bother you what we have happening in this particular case is actually quite once you realize what's happening it's not too difficult to understand the print statement i'm going to start in the middle here actually the print statement on line 15 that we see here is only run if line 14 is true right so that print statement on line 15 is only run if line 14 is true so we know that so that's one level of indentation that line 15. now the question is at the level in it of indentation of line 14 well where does that come from well that's at the same level as 13 and 16. right so these three lines of code at 13 14 and 16 are all going to be run if the line of code on line 12 is true so so that means that if line 12 is true then i run line 13 and then i run line 14. if line 14 is true i do 15 if line 14 is false i don't do 15. and then i do line 16. so that's what's really tying this whole thing together is that i have this sort of flow of my code now i can see just by looking at this that line 17 is not part of any of these blocks so line 17 is actually grouped with lines 11 and 12. so i run line 11 i run line 12. if line 12 is false i go right to 17. if line 12 is true i do 13 and 14. 14 is true i do 15 if 14 is not true i don't do 15 and then i do 16 i do 16 because it's at that same level that we see here as um it's at that same level as 13 and 14. and then i do 17 at the end so if 12 was true i go down that path and do 17 at the end this might be a little bit more clear with the flowchart so let's take a look at this with the flowchart so the flowchart is actually fairly simple here um let's follow the true cases because that is sort of the easy one so i say is x less than 10 well that's true so i follow that down to here and now i have another ch i print out that statement and then i come down to here and i say is x greater than zero well i have a choice here if it's true i go down here i run this line and then i'm coming right back into that same line of code right if x greater than 0 is false then i'm just going to drop down through to that still in the you know x less than 10 block right and the reason that that's happening is because this conditional statement that we're seeing over here so this conditional statement only has one line associated with it so if it's true we go out and do that line and come right back and if it's false we just continue on so that's what's happening there and then assuming regardless whether or not that was true or false i do this line over here and then i go to the end and i do the the final line of code now i could skip all of that and just say oh looks x less than 10 is false i'm going to go right down there and do this block at the end so that's my other option as well so this is basically about being able to look at the lines of code and being able to read what the flow looks like and the indentation actually really really helps here so if i go back up and i look at that code and let me just um delete all the stuff that i wrote on top of it which might make it a little bit more difficult to see i can see from my levels of indentation here i can see what's going on and the i will actually draw itself to this as we get used to coding so i know that if this statement on line 12 is true then i know that this code comes into play and if not i just drop it right down to that print statement so if my if is false i go right down to the print statement and i skip everything that's indented if the if is true then i need to look at the stuff that's indented and likewise i'm going to see the same thing uh here this if statement if this if statement's false i drop a rate to the print that's underneath it and i ignore everything let's indent it and if it's true i do the indented work so this indentation is actually going to be very very helpful to us we need that indentation in order to make the code readable and work so with an if statement you do the block of code if the condition's true otherwise you just jump to the end of the block which basically means you just drop yourself down you look at that that line of indentation if you want to look at it that way and you go to the next line of code that's indented the same at the same level as the if [Music] don't worry about nested blocks if they're in a block that you're skipping because if you skip the outer block you skip the inner block which means that you don't have to worry about it because you're just skipping all of that code and the block ends when you're no longer indenting at that level and when i and saying at that level is actually really important here because it doesn't mean no indentation at all so if you look at line 14 14 is an if statement and if the if is true i indent at line 15 and i could have a block there with a whole bunch of lines in it but i end the block from the if statement on line 14 as soon as i come back to the indentation level that's the same as line 14 which is what we see on line 16 right so it's not when the indentation stops it's when the indentation goes back to the level it was before the block started right and that's what goes on there now some people may be asking the question saying well what happens if there is no line 16 can i just go right back out to 17 yes you can end two blocks at the same time and skip right back out to the to the very very end uh documentation so deleting line 16 means that it wouldn't print out but it doesn't change the functionality of the program you can end two blocks at the same time so that's what's going on there so let's just skip ahead and let's talk about style for just a quick second there's a couple of things in terms of style that we should look at here people usually avoid a blank line directly after the beginning of an if statement so if you have an if statement your first line should be a proper line of code um you can put vertical space inside your code block that's okay um you can nest other conditional statements inside your block that's okay but keeping your indentation clean is the key to making your code readable if your indentation's not clean then your code will not be readable let's just go over here and take a look at a quick example of this in spider so you can see what it is that i'm talking about i'm going to say x is equal to 10 and y is equal to 20 here just to give me a couple of numbers to work with and then i'm going to start my if statement i'm going to say if x is equal to 10 and that's the beginning of a block so uh and then i'm i'm going to do the next i'm just going to say print x is equal to 10. so i'm just going to print that out on the screen and actually i'm going to print that out on the screen two or three times just that i have multiple lines there so when i say that you shouldn't put a space at the beginning of the block so what i'm saying is is that this is fine this layout is fine what i have there at the moment let's make that a little bit bigger in size that you can see that on the video right so this is fine you can do this but don't do this don't start a block and then put a blank space here like you see on line five that is just ugly and you shouldn't do that um it's okay to put spaces inside this will not break the block the block will still work perfectly fine if there's spacing inside so if there's something that you need to do in order to be able to to separate out your code then that's fine as well and put another conditional statement in there i i can say if y is equal to 20 and do that here and then i can print y is equal to 20 and i'll do that a couple of times just oh i guess that spacing that indentation did not work well so i can do that a couple of times in there just so you can see what's going on now in this particular case you can see my input it'll print out x x then i'll print out y y and then it prints out x x again and that's exactly what i expect so here this internal block of code from 789 right um seven is part of the big block from the first if right and eight nine only get run if line seven is true so this type of spacing um that we see could be adjusted i could do things like this and make it a little bit easier to see to make this if statement really really stand out if i want it to right so that's the type of thing i could do and it all depends on what style you're looking for for programming so this works fine so having if statements like this you can see how that indentation is really helping us here this is the example i talked about in the last set of slides can i end two if statements um can i end two if statements at the same point in time and the answer is yes i can so by what i have here you can see that i have this if statement and that's at the same level as this print and this if statement here which is indented 4 when this block ends it's going to drop back out to the block where the five and six words at that level but that block is now over because the indentation is now back out at the beginning and that code works perfectly fine too and that's the type of thing where i would separate it out because really this code on line 11 is not part of this if statement and this block of code up here it's actually separate right because it's going to run regardless has nothing to do with the if block so that's why i would put a space in here just to make things a little bit better one little key bit that i can tell you just that you know is if you hover in spider over here on the left you'll see these little down arrows and what those do is they actually collapse blocks of code for you and what they're intended to do is if you've written a function which i know we haven't talked about yet but we'll get to that soon or if you've written a piece of code and you're like here's my block of code my block of code works i don't need the my block of code to be in my way to see it all the time i can collapse that block of code just to hide it then you can just click the little button and that hides the block of code but this is a little bit of a trick too is that you can use that to see what your blocks of code actually look like right in your program what's included in your block of code and what isn't because as you click that button um certain blocks of code will disappear so you can see this print statement never ever disappears if i were to go put another print statement in over here i suppose i should spell print right if i were to print um here is another statement um then you can see that when i collapse that block of code for 7 that this statement on line 10 stays so this is a way to figure out what's in your block and what isn't so uh if what is on line four is true i'm gonna run lines five and six and seven and ten i know that whether or not i run set 8 and 9 are going to depend on what the answer to the conditional statement on line 7 is so that is what things look like when you're actually writing code and that's you can put in some vertical spacing to sort of separate out things like um things like the code on line 12 and you might also ask yourself the question well how many layers can i go deep in terms of nesting uh really five six seven you can go you can nest as much as you want although it is problematic because you end up getting lines of code that are too long and too ugly so if you're nesting that much you probably have bigger issues in your structural design of your code and that is the end of this particular slide deck thank you for watching