 Welcome back to 105. Thank you for joining me today. So we're at like a quarter of the way through the course. Everything good so far. Any lingering questions, comments, concerns or anything like that or fairly smooth sailing, lab one okay, lab two going okay. Seems okay, that's good. So if you can too, I encourage you to also do the textbook exercises and all of that fun stuff because especially if you're new to this, best way to learn is just practicing. So today we get to talk about four loops. So luckily for us, they're not really anything new after learning about while loops. They're just used for a slightly different purpose and they're slightly easier to read other than that. They're pretty much exactly the same as while loops. So they usually represent bounded repetition. So something we know that will only repeat a certain number of times. So syntax for this looks a bit wordy. So it starts off with a four, so keyword four. So again, not allowed to name a variable four. And then instead of just a condition in the parentheses, we have a whole whack of stuff now and it looks complicated but really it's actually not. It's not any more complicated than a while loop. Just makes it easier for you to read some code, believe it or not. So the first thing you're allowed to put here is an initialization statement. So you can define a variable and initialize it to a value here. This will only run once and we'll see the flow of how this works on the next slide. And then in the while loop, all we had was a conditional expression. Well, in this case, our conditional expression for how many times to repeat this loop is going to be here in the middle. And then finally, there's another semicolon and then there's an increment expression. So that is when we're going to change our variable and it will run at the end of every iteration of the loop. Then here, of course, we have a statement. However, in this course and in C in general, you should always write it like for, we have the initialization, the conditional, the increment, and then we use curly brackets and have all of our statements in there. So every time through the loop, we execute all of the statements within the curly brackets. So our control flow, we have a lot more stuff but it's not really that much more complicated. So we have some code here that would be at the start and then I'll just call them the first one. I'll call init short for initialization condition, short for conditional expression because we're lazy. And then instead of increment, I'll just say ink because it's shorter and it gets the point across. Then here within the curly brackets and have some capital A, that's some code that I want to run every iteration of the loop and then I have an end. So the control flow for this or the flow for this loop is well, it will always execute the code at the start and then it will always execute this initialization statement and it will only do that once, right before it checks the condition. And then similar to a while loop, it will check the condition. If it was true, then it will go ahead and execute the statements and then unlike the while loop, we are allowed to tell it to do something at the end of every iteration so we don't forget something as the main point of this. So this increment will always run at the end of every iteration of the loop and then after that's done, it will go ahead and check the condition again. Here, we have to make sure that our condition will eventually be false or else we're in that fun infant loop that we cannot get out of except if we hit control C and then just get rid of everything. So eventually whenever we hit false, then we just go to the end exactly like the while loop. So just some terminology in case, hopefully I've defined everything that I've used at least within a few slides. So here you might hear programmers talk about iterations. So that's just a shorthanded way of saying a single repetition of a loop. We call that an iteration. So also, if you wanna think of it in another word, it's just a single step. So single time through the loop. So some fun rules about this because well, you actually don't have to put in the initialization or the increment expression if you don't really want. So you can write for semicolon and then your conditional here which I'll just do a lowercase a and then another semicolon and an a. However, if you write this, so you get rid of the extra functionality of the for loop and it just becomes a while loop. So this is exactly the same as writing while a and then just a curly brackets with some code in it. So for these, yeah, you can express the same thing but for bounded repetition, you should write a for. So just don't write a for with no conditions in it. It's weird. Just use a while loop if you don't know how many times you're going to iterate through. Everyone agree? Great. So let's write a program now to count from zero to nine. So we can do this with a while loop. So here, let's just do it for fun. So anyone tell me how I would print out the numbers zero through nine all on different lines using our good old friend a while loop. Yeah, so create a variable called count, initialize it to zero with the new one. All right, oops. And did I forget anything? And account like that. Yeah, so count equals count plus one. Now I'm good, right? All right. Any disagreements with what we have currently written? Up there, yeah. Yeah, so there's a few different things we could change about this just depending on style but it all means the same thing. So if we run count now, what we get is zero, one, two, da, da, da, da, da, exactly what we expect here. So style things like mentioned, if I wanted to, I could write here instead of count equals count plus one. I had a few options. I could write minus, minus count. I could write count plus equals to one. This probably when you're beginning is probably easier to read and understand what's going on. So you might prefer just to do this. I could also change the condition if I wanted for the loop. In this case, it doesn't really matter. I could also do just less than 10 if I wanted to get rid of the equal sign. Right now, this is just a matter of preference but later on you'll find that just having less than the number is going to be slightly easier for reasons we'll get into later but they mean the same thing. So if I compile this and run it, still it goes all the way up to nine. So this can get a bit tricky because well, if I want to argue that my loop will actually terminate, so I know it's going to run like 10 times but I have to keep track of a lot of things if I read this. See, okay, what did I set the first value to? Okay, my condition is here, okay, great. And then, oh, did I make sure that eventually this condition will be false? So am I doing something to change the value of count such that it will go ahead and eventually be false? Oh, okay, yeah, I'm doing that. It's right at the bottom here. So I needed to read a lot of places and then eventually your code might be long and then it looks somewhat thing like this and you're like, oh, okay, not sure if that's right because I can't see the end and then by the time you get here, you might forget this line and then forget what you were doing above and then, oh no, suddenly you're in that infinite loop situation. So for loops, the primary reason to have them is to put all of those count variable checks, increments and initializations right beside each other. So instead of writing this, I could instead write it as a for loop. So I could say four in count equals zero, semi colon and now I would do the same check. I would check that count is less than 10 and then usually for for loops, we just want them as small as possible. So we might actually prefer to do plus plus count here and then just looking at a single line here, we can argue about how many times this loop goes through. So just by looking at the single line, we know it starts at zero and then it will always be less than 10 and we increase it by one every time. So it should go from zero all the way up to nine and then it looks a bit nicer because we can just have our print just nicely sitting on a separate line. So instead of having all those lines of code, I can just do it in a single for loop. So if I run it that, I get exactly the same results. So questions about that, yep, yep, yep. So the question is, well, now can I not use count outside of the for loop? So we'll go into the rules that you can use variables in one or two lectures from now. But yeah, the difference is, if I try to use count now, my compiler will yell at me. So my compiler will say, use of undeclared identifier count because it doesn't exist anymore. Yep, yep, yep, yep. We'll go over those rules a bit later. But if I wanted count to exist outside of the loop, this is also a good reason to use a for loop because generally I'll only use count inside the while loop and or inside the for loop. And I don't wanna accidentally use it outside the for loop because while I might not remember what the last value of it is, but if I wanted to, I could do something like this. So I could do in count and then this initialization statement that just runs one time before we even get into the loop. So I could just write something like this. It would do the same thing. Yep, yeah, so while loops, they don't have this initialization part and the increment part. While loops just have this condition and then you can write whatever you want. So this just kind of shoves it all into one thing so you can just, the idea is we put all of our count things right beside each other instead of having them on separate lines. Usually it's a bit easier to read. This will probably be your favorite loop if people have favorite loops, it might be kind of weird. All right, questions about that? All right, so we can just do a bunch of problems today because that's all about practice. So here's a solution to that so you have it on the slides. So let's write a program that just prints 15 stars on a line. So I could just write 15 stars on a line with a print F but I don't really wanna count how many times I hit the star key. So if I wanted to write that program, let's see, any suggestions with what I should write? If I want to be lazy and I just want to see 15 stars on a single line. Anyone help me? I have a C amnesia. It's a rare condition. Yeah, just do a for loop that goes 15 times and print a star each time. So if I'm doing a for loop, well, I need a variable in order to increment. Let's call it, let's just call it count for fun. What should the first value of count be? Zero, we like zero. Okay, and then what should my condition to keep on going through this loop be? Yeah, count less than 15 and then each time through, well, typically for all the loops we'll either do, we'll just do plus plus count or whatever our variable is, we'll generally just increase it by one. So now this loop will go through this 15 times. So we'll go zero. Next iteration of the loop, if we want to use our fun new terminology, count will be one, then count will be two and then it will go through a few more times until eventually count is 14. And then as soon as we increment, or as soon as count is 14, at the end of that iteration of the loop, we do plus plus count. So count would now be equal to 15 and 15 is not less than 15. So we would exit the loop and then just exit our program. So we would stop at 14. So we could do this some other ways. If you're starting, well, it might be easier to read. You might prefer this. Start count as one and go until it is equal to 15. So then it's like one, one, two, three, four, five, six, seven, all the way up to 15. Turns out that's okay to begin with but turns out later in the course, well, computers actually really like counting from zero, not one. So if you want to get into the habit, while there's not that much else to think about, just always start your loops with the count equal to zero. In this case, if I want to just know that I go 15 times, just go less than 15. So I'll go zero all the way up to 14. So in this case, if I want to print out 15 stars, I know I iterate 15 times through this loop. So each time through it, I can print a star and then we should always be nice and print a new line whenever we're at the end of the line. So if I compile and run that, oops, I'll print stars, stars, then I get 15 stars in a row. I didn't have to count or anything like that because while as you get used to programming, you're gonna be lazy and it's gonna be great because you'll no longer need to count higher than about 10. Ideally, you don't have to count higher than like seven ever. So yeah, it's great being a programmer because you can even forget how to count. It's pretty nice. All right, any other questions? Yeah. Oh, if I do this. Okay, so I can write something like this. So if I compile my compiler, doesn't really care. And then if I do this, I'll see, I assume that's 15 stars. Let's measure. Nope, we're slightly off. So in this case, we're slightly off because in this rare condition, we probably actually mean post increment or we should just change the bounds. But doing something like this, having a change happen while you're doing a comparison, generally that's a bad idea and you'll probably be wrong. You'll be off by one and it'll look weird because there'll be nothing here. So generally, you should actually just do something like that and just have your condition, just do a plain old conditional check. Yeah. Yeah, so it would go ahead and here. So we always do the start, then we just do the initialization once and then condition exactly like if it was a while. Yeah. Yeah. Yeah, so the reason we saw that bug is because while the condition happened once and we incremented it from zero to one and then we checked the condition and then we went through. So we messed it up. Moral of the story is just generally mixing plus plus on a line where you're checking a condition. Bad idea, especially if you are beginning. So do yourself a favor and don't do that. All right, any other questions or weird things we can do? So there are some weird things you can do that I have seen on exams for some reason. They have asked this, I don't know why, but turns out, so this line actually, whatever we write here, always executes at the end of the iteration of the loop and in C for some ungodly reason, you can use a semicolon to kind of chain two things together to make it a single expression. So I could write print F star and then a comma and do this. So now it will still follow all of the rules of the for loop. So this will happen first, then it will check this condition and then, well, it would do whatever's in the loop. Right now there's absolutely nothing there and then right at the end it would do a print F and then the count because we chain them all together. If I compile and run it, I get 15 stars. I've seen this on your exams. Please don't write things like this. This is really, really confusing. I will might argue if it comes up on your exam because I have never seen this before in any real program I have written or read another human wrote. So yeah. If you do that, alarm bells will go off and I will be disappointed. So don't disappoint me. All right, so here was that rules. You could but shouldn't underline the shouldn't use a comma to like chain together multiple expressions together to make it a single one and shove it all in that increment expression to make it do two things. Generally it should do one thing and one thing only that increment expression should increment a variable. So maybe you don't want to increase it by one every time. That's cool. You can increase it by two if you want, go for it. So like, yeah, we'll see some examples at the end but yeah, don't do like a print F as part of that. That's just weird. All right, other things that are highly discouraged in this course but are actually I find useful but we won't go into them. Is there are two other keywords to give you more control over how your loops operate. So they are called continue and break. So they themselves are keywords so you cannot name your variables break or continue. Bad luck for you and they help you control, give you more control over what happens with the loop. So if you write a break statement in the body of your loop, well that causes essentially like the go to jail card and monopoly. So it will immediately kick you out to the end of the loop and it will stop iterating your loop. So essentially if you do a break within one of these statements that we execute in the loop, we will go straight to jail and go straight to the end without checking the condition again, without incrementing, without doing anything like that. Sometimes that is very useful, probably not in this course. There's also a statement called continue and what continue will do is it will just stop the current iteration of the loop and then immediately restart it. So if I go back to the diagram, if within a I hit continue, it encountered the keyword statement continue, what it would do is immediately not execute the rest of the statements in the curly brackets, do the increment and then do the condition. So it'll essentially just skip the rest of the statements that you have in your loop. Sometimes that will be useful in this course since you're beginning doing these advanced loop things, highly discouraged, I guess depends on your problem, like any tool, it depends on what you use it for. So good to know these things. Do you have to use them in this course? Probably not. And in fact, coordinator encourages against it, but who am I to tell you what to do? So I don't want you to have to read C code and see one of these and be like, well, I don't even know what the hell that means. So I told you. All right, so now we can do something more advanced. So we can write a program to print a triangle of stars. So now we can break this down. This seems to have two types of iterations going on, which might be a bit confusing at first. So two things are going on. So I have some bounded repetition here where, well, I'm printing off, in this case, there are five rows, remember, I don't have to count higher than like a seven, so I'm good. And then for each row, I am printing just the number of stars that matches that row number. So a lot of programming is just being able to detect patterns and kind of break it down and explain it to a very stupid machine. So your computer, very, very stupid machine, if you want it to print off these stars, you have to tell it exactly what to do. So if I were to break this down, so we want to print something that looks kind of like a star, two stars, three stars, four stars, five stars. So if we go with our pattern, you might want to write out that, hey, you know, this is since I told you computers like starting at zero while humans like starting at one. So I might say, hey, row one, that has one star. So that kind of makes sense. Row two, well, that's got two stars, oops. Row three has three stars. Row four has four stars and you probably get the idea by now. Row five has five stars. So if I wanted to write a program to do all this, oh no, amnesia, help, I need help. Oh, so just copy, paste this for loop. All right, so best programmers does copy, paste stuff. So it's more true than you think. It's not really a joke. So inside of which one? No, yeah, it's a statement. So print a new line here. Well, just right after, yeah, right after. Right after? No, I mean like within the body of the loop. Within the body of the loop, okay. So we start this at one. So yeah, here maybe I want to rename the variable count because this seems to be representing like the row kind of, right? So yeah, maybe I just rename it, do a good old find and replace. So I rename it to row. So now, this for loop will iterate five times. First time row will be equal to one, then two, then three, up to five. So if I look at this, okay, now I have my five rows. So what do I want to do each time I'm through a row? So stars, yeah? Yep, yeah. Yeah, I want to print off that many stars. So what do I need? Okay, okay. So what should I call this variable in this loop? Column, column, stars, preferences, column or stars? Doesn't, stars, sure. All right, so stars, starts off at one. What's my condition here? How many stars do I want to go until? Yeah, yeah. So I want to print the number of stars that are equal to the row number. So here I have this number. So row is equal to one, then two. So I should print off how many stars in the row. So I just do the same thing again. So int stars less than or equal to one, my condition, I would go up until row instead of just a predetermined number. So instead of going up to five every single time, well, each time through this loop, I will have another loop that iterates up to row number of times. So then I do plus plus stars. So now the first time through, row will be equal equal equal to one and then we would execute this for loop and then in this case, this for loop would happen one time and inside of the loop, I should just print off a star. Should I move this new line inside there too? No, because I want just, I essentially want a new line every time I'm done printing all of the stars, not just one. So in this case, if row is one, this will go ahead and only happen one time. So I'll only see one star and then that will be the end of row one. So I should have a new line at the end of it. Then I would go through the loop again. I would increment row from one to two and now two is still less than or equal to five. So row, this iteration of the loop, big words. Now row is equal to two and remember, we have to do equal equal and C just to make it a Boolean. Otherwise, it's assignment. So now, this time through the loop, this for loop will execute two times. So I will get two stars and then have a new line to say I'm done with that row and then similarly we'll go through, row will be equal to three. We'll go ahead, print three stars four. So now I execute this, hopefully I get that fun triangle pyramid, right? So let's compile that and run that build print star triangle. Hey, cool, a triangle. Neat. I like computers. So nice thing about this is if I wanted to print another row, well, if I wanted to do that manually, it kind of sucks. I have to type six stars and then another row I'll have to type seven and then eight and then I came and count up to eight. So now, instead of doing all that, I just change this to 10 and now I get 10 rows. So if I compile and run that, cool. I get a bigger triangle pyramid thingy. Cool. So questions about that. So like we had nested if statements, we can have nested for statements and inside here, I can put ifs in there if I really wanted to and all sorts of fun stuff. Questions about this or how this works. All right, to end off the day, so let's do something a bit more tricky and a bit more slightly practical. So let's say I want to print off rows and columns of something and I wanna do it in a way that a computer will like it slightly more and we'll see why it's kind of a better thing to start at zero. So say I wanna print like a 10 by 10 grid of something that goes from zero all the way to 99. So that would be 100 and I have 10 things per, like 10 columns per row. So I might do something like int row equals zero and say I want 10. So row is less than 10 and then plus plus row. Now, if I want to do columns, well, that's just a nested for loop. So instead of row, let's be a wizard. So now I just have an inner for loop that does the same thing for a new variable. So now what will happen here is first time through this loop, row will be equal to zero and then while row is equal to zero, this loop will go through column equals zero all the way up to nine and then finally be done with that for loop because nine, 10 is not less than 10. Then we'll go through it again, row will be one and then this thing will happen 10 times. Same thing, eventually we're gonna have 100 times. So let's say I wanted just a nice grid of numbers. So we can start off and just kind of guess at something and see how it looks. So if I want to print off like the actual numbers, I want them all in a row, so zero all the way up. Well, I might start guessing by just doing column and then let's see, print F and then just do a new line at the end of each row. So if I compile and run that, that does this 10 times, which looks kind of right. So if I wanted to go all the way up to 99, that's what it would look like for like the last digit of every column and this kind of looks like that mod thing, right, so how would I change this so that every time through the loop I'm printing in order zero all the way up to 99? Any guesses? Yeah? Well, I didn't say from the point you wanted to start. Yeah, so if I wanted to convert it all the way up to a whole number, well, this column kind of looks like if we did like that number mod 10. So if we did that number mod 10, it was positive, we'd always get zero through nine. So also represents like the last digit. So if I wanted to, that essentially represents my ones column. So if we went back to good old elementary school, the row actually kind of represents the tens column. So if I did row times 10, well, first time through the loop, which is why it's useful to start off by zero, then I'll have zero times 10 and I'll just get the plain old numbers. So I'll get zero, one, two, all the way up to nine. Then second iteration through the loop row is going to be equal to one. So I would get one times 10 and then first it would start off at plus zero. So that would just be 10, then plus one, 11 and go through all of the digits that start with a one. So if I do this, it probably will look a bit ugly, but it's kind of close to where we got it. If we want it to look like a nice grid, we might wanna normalize this thing and just always print two characters. So if we remember our format specifier, we can do something like a two ahead of it. So we always have two characters printed. So if we do that, doesn't help us that much until the first row and hey, at least every row looks the same. So now we have the problem of we might wanna pretty this up and have a space between all of them. So this is some common issue you'll have because what will happen if, oh, and first let's make it, I'll continue. So what would happen if I just put a space between or before every single character? Would it look like the prettiest thing I've ever seen? Probably look a bit weird. So if I do that, it kind of looks like a nice grid, except I'm very, very pedantic and there's technically a space there and I don't want one. So if you were to follow my rules, how do I get rid of that space? All right, so there's the cheating solution that you just do this, right? So if we just do this, then it looks pretty good. It starts at the beginning, but I have laser eyes. I know that's a space. So how would I get rid of that? Because I can write a program that checks if there's a space or not at the end of there. And I can make it angry at you. Yeah, so we probably want an if condition because we like, so here we have 10 things, right? So I have 10 things. How many spaces do I want? Yeah, I want nine. So I want one less so that it kind of depends. If something kind of depends when we're programming, it means we should probably have an if statement. So here, just to demonstrate, there's more than one solution. So I can either choose to conditionally put a space at the beginning or put a space at the end. Which one shall we prefer today? End, beginning? End, we got an end, okay. So if I wanted to conditionally put a space at the end, we can think about all the conditions. So if something, then we want to put a space at the end because this is after our print statement, it will happen after our number. So what numbers do we want to print a space after? So we could go through and even ask ourselves, do I want to print a space after zero? Probably, we're column zero. Do I want to print a space after column one? Yes, okay, let's skip a few steps. Do I want to print a space after column eight? Yeah, do I want to print a space after column nine? Absolutely not. So in that case, well now we know our condition. So if it's nine, then we don't want to do it. So if we're on our last column, so we could just say if column is equal or is not equal to nine, then we go ahead and we just print a space because we can think about when do we want to print a space? Well, whenever the column's not equal to nine. So if I do that, hey, that looks a lot better even though I can't really see it, but trust me, it works. But there's more than one solution. I could also just think about what happens when if I want to put a space before something. So it turns out this is a bit easier to think about because well, if I look at essentially column zero, do I want to put a space before column zero? No, so, and that's the only time, right? So I only don't want to put a space. If we think about the inverse of that, I only don't want to put a space whenever the column's not equal to zero. So as long as the column is not zero, I want to put a space before it. So if I build this and run that, hey, that's the other solution that does the exact same thing. Maybe I want to pretty this up with the leading zero just to make it look a bit nicer. So now we have a giant fun grid. Looks pretty sweet to me. You might also prefer doing this zero because even if I were to make the column larger for some reason, well, before when I had this, I just didn't want to print column nine. Well, say I wanted to make the column bigger for whatever reason, I want it to go up to, like spinal tap, I want it to go up to 11. So if I want it to go up to 11, then I have to remember to change this to 11 and then change this to 11. And then I would also have to remember to change that if statement from a nine to a 10 because I made it bigger. But if I have it at a zero, don't really have to change it. So you might actually, I would consider doing it before to be slightly better. But again, it's programming. There's not just one solution, it's kind of up to you. So now if I print this, looks a bit ugly because I get up to three digits. So let's fix that. So now goes all the way up from zero, prints 11 numbers a line and goes all the way up to 109, looks pretty, didn't really have to change anything. So any questions about that? I was just kind of screwing around just to show you that, hey, programming more than one solution. All right, all good. All right, with that, just remember, I'm pulling for you. We're all in this together and have a good weekend.