 Alrighty, welcome back to 105. Thank you for joining me today. So before we went over if statements, we figured out how to jump forward in our code. Well, today we're going to complete our jumping routine and we're going to be able to go backwards in addition to forwards. So today we get to talk about while loops. So while loops allow us to have some repetition. So, like I said previously, if statements, we were allowed to jump forward whenever we executed our program and conditionally execute some code. Well, while statements kind of the same idea, but they allow us to jump backwards and then repeat the same operation again. So now we get to explore what makes computers great. They're fairly dumb, but they can do the same thing over and over again, really, really fast. So how we introduce repetition is, well, name of the lecture, a while loop, which is done by writing a while statement. So the syntax, which again just means the C rules for actually writing such a thing in the programming language is the following. So we start with the keyword while, so that special reserved word by C means we can't use it for a variable or anything like that because it's meant for this while loop. So we write the word while and then we have a parentheses and then inside of that we have an expression that will, again, similar to an if statement is supposed to be a boolean, so it should evaluate either to true or false. And then after that we have a statement. So we could write it like this, but similarly to if statements, we should always write it using the curly brackets so we don't make a mistake that costs billions of dollars. So we should do while expression and that will be a boolean and then some code here within the curly brackets that will possibly repeat over and over again. So the actual flow of this to compare it to our if statement is if we have a start here and a while and then some expression A which should either be true or false. Then within the curly brackets we have some code here so I'll just denote it by the comment A and then at the end of the while statement I'll just have a comment that says the end which would be replaced with whatever the actual program is you have written. So the flow of this is whatever code is right above the while statement will always run and then the next thing that will happen whenever we run this program is we will evaluate that expression so C will compute what the result of this expression is so if it's A it will either be true or false. If it's true it will actually run all of the statements that are in this case at the place capital A and then after it's done running all those statements however many we put there it will go back and then check the value of A again and now this is where we have repetition because if it is true again well we will run that code that we have at capital A again and we will keep on doing this loop while A is true which is while it's called a while loop so we keep on doing something while A condition is true and the only way to stop it to get off this crazy ride is A has to evaluate to false and then as soon as it evaluates whoops to false it just skips right to the end just like what happens with if statements but in this case unlike an if statement we will constantly check that expression over and over again and keep repeating all of the statements every time it is true and then only once it has changed to false will we finally stop. So now using this we can write some more interesting programs for your definition of interesting so we can write a program that just does a simple little countdown from 10 and then finally print off blast off so this program would have no input and our output should just be these in this case 11 lines so I should say countdown 10, nine, eight, seven, doo, doo, doo, doo, doo, doo all the way to one and blast off so if I wanted to I could write 11 print F statements that is something you can do but as programmers we want to be as lazy as possible so if I look at this I see a lot of repetition here so countdown repeats every single time and I seem to be printing the countdown every time the value is above in this case zero and every time I print it I seem to just decrease its value by one so if we wanted to write this using a while loop instead well first our first thing we see is a countdown for 10 so we'll create a variable called count that is what we're keeping track of as a condition in our while loop that we're gonna write so we'll start it with 10 because initially if we want to print off a countdown well our first line would look like this so we would do countdown D with the line and count so if we just run that well what we're gonna get is countdown 10 and then blast off because while we just have these two print F lines we don't have a while loop we don't have if conditions so we would start executing from main create a variable called count initialize it to 10 print off countdown with the count which is currently 10 and then print blast off so this is starting to look like what we want but not quite there so what we want to repeat is essentially this countdown so we will start off a while loop and just write an empty condition while we think about it but inside the curly brackets is the line I would like to repeat so in this case if I run it well if I just put like while one well this is something bad this is called an infinite loop so if I go ahead and run this I will see something bad so I'll just see 10 over and over and over and over again as long as I want to keep my computer doing this and to stop it I have to hit control C or something like that so this is called an infinite loop because that condition one is always true and because that is always true I will essentially take this loop forever and ever and ever and I will have no way to get off this crazy ride so if I'm thinking about the condition that I want here well I only want to print a count if it is in this case let's just say it's greater than zero so this also doesn't help me because I still have an infinite loop here because count is 10 so 10 is greater than zero so it would print countdown 10 and then start over and over again so whenever you write a condition here you never, at least in this course you never want an infinite loop so inside of the body of the loop you should be able to make a change to the condition such that it will eventually be false so in this case well I want the count to go down by one each time so if we remember how to do that while there's a bunch of different ways I could write a count minus equal one if I want so that will take the current value of count subtract one from it and then reassign it to count or I could do something like minus minus count we'll do the same thing it'll just decrease it by one so now if I write it like this every time through the loop I decrease the count by one so I'll get as a result I'll see countdown 10, then 9, 8, 7 and because I'm counting down eventually this condition will be false and as soon as it is false it's representing a count I do not want to print so if count is one then one is greater than zero then it would print down countdown one and then we would decrease it from one to a zero and now zero is not greater than zero so it would stop this so this would then be false and we would skip to the end so we would not see countdown zero so now if we run this we'll see that hey we were lazy we only wrote one countdown printf statement and we got countdown 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 all the way to blast off so the output doesn't look quite what we want because I had leading zeros there and in one of your labs they're asking you to do leading zeros so if you look up that format specifier information you are allowed to say hey how many characters should this digit print so if I do something like two in front of the D that means that it will always be two characters so if I compile and run that there's now an additional space here compared to what I had before so it will always be at least two characters and if I want to just have a leading zero and fill that in you don't have to write an if statement or anything like that in the format specifier they will do that for you you can just write a zero at the beginning so zero two always makes it two characters and we'll put in leading zeros yep question does it work for doubles too and yes yeah it works for doubles leading zeros yep leading zeros yeah it should work for doubles as well but see now if I print that I get 10 zero nine zero eight do do do do seven do do do do do do all the way down so cool loops seem to be useful they save us from writing a lot of code if we can just extract out whatever we need to repeat and then make sure that whatever condition we write here will eventually be false when we want to stop repeating something that any questions nope okay all right so solution as always in the slides so you have it so like I said before beware of the infinite loops so the statements will run every time eventually that expression needs to be false otherwise your program never stops and this is a large source of problems you will have whenever you are starting programming you will end up accidentally writing an infinite loop at some time if this happens as long as you're on Linux or you're connected to the ECF machine if you get in this situation where it won't stop hit control C and then it stops all right so other program we might want to write is writing a program to print the number of digits in an integer so our input would just be some positive number and well as some examples if we input the integer one that's one digit if we enter 10 well that's two digits if we enter one, two, three or 123 that is three digits and if we enter this giant number well that has five digits so we can also solve that so here is our program set up for that one so here I just create an integer called input prompt the user to input a positive integer I'll do my good old handy scan F and then I'll create a variable that's supposed to count the number of digits so if I wanted to solve this using a while loop well what I might think of is how do I decrease the number of digits by one well turns out we can use integer division as our friend so if my input is something like five and I do I don't know input divide equals 10 so I just do the integer division because it's integer division that just truncates that essentially will get rid of a digit the lowest digit every single time so if I do five divided by 10 well the result of that is going to be zero and that means I have no other digits if I have something like I don't know 23 well if I do 23 divided by 10 well because we are in C integer division the result should be like 2.3 but because it's C and it just truncates we're essentially going to chop off the last digit no matter what it is so my result would be two so I could keep on doing the same thing until I hit a zero so if I take two and divide by 10 well that result is a zero and there's a bit of a pattern here so I can just keep on taking that number each time I divide it by 10 I get rid of a digit so if I want to figure out how many digits the number has I can just count how many times I do that until that number becomes zero so that sounds an awfully lot like a while loop so I could do while the input does not equal zero then just chop off a digit and each time I chop off a digit well I want to record that I actually chop off a digit which represents you know just increasing the number of digits by one so if I do something like that and I run it so input a positive integer if I say something like one well I get one digit which is what I expect so my input would be one so currently it's not equal to zero then I would take the input and then assign it to the input divide 10 aka I chop off a digit so if it's five or one in this case then the result will be a zero and I would have I have to account for that digit so I increase my num digits variable by one and then we would recheck that condition again this case input is equal to zero so it would skip right to the end and then just print off the number of digits now if my input was like one two three I get three digits because what will happen is the input is currently 123 which is not equal to zero input will chop off a digit so we'll change input to 12 and then we would now have one digit go here input is not equal to zero then we change input equal to one num digits is two check this condition again it's not zero so we take one divide 10 that's zero now we have three digits and input is zero yeah so the question is why am I using the prefix increment here so in this case it doesn't matter for weird rules you should just always use the prefix and not the post fix because like the result of this expression is the updated value so it would be one in this case if you use the post fix it would be zero which is just weird so there's a bunch of other weird reasons to prefer this but yeah always prefer the post fix or prefix prefix all right almost screwed up myself but yeah always plus plus something yep will I care on the exam? No no yeah in the case of exams or anything like that write whatever you want as long as you're not using the result of it which you shouldn't be anyways so as long as your program's still right you're all good all right any other questions for this one yep so the disadvantage of using the post fix version is the return value is just not what you expect so if I have num digits equal to zero here and I do num digits here let's say so if I write something like this what's the value of x? so you said one that's what you would expect well if we actually look at the value of x what would make sense is that it would be one but if we run it it's zero that's why yeah if I did prefix then it would be one so if I do this now it's one which is more what you expect so in English might make more sense to do post fix but in C as a computer programmer I can freely admit this I can barely speak English so speak computer not English things work better so just prefix is usually what you expect yep all right any other questions or things we need to go over all right cool so solution on the sides so there is another version of the wow loop called the do wow loop so the difference between them is sometimes we want to always run code and then only repeat it if the expression is true so they came up with something called a do wow loop so do is also a keyword in C so you're not allowed to name a variable it and the syntax or rules for it is do statement while some expression so we just put the expression at the end because we check it at the end so can write it like that but you should always write it like do curly brackets some statements and curly brackets while expression so if I was to represent it using that same flow diagram I had for the plain while loop it would look like this so some starting statements that would run before we try and go into this do wow loop and then we would have dual do then between the curly brackets some other statements here in this case capital A and then while some condition here that would be some Boolean lowercase a and then an end so what will happen is whenever we run this it will run the code right above it and then it will go ahead and then directly start running the code in a so it won't check the condition this code will always run at least once and then only after it is done running then we check the condition so if the condition is true then we just go back to the start and we execute all of the statements in the curly brackets again and then we check the expression see if it is true if it's true we go over and over again so this also has the opportunity to put us in an infinite loop so same rules apply we should make sure that the condition eventually becomes false whenever we want it to stop so when it is false we just skip directly to the end and why might we do that well currently at least in that last example I just asked the user to input a positive integer and if they didn't input one I couldn't do anything about it but now because we have a do while loop well we can actually do something about it so we can write a program to ensure that the integer is in fact positive so the input to this program should be any valid integer that fits in an int so it could be negative zero some large value doesn't matter and then we want to ensure that we always have a positive integer no matter what the user tries to type and in this situation a do while loop is going to be useful because well we always want to ask the user for input at least once so if the user is nice and actually inputs a number like a positive number then we're all good otherwise if the user is trying to be funny or try and screw with us then we might have to ask until they finally learn to behave so if we were to write that well we'll just create a variable for the input and then well we'll do the same thing we did before we need to ask the user for input so in this case I always want to ask or prompt the user for input and allow them to input something so I will put that in my do so I'll ask for a positive integer and then allow them to input it and store it in the variable called input and that is all I have to do I just ask a user for input and then as part of the condition I'll just ask them as many times as I need to so my condition would just be while the input is less than or equal to zero or whatever I want so if this is true so if the number they input it is zero or less then I'm just going to repeat I'm going to go, hey, excuse me can you try again please because yeah, you're not having a good day and I'll do this as many times as the user tries and then at the end here in this print F I'll only break out of this do while loop whenever the output or input is greater than zero so whenever I print this it will always be some positive number that is my num digits, whoops alright so now I can run my positive input program so now it says input a positive number well I can be a jerk negative 47 well now that condition is false, I inputted a number that is less than or equal to zero so it's just going to ask me again and negative one oh no, it asked me again negative 23 ask me again and I can't outsmart my computer it is a lot more stubborn than I am so it's going to outlast me so eventually I'll have to input a number that is greater than zero so I can try and input zero still doesn't like me now if I finally input six well then I made that condition false and I know my input is greater than zero so I can stop doing that repetition I can stop asking the user because they finally finally actually obeyed my rules and then I print off you entered whatever they entered in this case six so yep so the question is what happens in scanf if you overflow the input I believe it will just try and yeah some overflow rules will happen and you'll essentially for this course you'll get a random number yeah you could smuggle it in scanf's actually not that smart we should actually give you something that's a bit smarter than scanf but scanf's pretty dumb alright any other questions for that so now I can actually use this to actually make sure that well I can let the user input whatever they want I just won't let them get away with it alright so we can write our final program of the day so we can write a program now to sum all the positive integers so our input can be any number of positive integers for now I'm just going to assume that but we just wrote a program that would make sure it was positive but I'll just assume they're being nice and then I'll have this keep on adding all of the numbers together until the user input zero and then that means stop so the output should just be a single integer just representing the sum of all of the things the user has input so if I go to sum I will set it up a bit and then give us some time to think about that so I'll create a variable called sum I'll initialize it to zero so I need a variable to store the summation of literally everything the user types or yeah every user the or every number the user inputs so after that I'll ask them to input a positive number create a variable called input initialize it to zero in this case it doesn't matter but good form to have and then I will do a scan F and then get a single number so after this well I have to do some repetition because I want to allow them to input any number uh... that they want so I'll start it off a little bit so because we have repetition we will need a while loop so that keeps on going over and over again until the user actually inputs a zero so what we want to repeat is we want to ask the user over and over again to input more positive integers until they input a zero to stop here I can just have or do a scan F to get the next digit alright so let us look at this and then think about what I need here so what is the condition I want to finally stop at so when do I stop prompting the user for more input yeah yeah when the input is zero so when the input is zero I want to stop asking them so what should my condition here be if only when the input is zero should I have this stop right so I want this to be false whatever the input is zero so what is the condition I need to actually express that in C yep while input so I could say while input that gets into some weird rules but essentially that will work because in this case the C conversion rules is if input is zero that's false any other value will be true so that does work might be a bit hard to read yeah so I set the default value of zero here so yeah there's a question why did I do this or doesn't this screw it up so for running this program it's going to create this variable like it's just going to go line by line so it's going to create this variable assign the value zero to it then do a print F create this variable assign the value zero to it and then this scan F as long as it's successful it will change the value of input immediately so I didn't actually need to say input equals zero I could have left it alone it's just good practice to initialize things but yeah in this case I immediately change it all right so back to the question so this while input will work so let's make sure it does work it's called some so say input a positive integer 12 1 2 and then whenever I hit zero it stops so I'm most of the way there but this usually this should always be a boolean expression so if I want to change a number to a boolean I should use one of those relational operators so any other input as to what I should change this to yep yeah it does not equal zero so if I change it to this well that is probably a lot easier to read and actually expresses what I mean much better so for any other value except zero this is going to be true so no matter what the user inputs if it's negative well in this case if it's negative or positive doesn't really matter this while loop will keep on going and it will only stop whenever the input is exactly zero because only when the input is zero is this false because zero does not equal zero so we would stop and then say the sum is whatever so let's make sure that that works positive number 12 1 0 and then we stop all right what about the last step of actually computing the sum correctly how do I do that where should I do that yeah inside the while loop what should I write yep sum equals sum plus input so each time through the loop I'm just adding the input to the sum so what that will do is the first number I input say in this case I inputted 1 2 3 and since I initialize sum to zero the first time through the loop well sum is zero plus input so 1 2 3 that'll be 1 2 3 and we'll assign that value to sum and then it will ask me to input another number and well I input uh I update that input variable with 12 it would go back up to the top check well 12 is not zero so we would increase our sum so sum was 1 2 3 then plus 12 and then whatever the value of that is is going to be reassigned to sum and then we're going to do that again for input is 1 then increase the sum again then we input zero and then we stop so if we do that 1 2 3 12 1 and then zero and now we get the sum of all that is 136 which is actually what we expect in this case so we sum up the value each time every time through this yep yeah so in this case no reason why I wanted a positive integer I just said that to tie it to the last example but in this the code as I've written it it works fine for negative value so I could sum a negative one negative two negative three which should be negative six stop it six it's fine so I arbitrarily restricted myself for no reason all right any questions about this little program okay if not um I will give you a debugging exercise so say I accidentally what about if I just move this here is this okay here I'll give I'll give everyone like a minute to discuss and then I'll pull so yeah feel free to discuss have I royally screwed up or who's just moving that line all good yeah so now if I moved that line there well at the very least I missed that first number so in this case say I want to sum together two and three oops I didn't compile it so two and three and stop well now I get the sum is three oops turns out in computing you know especially with programming the order matters so what happened in that case is I said input a number and then they inputted right here they inputted two so two does not equal to zero so we would ask them to input another number and then the two they entered well this scan f would just reassign that so now two gets reassigned to three and we lost that two so we just sum is still zero we do zero plus three update the value of sum to be three and then I inputted zero and then it just stopped so because of that I missed the first input and this is like a very subtle change that makes your program suddenly not work so whenever you need to figure out some things like this some things like this remember your computer is not magical it will only start executing at main and then go line by line by line until it does something a little more tricky like a while loop but it's just always going to follow the same rules it just won't randomly do things so here it would always check the input if it's true it would do this go ahead check the input again if it's true it does it again if it's false it goes to the end so that's what happened I just seemed to mess it up a little bit all right any other questions for today because that went real fast all right cool so I'll just hang around here if there's any other questions or anything like that but we can enjoy ending 10 minutes early so just remember pulling for you we're all in this together yep yep so the labs are becoming like more and more complex now yeah because now we have like conditional code execution and loops everything yeah and like the lab specification usually doesn't go over like all the like how all the edge cases should be handled I was wondering if like if it's possible that you guys post the executables for the solutions so we could kind of like run like tests against them and see like how do they react to the to like certain inputs like edge case inputs yeah I can ask but I don't control the labs but I can I can bring it up yeah if you can't because like I wouldn't expect people like knowing assembly being able to like figure out like what the solution is actually doing yeah yeah yeah yeah that's a good suggestion they might be able to do that yeah because like I was wondering I thought that's how the exercise like command works but it's actually like you guys have like pre-written inputs and outputs that are compared like they didn't pair so it never actually has any executable yeah like I mean like there's probably an example but it's just like a test case and they're just inputs and outputs yeah so okay and I had to look at another quick question you probably wouldn't have like that but like for for the integral computation for one of my test cases my answer is like wrong in like significant or significant yeah but um yeah I'm kind of not like I guess I could probably just you know again like multiply everything by a thousand and then just divide by a thousand yeah it the test cases should be fine if it's off by a little bit oh no I mean like they're comparing one like different like this it's output to like five up to the decimal point five number like five digits up to the decimal and mine is like wrong in the third like after the decimal I think and I'm pretty sure like it doesn't like have like uh do anything in epsilon delta yeah uh I'll try to do multiple of like try to do like the integer yeah yeah you can DM me your code on discord too or email me too and I can see if you did anything concerning because like there's more of the one test case there's like four of them yeah and only one of them fails yeah the one with like I'm assuming that's over the floating point like numbers because like well some of them are like kind of like yeah yeah yeah just show it to me I'll see if anything's like obviously weird if it's not like before for the other test cases and like this is crazy like let them plus minus by one because these like especially how it was written it's like yeah it's not gonna work and I was wondering if there is like a