11:36:37 And we've learned a lot about Java. But before I give you a very new and powerful tool in the Java programming language, what I would like to do is a little bit of review. 11:36:52 So let's just look at a few things that we've learned so far. 11:37:05 The first one being that programming languages have keywords or reserve words that can only be used for that specific meaning within the language. 11:37:14 The language itself is very small. So we've also learned that the language are expanded through the use of libraries, the API. 11:37:22 The math library being very useful and already embedded within the language itself. As is the system. We also discovered that we have used import statements. 11:37:35 To use the scanner or the decimal form matter. 11:37:42 We've learned that the primitive data time types are also inherent in the language and we utilize them for data storage. 11:37:52 They have to be declared before used and in good programming form, we declare them at the start of our method. 11:37:59 You'll also note that it batches our flowchart that one of the first boxes and after creating any objects we need. 11:38:08 Is that storage box for the data storage that they were going to use. Those primitive data types are, size and they have a very specific range. 11:38:21 You may revisit that slide. It's posted in the NAV bar on the left. 11:38:25 Side of the course homepage. We also learned the very first rule of programming. The programs are processed in a linear way. 11:38:35 Programming rule number one, the process is linear. They are sequence structures. The nice thing about the flowchart, it preserves that programming rule number one. 11:38:46 We have one start and one stop and a path that goes from start to stop. Now, we also learned the idea of a decision structure which allowed for branching or multiple paths to our sequence structure. 11:39:00 Remember all paths then reunite and go to the one style. So where are we headed? Well, this is our next and very powerful tool. 11:39:11 And let's go. So here we go again, loops or repetition structures. So when you want to repeat a task with different data. 11:39:26 It allows for code reusability. Now the next slide is extremely important. These are some of the key components to a repetition structure. 11:39:37 So this is one of your assignments for this module. After I list these key components, I want you to write them down, make a very nice poster of them. 11:39:50 And you're going to turn that in as part of the assignment for this week. I suggest you make the post a very large and place it in a prominent place within your household so that you can see it often. 11:40:07 The first one being we need a starting value. Something that allows us entrance into the loop. And so that value will determine if the loop will execute. 11:40:16 Now, usually it's initially initialized to set so the loop will execute, but not always. 11:40:26 Sometimes we don't want to execute the body of the loop. Number 2, a conditional statement. 11:40:31 Loops run on true. So when the conditional statement is true, the loop is executed. Now you've already been exposed to conditional statements when we learned about branching that if else those were conditional statements that you were writing that evaluated to one of those Boolean valleys either true or false. 11:40:53 Now when that conditional statement evaluates the true, the loop body is executed. Okay, when it's false the flow of control proceeds to the statement following the loop. 11:41:05 So in other words, we only go into the body of the loop when that conditional statement is true. 11:41:13 Once it becomes false, we again proceed in a linear fashion throughout the code. Now number 3, this is the really interesting and fun part. 11:41:21 These are, this is the body. The commands are instructions that we want to repeat. Now, just as with the if and the else, only one statement is executed if that conditional statement is true. 11:41:37 Thus, we want several tasks to happen within the body of the loop, we are going to use those curly braces to create a compound statement. 11:41:45 Now, also important in the body include only the tasks that require repeating. And so I'll give you a brief example here. 11:41:56 Let's say we were collecting a series of scores for tests. And we wanted to figure out the average test score. 11:42:05 Well, in the body of the loop, we would want to sum up the scores that were being entered. 11:42:11 But within the body of the loop, we would not want to calculate the average. Only after we have all the scores and account of how many scores were entered. 11:42:22 And we take the sum of the scores divided by the count would give us the average. But that average would not need to be. 11:42:30 Completed within the body instead outside and after the body of the loop where the date of was being summoned, summed, and the values being counted. 11:42:44 Now the very last one, Where did I get that? Most answer method of egress. So however you want to remember it, this is probably one of the most important components because you must have a mo. 11:43:01 It must occur within the body of the loop. And in other words, its job is to manipulate that starting value to cause the conditional statement to evaluate to false. 11:43:10 Now we're gonna look at a few different kinds of loops pre test and post test. 11:43:16 And we'll look at their syntax and how this works. But somewhere within the body of the loop, we have to make that conditional statement eventually become false. 11:43:28 Otherwise, you will write an infinite loop. So beware. Without a method of egress, you've created an infinite loop. 11:43:38 So without mode, that's bound to a. Now how would you do that? Well, you can use a Boolean control variable that's initially set to true and somewhere within the body of the loop. 11:43:51 It's return becomes false. Another one that's very common is reaching a desire count on an index or control variable. 11:44:00 So. That's done by either incrementing or decrementing that value until it reaches either 0 or reaches the max length, whatever. 11:44:12 Either end will work. Or as we're gonna see as we progress through the course, sometimes user control. 11:44:21 And you wanna think about this, if you've ever launched one of your games, that means you wanted to play it and you start playing it. 11:44:27 And after the first round, when you lose, sometimes it asks, do you want to play again? And then you can say yes and the game relaunches or if you say no the game shuts down. 11:44:40 And so sometimes our way out of the loop, the repetition is by user control that they say, we don't want to do this anymore. 11:44:48 So now let's look at the 2 major categories of loops along with their flowcharting and their syntax. 11:44:58 Okay, by the way, your labs for this week are embedded within this video. I will try to give you helpful points when we get to them. 11:45:10 So the first type of loop that I want to introduce you to is in the category of pre test loop. 11:45:16 So a pre test loop means that that conditional statement is verified or tested. Before the body of the loop is executed. 11:45:26 And it looks something like this. Let's say access my control variable. So you see that's my starting value and I set it to what? 11:45:36 Now here's my conditional statement. Now if you notice I'm removing the word if at this point. 11:46:24 Statements, but we're not going to ask if. So you'll see our conditional statement here is just within the parentheses acts less than 10. 11:46:34 Well, you can see x equals one, so it's less than 10. So this is true. 11:46:40 I remember loops run on true. 11:46:45 So here's the path into the body of the loop. Now in the body of the loop, I'm only demonstrating a process box here, but you would have maybe several boxes, whatever the test needs to be repeated. 11:46:57 Now the box that is important. Is the This is part of the loop body. It either happens last or it can happen at any point, but it happens within the body of the loop. 11:47:12 And so in this case, our goal is to get X to be what? 10, right? Cause 10 is not less than 10, so we'll fall out of the loop. 11:47:21 So you can see I'm incrementing X in this example. Okay, and then where does our flow go back to? 11:47:28 Not the starting value, but the conditional statement. Do you see how the arrows mark that? And so eventually, whatever our task was, keeps getting incremented and it finally reaches 10. 11:47:44 At which case, 10 is not less than 10 and it's false and we fall out of the loop. 11:47:47 Now we are back to our linear process. Now, the loop syntax for this flowchart is the 4 or the while. 11:47:59 Now in the case of the for loop, which will see the syntax in a moment, that actually happens last. 11:48:08 So in the while loop, it is the programmer's responsibility to put it somewhere within the body of the loop. 11:48:14 In the case of the 4 statement, you'll see it's nicely listed as part of the 4 statement. 11:48:19 And it is actually executed as the very last step in the body of the loop. This is very important to understand. 11:48:29 That mo occurs as the very last command before going back to check the conditional statement in the case of the for loop. 11:48:40 Now, if I were to ask you how many times or how many iterations will this loop body be executed? 11:48:48 Think about it. Do you have an answer? Did you say 9? If you did, you're correct. 11:48:57 Because while x equals one, we'll go in for the first time. 2, 3, 4, 5, 6, 7, 8. 11:49:04 9 is less than 10. We'll go in once X becomes 10. We are done. So this loop will execute 9 times. 11:49:13 Now, when we know the number of iterations like we know we want to run a loop 10 times or 100 times or a million times. 11:49:22 The best loop syntax choice is the for loop. So we'll come back and we'll get sentax in a little bit. 11:49:31 I first want to introduce you to some of these the flowcharts. Okay, so let's look at. 11:49:49 Host test loops. Now in a post test loop, what happens is the body of the loop is executed at least once. 11:49:58 That's a big keyword there. The body of the loop is executed at least once. In other words, the body precedes the conditional statement. 11:50:08 And so you can see I placed the words doing while on the screen, but let's see the flowchart coming into focus here. 11:50:16 So again, we still have a starting value. In this case, I've shown an example with a bullian variable x and I've said it to true. 11:50:26 Then what happens is I'm putting the syntax for the do while loop on the screen here for you so you can better understand it. 11:50:35 The loop body whatever is gonna be repeated in this case, I'm showing an example. 11:50:42 Maybe we get from the user input a new value for acts and they finally say false. But look what our conditional statement goes. 11:50:51 So we see our starting value, our body. Our mo and then again see I'm removing that if while Acts equal equal true. So this is our conditional statement. 11:51:05 Notice it goes last. Now, the other thing I want you to note is I have put the syntax of the do while on the screen for you to help you. 11:51:16 So you start with the due and an opening curly brace, you'll have the body task, the mo embedded in there. 11:51:23 A closing curly brace, the word while. The conditional statement within parentheses and then there's your semicolon at the act. 11:51:33 Okay, and so while it's true. We go back into the. Eventually, it becomes false because it from user input, they change it to false. 11:51:46 Think of that as when you're running a game. And so how many iterations will this loop body execute? 11:51:57 Okay, think about it. The answer is on the screen. Did you say at least once? Then you are correct. 11:52:07 So while we're looking here at is a post test loop, there is only one loop syntax for it and it is the Now, as noted. 11:52:18 When we want to do something at least once, but then usually, it's user controlled. 11:52:26 This is the best loop choice. Now it doesn't always have to be user controlled. But as I stated with that example, if you launch a game and you want to play it at least once and then you may play it again or you may not. 11:52:39 So you can see when this would be the best choice. If we want to do the body task at least once and then from there decide if we're going to do it again. 11:52:47 The do while is our best choice. Now, something that we have been introduced to and we will keep revisiting are the increment and decrement operators. 11:53:00 They play heavily in loops because they are ways to get that m, that method of egress. 11:53:06 They are unary operators. Their associativity is right to left. Remember the increment operator adds one to the upper end. 11:53:16 The decrement operator subtracts one from the operand. They can be prefix, meaning perform the operation. 11:53:25 Of adding or incrementing or decrementing before using the value. Or they can be post fix. 11:53:34 In other words, use the current value in that variable. And then after you've used it. Update its value in storage. 11:53:42 So remember there's a big difference between free fix and post fix. So there's many ways to say the same thing. 11:53:51 For example, all of these statements will increment x by one. X equals X plus one. X plus equal one. 11:54:01 Plus plus x or x plus plus. Now in this case, the prefix and post fix do not matter because they are in one command. 11:54:10 And as a result of the end of that command, the value will be incremented. It is more important when you see these operators within a larger expression. 11:54:23 Now there's a source code example for you on increment and decrement operators posted in this. 11:54:30 Module. Okay, and so these operators are often used as an index or counter control variables. So these are the variables that are used to count the iterations or repetitions of the loop body and to eventually cause the conditional statement to become false. 11:54:52 So as a reminder. Where do they sit? Way at the top. Okay, so are they occur before anything else in a statement. 11:55:05 So there's our. Increment and decrement operators. Okay. The post has higher precedence than the pre. 11:55:14 Please note that. And always, when in doubt, use those parentheses. Now there's some other terms I want to introduce you to as we're working with loops. 11:55:25 The idea of Sentinel values. Now, a sentimental value. Is a value that's used to mark the end of the list. 11:55:34 Now, can it be anything? No, not exactly. The value cannot be a potential member of the list. 11:55:42 Okay. Oftentimes they'll Sentinel values can also be used to terminate a repetition structure. 11:55:50 So for example, Let's say the program was to collect or enter positive integers for X. 11:55:58 Well, you could enter 2, then 3, then 72, then 0. Negative 16 is not a positive integer. 11:56:06 So again, removing that is as long as x is greater than negative one, the loop executes. 11:56:14 So. Any negative value could be used as a sentinel. Or here's another example. Enter lowercase alphabet characters for X. 11:56:27 So we enter H then E, then L, then L, and then upper case B. Well, again, we could write the conditional statement here as long as x is greater than or equal to lowercase a. 11:56:38 And X is less than or equal to lowercase z. So in here I've written a compound statement that gives me the range of A through Z lowercase. 11:56:50 So as long as the value is A through Z lowercase, the loop executes. Please revisit the ASCII table for better understanding and you'll see A through Z are nicely neatly together and we can compare the range. 11:57:02 This will be a very useful technique for some other labs that you will be doing throughout this course. Please make sure this is solidly in your notes. 11:57:10 Now one other thing I want to bring to your attention. As we've used next in next float next double next line for strings in this course there is no next care. 11:57:23 So characters have to be taken in as a string. So in other words, creating a temporary string and then reading the character at the first position, character at 0. 11:57:35 Please make sure that you remember that idea. Now, let's look at syntax, shall we? 11:57:46 So the syntax for the for loop looks something like this. 4 int I equal 0, there's our starting value. 11:57:55 I less than 10. That's our conditional statement. I plus, that's our mo. 11:58:03 And so note that there is no semicolon at the end of this line. If you put a semicolon at the end of this line, it would be the equivalent of riding the empty statement for the loop. 11:58:16 In other words, execute the loop. 10 times, but do nothing. So make sure you do not put a semicolon at the act. 11:58:25 Now there's our curly brackets to encapsulate the body of the loop. Now notice in this case, the mo is not in the body, it's in the for statement. 11:58:37 It is executed last, like right before that that closing curly brace is when the I plus plus would be executed. 11:58:45 Now this is all fine and good. But when we declare I within the 4 I scope. There's that word scope again as I promised I would introduce it as needed throughout the course. 11:59:00 High school is only within the loop. So in other words, if I were trying to reference I after the loo. 11:59:10 I would get a compiler error. It would say I does not exist. So in better programming form, I would suggest that you do this when you declare your variables at the start of the program in that giant storage box declare and I. 11:59:28 Then in the body of your for loop, sorry, in the 4 header statement. Say i equals 0. 11:59:37 So you declared storage pace for an integer i and then you gave it its initial value in the for loop. 11:59:43 This is in better programming. 11:59:48 Okay, so. This is the best choice when the number of iterations is known. And here is your first embedded lab. 11:59:58 I want you to use the for loop to display to the screen only the odd values from one to 25 inclusive, inclusive meaning including 25. 12:00:11 Slow charted first, then code it using a for loop. Please use the sample. Flow charts that I gave you, there is no for symbol in the flowcharts. 12:00:24 I showed you how to use it using conditional statements. Now, the example output. Here is the answer on the screen. 12:00:33 It should be one comma 3, comma 5. etc., etc. But please note after 25 our last value, there should not be a comma. 12:00:45 This is something you need to handle. Please make sure your output looks exactly like what you see on the screen. 12:00:54 There are several ways to solve that problem. Have fun with it. I would suggest at this point you pause the video. 12:01:02 And work on the for loop lab. 12:01:12 Welcome back. Your next lab is using a wild loop. So let's look at the syntax. 12:01:20 So again, in this example, we have a starting value of into x equals 0. And while X is less than 10, there's our conditional statements. 12:01:31 Now again, no, no semicolon. We'll have our body task. Now this time as a programmer, it is your responsibility to make sure that the mo is a port in within the body of the wild loop because it's not nicely placed in the header as in the for loop. 12:01:51 The other thing to note is for all of these loops, just like the if and the else, they execute one statement. 12:01:58 And so if we need to do multiple statements within the body, we need those curly braces. Okay, and so there's our parts, we have the starting value, the conditional statement, the body depends on what you're going to be doing. 12:02:19 Now this is the best choice. When sometimes the loop doesn't need to be executed at all. 12:02:25 In other words, for some reason, the starting value. Provided by the user or changed by the program itself. 12:02:32 Because it's variable. It's already set to 10 and we never go into the body. Okay, this is also good loop choice when we're going to stop on the Sentinel value like we're looking for an uppercase letter or something like that. 12:02:47 Okay. And so your job this time, here's your lab, you're gonna use a repetition structure, specifically the while loop. 12:02:56 To sum all the positive integers entered by the user until a negative integer is entered. Then the goal is display the sum and the mean. 12:03:08 Mean as a fancy word for the average of the positive integer list to the screen. So flowchart then code using a while loop. 12:03:18 Now here's a big hint. Make sure you do not add that negative value to your or use it when computing the average. 12:03:26 So be very careful not to include that sentinel value. 12:03:34 So here is the example code that I want you to use. So when you run it, use these values. 12:03:39 So I want you to have an output statement that says please enter positive integer values. For this sum and mean enter a negative value to stop the computation process. 12:03:51 And so the data I want you to enter is 1, 2, 4, negative 17. And so the result that you should be displaying to the screen. 12:04:03 By your computer, by your program computing it, the sum is 7. And the mean is 2.3 3 3. 12:04:10 So these are things to think about. I'll give you a hint. Note that the mean is not an integer. 12:04:17 Think about typecasting. And some forcing very, variable values that were brought in an earlier lesson. 12:04:26 That is a huge hand. Okay. Let's see, do I want to give you any other hints? 12:04:35 No, I think everything is there. Again, pause the show. Do this lab. Then we'll move on to the do while. 12:04:52 Okay, time for the do while. So we saw a little bit of the syntax in the flowchart so that you could better understand it. 12:04:58 Here is the syntax without the flowchart. So again, let's say we have the intacts equals 0. 12:05:07 That's our starting value. There's our word, There's our opening curly brace. 12:05:11 Now the do does require Well, unless you can work the mow into the body task as well, the due usually requires curly braces. 12:05:22 Because you have some sort of body of the loop. Some sort of method of egress. If you could do that both the body and the method of egress and one statement, you could get rid of the curly brackets, but I don't think that's too much of an option most of the time. 12:05:39 There's a closing curly brace. And there's our wild access less than 10 our conditional statement. 12:05:48 Now notice this one we do use a semicolon at the end of that while header so it's like while X less than 10 that's a conditional statement there's where we place our semicolon. 12:05:58 This is important to know. Okay. So this is our best choice when we know we wanna execute the loop at least once. 12:06:08 So your lab for the do while is I'm gonna ask that you ask the user for a single character input. 12:06:16 Now remember that's tricky. You're gonna have to take it in as a stirring and then parse out just the first character. 12:06:23 Using that care at 0. Please go back and review that video. And then I need you to display if it's either uppercase, lowercase, or non alpha. 12:06:34 If the loop is non alpha, meaning it's not an upper case letter or a lower case letter, the loop should terminate. 12:06:41 So that's our sentinel for it. Now please go back to the ASCII table and you'll see that the upper case letters and the lower case letters can easily be reached within ranges. 12:06:53 And I've already demonstrated that range for lowercase within this video. So you're gonna flow chart then code using a do while loop. 12:07:03 This is our post test loop. Now I'm gonna want you to run this program multiple times. So the code I want you to run it with is not the string ABCD explanation point. 12:07:14 But run it with the letter A. Because there's a loop, the loop should then ask me for another letter, which will be uppercase B, then lower case C, then uppercase D. 12:07:28 And then when I hit that exclamation point, it should tell me that it is not alpha and it should terminate the loop. 12:07:34 Now you see that for the other 3 examples. So when you get to last one, the ampersand it should say this is non-alpha in it to terminate immediately. 12:07:45 So you need to run it with those 4 sets of input. Okay. Again, they're not a stirring. 12:07:53 Enter them individually as individual characters. That's your next lab. Again, please pause the show. 12:08:01 Try this lab and we'll finish with. One last lab and some closing notes on loops. 12:08:16 Nested loops. Wow. So just like we could nest if else branching. Statements to create more complex decision structures, we can nest loops. 12:08:27 So in other words, our task. It has to be repeated. So inserting a loop structure within a loop structure. 12:08:37 The inner loop will always complete before we're returning to the next iteration of the outer loop. The total iterations of both loops will be the number of outer iterations. 12:08:48 You'll need to know that kind of information when you take your intro to algorithms class and you look at the time complexities of the work that you're doing. 12:09:03 Now in this lab time, I just need you to use 2 simple 4 loops. The outer loop is going to print the rows of a multiplication table and Sorry, the outer loop is going to list the rows of the multiplication table and the inner loop is going to go across the columns. 12:09:24 Rose go horizontally on the page. Columns are down the page. So that inner loop is just gonna go across. 12:09:33 In the top example, it's gonna be 1, 2, 3, 4, 5, 6, 7, 8, 9, 1011, 12. 12:09:38 That's example of the row, which the inner loop creates those columns. Now the caveat for this one is you are going to need print app. 12:09:47 I expect your multiplication table to look nice and neat like the one that you're seeing. On the screen. 12:09:57 This is your last. Lab embedded within. This presentation. Now I do have final notes. 12:10:07 So please stick around for one more moment and look at the final notes and then you can backtrack to this nested loop lab. 12:10:17 So lazy programmers. There are some sloppy techniques. For, running loops. 12:10:26 So one is continue, which means when if you have a conditional statement inside the body. And let's say if that conditional statement is true, you don't want to do any more work. 12:10:36 You just want to continue on to the next iteration. Sometimes that can be useful. So continue you can use upon occasion. 12:10:44 You should never use break to prematurely exit the loo. We learned the word break in our switch case and it was the only way to break out of the switch case once our we met matched the case and executed the code. 12:11:01 Break and be used in Luke's, but it is very sloppy programming. If you were to do that on a job interview, you probably wouldn't get the position. 12:11:11 So the trick is write a very good conditional statement. It can be a compound statement to handle ex exiting the loop. 12:11:21 For example, using a Boolean variable that is something condition happens, that that Boolean variable to false and use that as an additional. 12:11:32 And condition. For the conditional statement to cause the loop to stop. Using the word break is not allowed. 12:11:40 Your lab will be scored as a one. They are not allowed in class. Other than for the switch statements in the switch case. 12:11:52 I hope you enjoyed this repetitive. Lecture on repetition structures. In the labs work out well if you have any trouble at this point, please make sure. 12:12:05 Seek help. This course moves very quickly. Ask a peer leader, visit the tutors during their office hours. 12:12:16 Or as always, you are welcome during my office hours. Have a great time with Loops. Make sure that you complete that poster. 12:12:28 The details will be on the course homepage. Have fun and whatever you do beware the infinite loop