1 00:00:01,410 --> 00:00:02,920 OK, let's just looping. 2 00:00:03,030 --> 00:00:10,470 So what we're going to do is a little example is we're going to have a program that can draw lines and 3 00:00:10,470 --> 00:00:14,980 they can draw lines either horizontally, vertically or diagonally. 4 00:00:15,840 --> 00:00:23,370 And the way we want to do this, and that's just demonstrating in code is a vertical line would look 5 00:00:23,370 --> 00:00:24,270 something like this. 6 00:00:30,400 --> 00:00:35,680 So it's just a series of characters going down the screen while the horizontal line would look something 7 00:00:35,680 --> 00:00:36,200 like this. 8 00:00:38,320 --> 00:00:44,530 So a series of characters coming across the screen and then a diagonal line would look like this one 9 00:00:44,980 --> 00:00:45,730 and down. 10 00:00:46,900 --> 00:00:48,550 Over one. 11 00:00:48,940 --> 00:00:52,030 Down, you know, over. 12 00:00:54,900 --> 00:00:55,950 Two spaces. 13 00:01:00,450 --> 00:01:07,890 And then over three spaces and so on to draw a diagonal line, so we have our three choices and we can 14 00:01:07,890 --> 00:01:11,400 say how long we want the line to be within reason. 15 00:01:13,050 --> 00:01:19,370 So we want to do is to first of all, it's like our Frys example. 16 00:01:19,380 --> 00:01:26,280 We're going to ask, how many do you want a vertical and horizontal line or diagonal and then how many 17 00:01:27,880 --> 00:01:31,570 how long do you want it to be in length being the number of characters? 18 00:01:33,180 --> 00:01:34,340 So how do we do that? 19 00:01:34,350 --> 00:01:36,210 Let's start with a prompt and an answer. 20 00:01:37,230 --> 00:01:44,880 We'll say let trade equals prompt. 21 00:01:49,200 --> 00:01:58,040 What type of line and what do we do with the Fry's example and say, let's see each 22 00:02:00,480 --> 00:02:08,310 horizontal, the vertical, the diagonal. 23 00:02:12,990 --> 00:02:19,980 So we're going to prompt for that and then we will also prompt for how long you want it to be. 24 00:02:21,560 --> 00:02:26,400 What does the tribute to LVM equals prompt? 25 00:02:29,320 --> 00:02:39,530 How long do you want to our line and we won't bother you right now. 26 00:02:39,540 --> 00:02:44,250 I could type in one million and that would be a little annoying. 27 00:02:44,250 --> 00:02:46,830 But we won't bother to check for it now. 28 00:02:47,370 --> 00:02:55,080 And then we'll say, OK, depending on whether it's HVAD, we're going to do something a little bit 29 00:02:55,080 --> 00:02:55,480 different. 30 00:02:56,520 --> 00:03:06,780 So let's say let's do a switch like we did or say switch on the type that to upper. 31 00:03:12,390 --> 00:03:13,770 And then we'll say Case 32 00:03:16,620 --> 00:03:19,060 H, we're going to do something. 33 00:03:19,080 --> 00:03:28,530 But we're going to end it with a break case study and we're going to do something with a break case. 34 00:03:28,680 --> 00:03:30,080 The we're going to do something. 35 00:03:30,200 --> 00:03:31,290 We're going it with a break. 36 00:03:34,560 --> 00:03:35,340 Default. 37 00:03:36,840 --> 00:03:42,680 And just to clarify, we use the prompt to get information from the user, but we're going to still 38 00:03:42,720 --> 00:03:46,740 really use council dialogue in this program to send information back out. 39 00:03:48,810 --> 00:03:58,140 So we'll say, counsel, that if I can use a consultant error, I'm not really sure what kind of big 40 00:03:58,140 --> 00:04:02,100 difference it makes, but we'll say cancel that error and we will say 41 00:04:06,210 --> 00:04:11,970 you did not pick a valid line type. 42 00:04:17,930 --> 00:04:19,850 All right, so we our choices. 43 00:04:20,450 --> 00:04:22,370 So for right now, let's just see if it works. 44 00:04:22,490 --> 00:04:24,650 And so we'll just do some consider logs, 45 00:04:29,600 --> 00:04:30,470 horizontal line. 46 00:04:39,850 --> 00:04:42,670 And so this could be a vertical line 47 00:04:45,400 --> 00:04:47,110 and this was going to be a diagonal line. 48 00:04:50,840 --> 00:04:54,140 All right, let's run this. 49 00:04:58,180 --> 00:05:07,410 And we need to open up the death tolls, so we'll run a world with that line will say H how long you 50 00:05:07,410 --> 00:05:11,420 want to be 12 horizontal line. 51 00:05:11,770 --> 00:05:13,330 OK, looks like it's working. 52 00:05:13,730 --> 00:05:15,640 We can check the other possibilities. 53 00:05:16,240 --> 00:05:19,630 So let's do our horizontal line to start with. 54 00:05:19,930 --> 00:05:24,880 And what we do is use a for loop and we want it to be this length. 55 00:05:26,080 --> 00:05:29,100 Now, one thing, when we enter this prompt, we're going to get a string. 56 00:05:29,380 --> 00:05:31,750 And so the string might be the string 23. 57 00:05:31,760 --> 00:05:33,310 We have to convert it to a number. 58 00:05:34,390 --> 00:05:42,940 So let's convert it to a number before we get any further down here and say let let's call this Elián 59 00:05:42,940 --> 00:05:49,270 String and we'll said that we're not going to change it. 60 00:05:49,270 --> 00:05:53,950 So let's make it a token one equals. 61 00:05:54,250 --> 00:06:02,800 And then we can say we use a method on no, we can say no that pass it. 62 00:06:03,670 --> 00:06:08,890 And what that does is says send a string and we'll give you a number back. 63 00:06:09,700 --> 00:06:15,580 And I'm not sure right offhand what it does if you send us something that's not a number. 64 00:06:16,030 --> 00:06:21,120 But for right now, we'll pretend like we're always going to set a number of songs and strength. 65 00:06:23,350 --> 00:06:29,110 And if you go back to one of the first examples, the type of loan string would be string and then the 66 00:06:29,110 --> 00:06:31,790 type of Elian would be no. 67 00:06:33,310 --> 00:06:33,660 All right. 68 00:06:34,420 --> 00:06:36,550 So we want to do in here then. 69 00:06:36,550 --> 00:06:37,660 It's a horizontal line. 70 00:06:38,200 --> 00:06:46,210 So let's say for what I and traditionally people use I in for loops. 71 00:06:46,630 --> 00:06:49,950 I'm not sure why maybe for index or something like that. 72 00:06:50,680 --> 00:06:58,890 So for I equals zero and we want to do it the length times. 73 00:06:59,110 --> 00:07:04,030 So we'll say I less than Leon I was plus. 74 00:07:09,420 --> 00:07:16,890 OK, and then also want to do is to we can't say cos of that log and then do it multiple times because 75 00:07:16,890 --> 00:07:19,370 every time you consider a log it goes down to the next line. 76 00:07:19,860 --> 00:07:27,390 So we're going to need to do is, is build this result will build a string and then I'll put it in the 77 00:07:27,390 --> 00:07:27,720 end. 78 00:07:29,070 --> 00:07:32,670 So we'll say let. 79 00:07:36,190 --> 00:07:39,940 Blind string equals an empty string. 80 00:07:42,130 --> 00:07:52,720 So then we will say a line string, and if you recall, you can use the carbonation operator, which 81 00:07:52,720 --> 00:07:57,900 is a plus sign, but you can say plus equals to add on to a string. 82 00:07:59,200 --> 00:08:01,560 So I'll just add plus equals star. 83 00:08:03,550 --> 00:08:09,190 And then at the end it will say carful, that log 84 00:08:12,550 --> 00:08:13,270 line string. 85 00:08:15,910 --> 00:08:16,290 All right. 86 00:08:16,810 --> 00:08:17,800 So let's take a look at this. 87 00:08:21,960 --> 00:08:31,800 We only have the horizontal workings, so let's let's test that and H will say 22 and then we go horizontal 88 00:08:31,800 --> 00:08:32,040 line. 89 00:08:33,930 --> 00:08:39,860 All right, so now we have to do the same thing except with the vertical line. 90 00:08:41,280 --> 00:08:44,040 So let's do more or less the same thing. 91 00:08:44,610 --> 00:08:51,240 Now, let me tell you one thing that hasn't really been covered yet, which is that there are nonprofitable 92 00:08:51,240 --> 00:08:55,830 characters are non visible characters, and one of them is called a new line. 93 00:08:56,730 --> 00:09:03,720 In the way that you put it in a new line is it's just added to our line string, just as an example. 94 00:09:04,890 --> 00:09:08,130 Why is our horizontal line, as you put it, in a backslash end? 95 00:09:08,640 --> 00:09:09,570 And that's the new line. 96 00:09:09,600 --> 00:09:13,470 So if I put in a few backslash ends, we'll see if we run this example. 97 00:09:13,470 --> 00:09:20,130 It's going to put us in lines before we get to our I'm messing up with calling things the same thing, 98 00:09:20,130 --> 00:09:23,960 but it's go put in some new lines before we get to the line that we're trying to output. 99 00:09:24,780 --> 00:09:26,190 So also horizontal. 100 00:09:26,930 --> 00:09:27,500 Twenty two. 101 00:09:28,630 --> 00:09:30,950 So you see, it went down a few lines before it got there. 102 00:09:31,950 --> 00:09:35,370 So with the vertical line, we wanted the exact same thing. 103 00:09:35,370 --> 00:09:39,720 We want to, but we want to say start new lines, start new lines, start new line. 104 00:09:40,530 --> 00:09:42,550 And that way it's going to go down instead of a cross. 105 00:09:44,460 --> 00:09:49,760 So here we're essentially going to repeat this, except that we're going to put in the new line. 106 00:09:50,670 --> 00:09:54,840 So why don't we go copy this and we're going to cancel that. 107 00:09:54,840 --> 00:09:55,590 Load it out. 108 00:10:01,650 --> 00:10:09,510 And then we're going to start a new line and then each one that adds to it, it's going to go down instead 109 00:10:09,510 --> 00:10:10,080 of across. 110 00:10:13,360 --> 00:10:14,620 So let's take a look at that. 111 00:10:14,740 --> 00:10:17,390 That's the vertical run. 112 00:10:17,440 --> 00:10:29,080 So say we all say 12 and we go so good that instead of a cross now we can mix this up and do the diagonal 113 00:10:29,080 --> 00:10:29,400 line. 114 00:10:29,750 --> 00:10:34,300 It's going to be just a little bit trickier because it's going to have to get down and then it's going 115 00:10:34,300 --> 00:10:38,520 to have to go across a certain number before we can put it out the star. 116 00:10:39,820 --> 00:10:47,430 So we need to keep track of how many rows, which is the eye, and do that number. 117 00:10:47,440 --> 00:10:49,020 So let's copy this for a loop again. 118 00:10:55,000 --> 00:11:05,110 And back to the beginning, so the follow up is going to cause us to go down, but we also want to go 119 00:11:05,110 --> 00:11:06,370 across that number. 120 00:11:07,690 --> 00:11:09,760 So we're going to need a nested for loop. 121 00:11:10,720 --> 00:11:17,260 So we'll say for Jay and Jay is going to start at zero. 122 00:11:18,550 --> 00:11:23,050 But each time, you know, we're going to be going down rows. 123 00:11:23,350 --> 00:11:29,770 So each time is going to go down one over one down to over two, down three over three. 124 00:11:31,270 --> 00:11:37,030 So we need to put in the very first time we're going to go over zero because it could be on the left 125 00:11:37,030 --> 00:11:40,840 border and the next time it's going to need to go over one. 126 00:11:41,830 --> 00:11:53,680 So that corresponds to the value of so we start to set I instead of instead of zero and then we're going 127 00:11:53,680 --> 00:11:56,080 to need to go that number over. 128 00:11:56,740 --> 00:11:59,860 So we want to go over until J. 129 00:12:00,820 --> 00:12:19,690 Equals J plus I think or while it's less than that, what J is less than J plus I j plus plus and we'll 130 00:12:19,690 --> 00:12:23,390 do a little bit of debugging to show how this works. 131 00:12:25,630 --> 00:12:31,480 So again, we want to do this, this line string. 132 00:12:33,340 --> 00:12:39,210 Well, first of all, we're going to add spaces to the line because we're going to go over that amount 133 00:12:39,380 --> 00:12:43,780 of outside line string plus equals a space. 134 00:12:44,710 --> 00:12:48,580 And so it's going to push it over until it gets to where it's ready to stop. 135 00:12:49,120 --> 00:12:51,850 And then we're going to add the star and the backslash at. 136 00:12:54,210 --> 00:13:04,530 All right, so let's see what this does may maybe may not work, but we'll try it so diagonal, I will 137 00:13:04,530 --> 00:13:09,360 say five to make it short and did absolutely nothing. 138 00:13:09,520 --> 00:13:12,030 Uh, we need to put in our counsel. 139 00:13:12,030 --> 00:13:13,060 That reminds. 140 00:13:17,220 --> 00:13:20,370 So I see what I'm doing wrong here, which is that this case less than J. 141 00:13:20,370 --> 00:13:24,000 Plus I well, J is going to keep going up, so J. 142 00:13:24,000 --> 00:13:25,410 Plus is going to keep going up. 143 00:13:25,410 --> 00:13:26,430 So it's never going to get there. 144 00:13:27,450 --> 00:13:33,990 So it's actually I times to I believe so. 145 00:13:34,010 --> 00:13:34,590 Let's try that. 146 00:13:35,680 --> 00:13:42,060 OK, let's run this diagonal five. 147 00:13:43,950 --> 00:13:47,840 I was told that a vertical line, so the question is why? 148 00:13:48,420 --> 00:13:54,210 So let's introduce debugging and chrome, which can help us discover what's wrong. 149 00:13:55,250 --> 00:14:03,890 So what we do is I have my chrome open up here and you can see that there's a tad sources and we have 150 00:14:03,890 --> 00:14:05,140 our source code in here. 151 00:14:06,500 --> 00:14:08,870 And so what we do is find this position. 152 00:14:08,870 --> 00:14:13,880 And I'm going to go over here to the line 24 and do a little click here, and that's going to set a 153 00:14:13,880 --> 00:14:14,950 break in the code. 154 00:14:16,070 --> 00:14:16,820 So let's run. 155 00:14:16,820 --> 00:14:17,810 Hello, world again. 156 00:14:17,820 --> 00:14:24,980 It's going to say, do you want diagonal five that's going to get down here and it's going to pause 157 00:14:24,980 --> 00:14:25,910 in the code here. 158 00:14:26,510 --> 00:14:31,430 And I'm going to make this extra big so it you a little bit better. 159 00:14:32,300 --> 00:14:33,730 So this is different windows. 160 00:14:34,340 --> 00:14:36,830 We have what's called call stack, which we don't worry about. 161 00:14:36,830 --> 00:14:38,750 And then there's local variables into that. 162 00:14:38,750 --> 00:14:39,740 Elian is five. 163 00:14:39,740 --> 00:14:41,570 Alien string is string five. 164 00:14:42,020 --> 00:14:43,940 Our line string is a string. 165 00:14:44,090 --> 00:14:47,680 This is undefined, which won't worry about in our type is the. 166 00:14:48,500 --> 00:14:51,800 And you see we have one breakpoint to find this mother things down here too. 167 00:14:52,190 --> 00:14:55,320 But we're really interested in what's going on with these local variables. 168 00:14:56,180 --> 00:15:05,930 Now you can step in here and you see that the step over the next function call is F ten step into and 169 00:15:05,930 --> 00:15:06,690 step out of. 170 00:15:06,690 --> 00:15:10,260 But we're really right now interested in just this next function call. 171 00:15:10,700 --> 00:15:17,060 So if I click this, it's going to step down a line and it's kind of actually working its way across 172 00:15:17,060 --> 00:15:21,050 the floor right now, says checking the condition. 173 00:15:21,800 --> 00:15:23,160 And now it's coming out of the next slide. 174 00:15:23,180 --> 00:15:31,280 So our eye is zero and you see that it's defining blocks that saying, OK, you got a block, I zero 175 00:15:31,730 --> 00:15:32,630 your local variables. 176 00:15:32,630 --> 00:15:34,430 Are this because they're outside of this block. 177 00:15:35,420 --> 00:15:38,860 So I'm using the F ten to step because it's a little more convenient. 178 00:15:39,650 --> 00:15:43,730 So then our eye is zero or eight times to zero. 179 00:15:44,720 --> 00:15:46,160 RJ is also zero. 180 00:15:47,870 --> 00:15:55,210 Right, because it's going to initialize jaja zero and it's going to say while J is less than eight 181 00:15:55,220 --> 00:15:56,120 times two. 182 00:15:57,650 --> 00:16:02,090 So that's not it is what we want because we don't want to go for any spaces. 183 00:16:03,530 --> 00:16:06,670 So it's going to go over and it's not going to go into that loop at all. 184 00:16:06,680 --> 00:16:10,790 It's going to put in the star all right. 185 00:16:11,510 --> 00:16:21,620 Now or is one and it's because said what J equals one and continue while J is less than eight times 186 00:16:21,620 --> 00:16:22,520 two, which is two. 187 00:16:23,860 --> 00:16:30,880 So it should give us one space, oh, you know what, I could see what I did wrong here and use the 188 00:16:30,880 --> 00:16:32,650 double cross instead of a plus equals. 189 00:16:35,240 --> 00:16:37,840 So let's go back and change this and then we'll step through it again. 190 00:16:40,910 --> 00:16:46,820 So six plus equals so safe, 191 00:16:50,420 --> 00:16:56,440 just needed this this resume, script, execution, know, reload, 192 00:16:59,420 --> 00:17:01,330 so we'll do our run Hello world again. 193 00:17:02,020 --> 00:17:08,780 This kind of say diagonal will say five stops down here. 194 00:17:09,530 --> 00:17:12,110 So now we'll step in to it. 195 00:17:12,920 --> 00:17:15,150 See, our eye is zero. 196 00:17:17,150 --> 00:17:19,510 We don't get a J because the first one doesn't get one. 197 00:17:19,520 --> 00:17:21,250 So we're going to output our line string. 198 00:17:22,190 --> 00:17:24,920 We're going to add star backslash into it. 199 00:17:26,090 --> 00:17:34,220 And you can see it's put a little right or left arrow in here, which is just the symbol for a new line. 200 00:17:36,740 --> 00:17:45,860 So we got to star in a new line and then we get back up here, our eye is now going to be one and our 201 00:17:45,860 --> 00:17:52,190 Jay is going to get the value of one and we're going to add a space to it. 202 00:17:53,810 --> 00:18:03,920 So you can see in our line stringers of space and Jay has the value now to and that is no longer less 203 00:18:03,920 --> 00:18:10,760 than three times to, which is also to just kind of exit and add the star new line to it. 204 00:18:11,420 --> 00:18:16,690 So you can see over here now we have our star new line, Space Star, New Line. 205 00:18:17,900 --> 00:18:19,100 So we continue on. 206 00:18:23,480 --> 00:18:32,600 And we had a space, and this time because our eye is three, we can go one more in another space. 207 00:18:36,080 --> 00:18:39,940 And our star, so you see it's adding one more space each time. 208 00:18:46,430 --> 00:18:54,710 So now it's added more spaces and I'm just going to let it go and click over the console and you can 209 00:18:54,710 --> 00:18:56,570 see we to our diagonal line. 210 00:18:57,230 --> 00:19:01,220 The final thing I want to do with this program is a lot to be executed more than once. 211 00:19:02,280 --> 00:19:04,710 And of course, that's perfect for looping. 212 00:19:06,590 --> 00:19:12,440 So in here, we want to start with the concept that clear always and we know we want to do this at least 213 00:19:12,440 --> 00:19:13,000 once. 214 00:19:13,700 --> 00:19:19,310 So we start with the do, we're just going to say do left curly brace. 215 00:19:20,090 --> 00:19:21,230 So we do it once. 216 00:19:22,160 --> 00:19:31,190 And then down here we're going to say a while and we have to put a condition in there and we have to 217 00:19:31,190 --> 00:19:32,840 finish this with a curly brace as well. 218 00:19:33,920 --> 00:19:37,630 Let's do a reformat so we have to have a wall down here. 219 00:19:37,650 --> 00:19:39,270 The wall does end with a semicolon. 220 00:19:39,980 --> 00:19:41,080 So what's the while? 221 00:19:41,120 --> 00:19:43,130 Well, the wall is we're just going to prompt the user. 222 00:19:44,000 --> 00:19:48,180 We're going to say, let me see. 223 00:19:48,200 --> 00:19:50,300 Twenty four continue equals 224 00:19:52,980 --> 00:19:53,870 confirm 225 00:19:57,020 --> 00:20:00,830 draw another line question mark 226 00:20:03,380 --> 00:20:06,100 and got this one line to load. 227 00:20:06,110 --> 00:20:08,870 Can't be between the wall and the curly brace. 228 00:20:13,230 --> 00:20:19,530 And we're going to have a problem of continuing here because of scoping our continue will not be in 229 00:20:19,530 --> 00:20:22,080 scope, so we're going to find it outside of the loop. 230 00:20:24,630 --> 00:20:32,700 So I'm going to go up here outside the loop and I'm going to say let's continue equals false. 231 00:20:36,930 --> 00:20:43,800 And we'll put this in here, and so this is just going to allow us to keep drawing lines while they 232 00:20:43,800 --> 00:20:45,060 confirm that they want to do it. 233 00:20:47,670 --> 00:21:00,060 So let's go back to our program run hello world with Llamosa Horizontal or say 55 and draws our line 234 00:21:00,060 --> 00:21:00,840 55. 235 00:21:01,320 --> 00:21:02,100 Draw another line. 236 00:21:02,350 --> 00:21:02,720 Sure. 237 00:21:04,470 --> 00:21:10,530 Still vertical and we'll say 22 on the line. 238 00:21:10,560 --> 00:21:14,090 OK, and in this case, say diagonal. 239 00:21:14,970 --> 00:21:20,670 And I'm going to go back to our source and end this breakpoints. 240 00:21:20,880 --> 00:21:21,630 So I'm just going to. 241 00:21:21,630 --> 00:21:21,800 Right. 242 00:21:21,810 --> 00:21:23,910 Click on it and say remove all breakpoints. 243 00:21:25,920 --> 00:21:27,450 And back to the council. 244 00:21:28,290 --> 00:21:34,890 How long do you want your line was, say, 22 as well and just cross our diagonal line, 22 line. 245 00:21:39,270 --> 00:21:45,120 OK, well, that's our introduction to looping and how to do things multiple times, whether it's with 246 00:21:45,120 --> 00:21:48,150 a for loop a while loop or do while loop.