[MUSIC] [SOUND] The control flow statements are statements that alter the order in which statements are executed and which lines of code are executed. So we've seen these before in C. And these are common in every language. We're gonna look at the if statement. If then, if then else. For loops. And while loops. So let's start with the if statement. And this is pretty generic right here. The template you can see up above. If a condition is true, colon, you do some code. That indented code block, that's all the code that you want to do, you make sure it's indented right, so that the if statement, so the interpreter can tell that it's supposed to be executed when the condition is true. And then after the if statement is done, you have non-indented code. And whatever non-indented code you have that signifies the if statement is done. So, for instance, example. If temp is greater than 80 it'll print it is hot. There it'll print goodbye. So, even if the temp isn't greater than 80, it'll still print goodbye. Because the print goodbye is indented the same as the if which tells you, okay the if statement is finished. The print is not, the second print goodbye is not part of your statement. It ends the if statement over before it.. So the indentation, again, is key here. And, it is hot is printed if it's over 80. So this is an if statement, you've seen this type of thing before. If-else statements. This is a very similar thing, an if condition but you add an else. Now you have else: and you have another statement or a set of statements. So this case I'm using a single statement in each path. it temp is greater than 80 print hot. Else print not hot. Notice the print hot is indented, the print not hot is indented, the print hot is indented under the if. The print not hot is indented under the else. And then print goodbye is not indented so it means that the statement is done and it happens regardless of the condition. For loops. So these are inter loops, we've seen these before. The way, one difference about for loops, inside into Python is that these for loops, and y loops too, these loops iterate through sequences. Okay? Now, sequences typically, a string is a sequence, a sequence of characters, right? A list is a sequence, a sequence of items of any type. So a for loop or a while loop is gonna iterate through, sorry, not a while loop, just a for loop. A for loop iterates through some sequence. So in this case, we're using a string. Name equals Ian, I-A-N, three letters. It's a three letter string. It's a three letter sequence. So for char in name :print(char). So, it's gonna iterate every pass through. It'll go through three passes, right? Because there are three characters inside Ian. And each pass through, it'll take that variable for char in name. It'll take that char, and bind it to one character in the name. So the first pass-through char will be I. The next pass-through char will be a. The next pass-through it'll be n. And since it's printing char each time, what you get printed out is I and then a and then n. So that's how a for loop works. A for loop actually iterate through some sequence, which could be a string, or could be a list. There are a few other types of sequences, but those are the main ones that we're gonna focus on, strings and lists. In this case, a string, and notice that at each pass through the loop, it is binding the variable, car in this case, to an element, to one element inside the sequence. Another for example. So in this example, a similar thing. This time we've got a list of three names, three strings. Joy, Mary, Pete. So name in Joy, Mary, Pete, so every time you run through this, every pass through, name's gonna be one of these names. So the first time through, name's gonna be Joy, next time through it'll be Mary, next time through it'll be Pete. So if we print the names, it just prints, Jon, I'm sorry, I can't read. Jon, Mary, Pete. So it printed Jon, and then Mary, and then Pete. And any sequence, so in this case it's a list instead of a string as we had in the last slide, but strings and lists are both sequences. And all code in the loop needs to be indented. So in this case we just have one statement, the print statement that we've indented. But you could have as many lines of code as you want. So a while loop is different than a for loop, it doesn't iterate through a sequence. Instead a while loop just checks a condition at the top. It iterates until that condition is not true. So if we look at our while loop here, while i < 3. That means that it's going to execute all the statements in this loop until i is no longer less than 3. It'll keep doing that over and over and over again. If i is always less than 3, i never changes, then this would be an infinite loop, right? But in our loop, if we look at our loop, it contains two statements. First, print(i), next, i = i + 1. So it's always changing i, which is the way you'd want a loop, right? You want whatever the condition is to change periodically or else you'll never get out of the loop. So this loop, I noticed it before the while loop, I'd say i = 0. So I set i = 0, and then, a while i less than 3 it prints i and then increments i. And so what you're going to get printed out is a zero, one two and then it will end. Because as soon as I hits three, then when it gets to the top of the loop and checks is I less than three it'll say no that's false, it'll jump out of the while loop and that'll be the end of that. Thank you. [MUSIC]