Transcript for:
Understanding If-Else Statements in Programming

hi in this video we're gonna look at if-else statements last time we introduced the if statement if some condition is true then we execute some code this is what an if statement looks like you might say if the front is clear then we'll move let's introduce if-else statements we say if some condition is true then we execute some code if that condition is true otherwise we have some different code that we might want to run there are two options here there's one thing that says we can run this if the condition is true and one thing that says we run this code otherwise let's look at an example you can say is the front clear then you'll move otherwise else we will turn left but why might you want this if statements and if-else statements like Carol handle different types of worlds we went from giving Carol and very specific commands and solving only one problem it's now being able to solve more general problems with if statements and if-else statements so let's take a look at some of these examples in the editor alright so let's take a look at an if-else statement so remember our original program we say okay let's move then move again and when we ran this Carol crashes into the wall so we said okay well let's do an if statement so we're going to say if front is clear then we said we wanted to move okay when we reset our code and run Carol didn't crash and then we said okay well let's turn that well maybe we don't want to turn left if the front was clear but we want to turn a left if the front is not clear so this is where we can use our if statement in our else statement so we're going to now say else so if the front is not clear then we're gonna turn left so now we'll notice that this code executes only when our front is not clear so if we run this remove fronts not clear notice how the editor skips over that move statement and it goes right to this turn left statement again take a look at how it skips over the move and goes to that turn left okay so let's look at another example alright so this program what we're trying to do is put one ball on each spot so we could solve this problem pretty easy we can say a football move move move and then football okay so we run this you see Carol goes and we have a ball there great so we've solved the problem well if we switch to another world and try resetting our code and running that we're gonna see we have a problem here it doesn't quite solve our problem now because now we have two balls on the first spot two balls in the last spot and nothing in the middle so this is where we can start to look at putting some different conditions down so what we're going to do is we're going to make a function called check ball so we're going to say define check ball and we're gonna say if no balls president then we want to put a ball okay so now we can change this here to say check ball and then we're going to move and we're gonna repeat that so we're gonna copy this and repeat that several times so now we're going to check and we're going to see okay if there's a ball there up we have one last move too far there that's get rid of that great so I solves this world let's go back to this still work on our first world yeah so we solved that one and we have one more world we can try so now notice that we've written our code we've made it a little bit more generic using this if statement and that's allowed us to solve the problem for multiple worlds using the same bit of code so now it's your turn to play around