1 00:00:01,050 --> 00:00:05,850 In this video we'll be looking at repetition statements in C sharp. 2 00:00:05,850 --> 00:00:12,450 I have already created the project and this time the project's name is repetition statements and I have 3 00:00:12,450 --> 00:00:18,060 already set up the comments to guide what we will be looking at. 4 00:00:18,090 --> 00:00:22,860 You may pause a video here and go ahead and create the project and set up your code file to look at 5 00:00:22,860 --> 00:00:24,290 mine. 6 00:00:24,320 --> 00:00:27,530 Now we've already reviewed two cons control structures. 7 00:00:27,540 --> 00:00:31,650 We looked at sequence where that is the order in which the code will execute. 8 00:00:31,650 --> 00:00:35,520 So from top to bottom the way you write it the way it will be executed. 9 00:00:35,550 --> 00:00:42,150 Then we looked at selection where we get to a situation where we need to compare two values and then 10 00:00:42,180 --> 00:00:46,470 take an action based on the outcome of that comparison. 11 00:00:46,620 --> 00:00:48,970 And that selection more making a decision. 12 00:00:48,990 --> 00:00:55,100 So no we're going to look at how we repeat an action without having to type the line more than once. 13 00:00:55,110 --> 00:01:00,660 So in our previous example where we were looking at selection and I'm just going to go over to my condition 14 00:01:00,660 --> 00:01:03,770 statements project and click on that program not see us. 15 00:01:03,930 --> 00:01:07,530 We printed the same line four times. 16 00:01:07,530 --> 00:01:10,410 We were literally wrote this line four times. 17 00:01:10,440 --> 00:01:16,590 Now there is a way that I can repeat this action without needing to repeat the line because no if I 18 00:01:16,590 --> 00:01:21,480 wanted to change the sentence then I have to manually go in and change it for different times whereas 19 00:01:21,480 --> 00:01:28,350 if I could repeat printing this sentence four times without changing the sentence or rather repeated 20 00:01:28,350 --> 00:01:29,130 four times. 21 00:01:29,130 --> 00:01:33,660 But then I need to change a sentence then I would only need to change a sentence once but it would still 22 00:01:33,660 --> 00:01:34,560 go four times. 23 00:01:34,770 --> 00:01:40,740 So that's the advantage that loops bring to us the ability to repeat an action. 24 00:01:40,740 --> 00:01:44,540 So you see that I have outlined three types of rules that we'll be looking at today. 25 00:01:44,550 --> 00:01:50,490 There there is a fourth and I think there is also a fifth but then for no I'm just looking at these 26 00:01:50,490 --> 00:01:54,960 three because these three are mainstays and they're in every single language that you'll ever do. 27 00:01:55,470 --> 00:02:00,210 And the other one kind of has special applications which we'll be looking at later on. 28 00:02:00,210 --> 00:02:04,230 So the for loop is what we call a counter control loop. 29 00:02:04,230 --> 00:02:10,520 So counter controlled meaning I am setting the number of times that I want this repetition for. 30 00:02:10,530 --> 00:02:17,250 So if I wanted something to happen four or five times then I can specify that I want to type in five 31 00:02:17,250 --> 00:02:20,130 times no fewer and no more. 32 00:02:20,640 --> 00:02:23,180 So let us look at the syntax for a for loop. 33 00:02:23,430 --> 00:02:30,750 So it starts off with the keyword for and then we open parentheses and then inside of the parentheses 34 00:02:30,750 --> 00:02:36,360 we have a few things that we need to do one we need to define what we call the counter so a counter 35 00:02:36,660 --> 00:02:40,720 is going to keep track of the number of times it is being executed. 36 00:02:40,770 --> 00:02:43,790 So we need to me at the column to have a base value. 37 00:02:43,800 --> 00:02:47,350 And I'm just going to say I I short for iteration does that really matter. 38 00:02:47,370 --> 00:02:50,520 You're going to variable can have any name you would call it count. 39 00:02:50,670 --> 00:02:52,910 But just make sure you have a variable there. 40 00:02:52,920 --> 00:02:57,290 So I'm calling it I so into I is equal to zero. 41 00:02:57,540 --> 00:03:04,910 So I'm initializing my counter to something that is very basic very low in value and I'm starting at 42 00:03:04,920 --> 00:03:05,810 zero. 43 00:03:05,970 --> 00:03:15,540 Then I have a semicolon then I see I less then and then I'm going to give it the value that it should 44 00:03:15,540 --> 00:03:17,220 always run to be less done. 45 00:03:17,220 --> 00:03:24,900 So here's where it can get tricky and I just I'm going to try my best to explain it so that you understand 46 00:03:24,900 --> 00:03:26,510 why it's going to be written this way. 47 00:03:26,520 --> 00:03:27,750 So I would just say I. 48 00:03:28,110 --> 00:03:35,910 Which is going to have an initial value of zero less than five and then semicolon again and then I close 49 00:03:35,910 --> 00:03:37,030 plus. 50 00:03:37,190 --> 00:03:39,000 Now let me explain what's happening here. 51 00:03:39,000 --> 00:03:43,590 So I have a call center and I'm initializing it to zero. 52 00:03:43,590 --> 00:03:50,520 Then I have a condition where I'm seeing repeat as long as this condition is what as long as it's met 53 00:03:50,730 --> 00:03:53,040 then continue to repeat yourself. 54 00:03:53,040 --> 00:03:59,160 And then I have this section which is the incrementing seeing that the counter variable is counting 55 00:03:59,250 --> 00:04:00,860 how many times it runs. 56 00:04:00,870 --> 00:04:08,090 So I wanted to run five times on the first thrown it will have a value 0 0 then it will do the comparisons 57 00:04:08,090 --> 00:04:09,910 is 0 is less than 5. 58 00:04:09,960 --> 00:04:13,650 Okay then it runs and then it's increments. 59 00:04:13,650 --> 00:04:19,130 So no I is 1 then it compares is 1 less than 5. 60 00:04:19,170 --> 00:04:25,980 It is then it runs again and then it will do that and do that until we get to I being equal to five 61 00:04:26,450 --> 00:04:31,220 and then it will take a five is less than five which we all know is not true. 62 00:04:31,220 --> 00:04:34,140 Five is not less than five five is equal to five. 63 00:04:34,140 --> 00:04:37,060 So then it will exit the loop. 64 00:04:37,080 --> 00:04:38,650 Now you're probably wondering okay. 65 00:04:38,700 --> 00:04:42,750 So why the zero and why compared to let's say less than five. 66 00:04:42,750 --> 00:04:45,090 Well if you put your hand all right. 67 00:04:45,090 --> 00:04:48,110 No and do the math and you count zero. 68 00:04:48,120 --> 00:04:49,470 That's one finger. 69 00:04:49,560 --> 00:04:52,680 One two three four. 70 00:04:52,770 --> 00:04:56,250 Then you would have counted all five fingers on your hand. 71 00:04:56,280 --> 00:05:02,970 So it's typical practice to actually start off your column to zero and then just let it that the com 72 00:05:02,960 --> 00:05:08,070 pirates are on it until it is less than the number that you wish to stop at. 73 00:05:08,100 --> 00:05:08,670 Right. 74 00:05:08,670 --> 00:05:13,770 The alternative would be if I started this at one then I would have to change my condition to see that 75 00:05:13,830 --> 00:05:17,400 I should be less than or equal to 5. 76 00:05:17,430 --> 00:05:19,890 So we would have looked at the less than or equal. 77 00:05:19,950 --> 00:05:23,200 Very very briefly in the selection section. 78 00:05:23,250 --> 00:05:29,040 But what I'm trying to point out is that typically if you look at any other example if you pause this 79 00:05:29,040 --> 00:05:35,970 video and go and google a for loop example you're going to see more often than not that the initialize 80 00:05:35,970 --> 00:05:41,520 the contour to zero and then they made the condition run until it is less than the intended number of 81 00:05:41,790 --> 00:05:42,930 executions. 82 00:05:42,990 --> 00:05:48,870 So it is zero then turns to one that it turns into it and it turns to three then it turns the four at 83 00:05:49,140 --> 00:05:53,700 the point it turns the four the loop would have executed five times. 84 00:05:53,700 --> 00:05:58,290 So when it becomes five it does this comparison I realize that this is not violated. 85 00:05:58,290 --> 00:06:00,960 And then it will exit the loop. 86 00:06:00,960 --> 00:06:03,740 So that just part one off writing are for loop. 87 00:06:03,990 --> 00:06:09,490 So then after we do that then we need our braces so you notice that curly braces are like a mean state. 88 00:06:09,480 --> 00:06:12,420 Functions need them if statements need them. 89 00:06:12,540 --> 00:06:14,520 I know we see that loops need them. 90 00:06:14,540 --> 00:06:14,860 Right. 91 00:06:15,120 --> 00:06:21,840 So whatever we write inside of the curly braces would constitute the action that is being done each 92 00:06:21,840 --> 00:06:22,210 time. 93 00:06:22,210 --> 00:06:28,920 So let's just say I want it to print I am a winner five times so I want to say console Dot. 94 00:06:29,040 --> 00:06:29,530 Right. 95 00:06:29,520 --> 00:06:37,050 Line right line and then that takes them writing to the screen is I. 96 00:06:37,500 --> 00:06:43,650 Winner with an exclamation sign in case the all caps wasn't clear enough. 97 00:06:43,680 --> 00:06:50,350 All right so I am a winner and I want that printed five times to the screen so remember that when in 98 00:06:50,370 --> 00:06:55,740 the early example I did it manually by writing the line multiple times. 99 00:06:55,740 --> 00:07:03,270 This time I'm writing it once but I wanted to print out five times so I'm going to just switch my starter 100 00:07:03,270 --> 00:07:08,730 project quickly to repetition statements and then we're going to look at this and see if how many times 101 00:07:08,730 --> 00:07:09,640 is going to print. 102 00:07:10,060 --> 00:07:16,530 All right so then we see I am a winner being printed five times so this was the time he got printed 103 00:07:16,530 --> 00:07:19,990 while I was 0 and then it went around. 104 00:07:20,020 --> 00:07:22,340 So did the action then it hit the current to brace. 105 00:07:22,350 --> 00:07:25,420 Then he came back to 1 increments. 106 00:07:25,440 --> 00:07:32,340 So he did and I plus plus it incremented from zero to one and then he did the comparison to see is I 107 00:07:32,520 --> 00:07:33,830 still less than five. 108 00:07:33,840 --> 00:07:39,230 It was so rare and again and then it did the same thing came back incremented. 109 00:07:39,240 --> 00:07:40,790 So no I was too. 110 00:07:40,830 --> 00:07:42,390 And then that comparison happened. 111 00:07:42,400 --> 00:07:47,820 I realize it was less than five and then he came back and then it went to three and then it went to 112 00:07:47,820 --> 00:07:48,600 four. 113 00:07:48,690 --> 00:07:54,350 And then when it hit the four and then it incremented and no I had a value of five. 114 00:07:54,390 --> 00:07:58,380 It did the comparison and five is not less than five. 115 00:07:58,410 --> 00:08:01,730 So this this condition was not violated. 116 00:08:01,770 --> 00:08:05,090 So it came back to the curly brace and then exited. 117 00:08:05,160 --> 00:08:12,940 So whatever else would've been on the here and let's just say I said to console that right line. 118 00:08:13,080 --> 00:08:17,310 And then this is a nice cool way to get like some space. 119 00:08:17,400 --> 00:08:20,920 So if you want like a blank space between two lines you know a console. 120 00:08:20,940 --> 00:08:21,210 Right. 121 00:08:21,220 --> 00:08:23,420 Line I wanted empty space again just a console. 122 00:08:23,430 --> 00:08:23,920 Right. 123 00:08:23,920 --> 00:08:26,800 Line and it gives you a blank space right. 124 00:08:26,850 --> 00:08:33,120 And I just duplicated by pressing control D so just by being on the line control D and I will duplicate 125 00:08:33,120 --> 00:08:38,580 the line and that I'm going to see for loop finished. 126 00:08:38,740 --> 00:08:46,340 All right so let's try that again somewhere to execute this one more time and then here we see that 127 00:08:46,340 --> 00:08:51,020 it executed everything and then when it exited it continued to execute the rest of the court. 128 00:08:51,020 --> 00:08:52,700 So that's once again sequence. 129 00:08:52,700 --> 00:08:59,030 So it won't move on until all of what it needed to do inside the loop was done which was to print this 130 00:08:59,030 --> 00:09:03,800 five times to run five times and do whatever I told it to do four or five times. 131 00:09:03,800 --> 00:09:07,300 And then once it finished it went on to print the blank space. 132 00:09:07,310 --> 00:09:12,430 That's a empty console the right line and then print for loop finished. 133 00:09:12,440 --> 00:09:17,330 Now I'm going to do a little modification to what those inside a for loop I'm went to print another 134 00:09:17,330 --> 00:09:22,700 line so remember that as many commands as you want to repeat we can put as long as they're inside the 135 00:09:22,700 --> 00:09:25,810 tool brace someone to console that right line. 136 00:09:25,910 --> 00:09:33,260 And this time I want to print the value off the the incrementing variable or the column to write. 137 00:09:33,260 --> 00:09:38,780 So this time I'm going to see a counter and I'm just going to put a call on and then I'm going to use 138 00:09:38,780 --> 00:09:42,470 my interpolation and then plug in that value. 139 00:09:42,470 --> 00:09:43,940 So I'm just going to see I. 140 00:09:44,330 --> 00:09:44,600 All right. 141 00:09:44,600 --> 00:09:50,390 So each time that the loop runs whatever value I has I am going to be printing it to the screen just 142 00:09:50,390 --> 00:09:54,650 so you can see that it starts off with zero and goes up to four. 143 00:09:54,740 --> 00:09:56,540 But it would ever run five times. 144 00:09:56,540 --> 00:09:56,850 All right. 145 00:09:57,020 --> 00:09:59,330 So let me try that again. 146 00:09:59,350 --> 00:09:59,770 All right. 147 00:09:59,950 --> 00:10:05,260 So now we see I am a winner and a calendar value is zero then I am a winner again. 148 00:10:05,260 --> 00:10:08,940 Clinton value goes to 1 then to two then to three then to four. 149 00:10:08,980 --> 00:10:14,850 So zero one two three four equals five executions. 150 00:10:14,850 --> 00:10:15,130 All right. 151 00:10:15,310 --> 00:10:20,230 So it's good practice to understand why we start to zero because when we get to arrays and collections 152 00:10:20,440 --> 00:10:26,880 then you're going to understand that the computer actually starts counting from zero in as human beings 153 00:10:26,880 --> 00:10:32,160 we tend to ignore zero and we start counting out one sure but the computer starts counting at zero. 154 00:10:32,170 --> 00:10:36,910 So it's good to appreciate why we would start counting from zero. 155 00:10:36,910 --> 00:10:40,480 That being said I didn't necessarily have to start from zero. 156 00:10:40,480 --> 00:10:43,260 I could have started from 50 encode to 55. 157 00:10:43,350 --> 00:10:46,560 And so I could have said the Coalition should start off as fifty five. 158 00:10:46,660 --> 00:10:50,550 And it should execute until it hits 80. 159 00:10:50,560 --> 00:10:50,950 All right. 160 00:10:50,950 --> 00:10:57,580 So whatever number of times it's going to go between 55 and 80 or if you do the math and say 80 minus 161 00:10:57,580 --> 00:11:01,860 fifty five whatever number you get that's the number of times it's going to execute. 162 00:11:01,870 --> 00:11:02,190 All right. 163 00:11:02,200 --> 00:11:08,770 So if I just do that quickly the end result of that is this line I am a winner and the columns are being 164 00:11:08,770 --> 00:11:15,910 printed from fifty five which is a valued started with up on to seven to nine which means that when 165 00:11:15,910 --> 00:11:21,760 it hit 80 it violated this condition and jumped out and that's pretty much it for for loop. 166 00:11:21,760 --> 00:11:25,810 So this is a very simple example but I hope that you appreciate it. 167 00:11:25,990 --> 00:11:31,300 So I'm just going to reset this code so that when you look at the example you see it and I'm just going 168 00:11:31,300 --> 00:11:39,270 to add sample printing five times all right. 169 00:11:39,320 --> 00:11:43,970 So now we have some comments to guide what this is doing. 170 00:11:44,000 --> 00:11:44,300 All right. 171 00:11:44,300 --> 00:11:45,740 So let's move on to the wild. 172 00:11:45,800 --> 00:11:51,290 So the while loop is what we call a condition control loop and then I appended that with the expression 173 00:11:51,380 --> 00:11:52,340 free chick. 174 00:11:52,400 --> 00:11:57,920 So when we do the do while then you'll see what the pre vs. the post really mean. 175 00:11:58,340 --> 00:12:04,040 But right now I'm just going to focus on the condition control so the condition control portion of it 176 00:12:04,040 --> 00:12:11,640 means that it is going to continue executing until a condition is violated or it will continue to execute 177 00:12:11,870 --> 00:12:14,060 while a condition holds true. 178 00:12:14,060 --> 00:12:19,400 So with our for loop it was called into our control which means that we're just executing for a finite 179 00:12:19,400 --> 00:12:25,220 number of times because we know we wanted to do five times so we specify or we rotate in a way to make 180 00:12:25,220 --> 00:12:27,650 sure that it doesn't exceed five times. 181 00:12:27,650 --> 00:12:34,370 However if we want something to go on and on and on and on until a particular condition is met or are 182 00:12:34,370 --> 00:12:38,180 violated then we would use something like a while loop. 183 00:12:38,570 --> 00:12:46,760 So the thing is know that with a while loop you can run into what we call an in finite loop where if 184 00:12:46,760 --> 00:12:53,020 you write the code for the while loop and condition never gets violated it will never exit that loop. 185 00:12:53,150 --> 00:12:56,450 And so it will run on and on and on. 186 00:12:56,480 --> 00:12:56,730 Right. 187 00:12:56,740 --> 00:13:01,520 As long as a computer is on and whatever conditions are required for it to be running out there it will 188 00:13:01,520 --> 00:13:02,780 just continue to execute. 189 00:13:02,780 --> 00:13:09,530 So I would just start off by showing you an example of an in finite loop with the while loop Fred. 190 00:13:09,560 --> 00:13:16,220 So I want to declare a simple variable and call it and I give it to value 0 0 that I would to say a 191 00:13:16,220 --> 00:13:26,450 while n is equivalent to 0 and I noticed the syntax we have the keyword while we open the parentheses. 192 00:13:26,460 --> 00:13:30,410 And then we put in our condition just like we would have done with the if statement. 193 00:13:30,410 --> 00:13:30,690 Right. 194 00:13:30,690 --> 00:13:35,590 So the only difference between it and the statement is that instead of saying if we see a while. 195 00:13:35,610 --> 00:13:35,820 All right. 196 00:13:35,820 --> 00:13:42,800 So we see a while open bar indices put in the condition and then open curly braces with our actions 197 00:13:42,810 --> 00:13:43,140 right. 198 00:13:43,560 --> 00:13:48,440 So the way I've written this and is not going to change from zero. 199 00:13:48,450 --> 00:13:54,030 So this is going to say a while and has the value or is equivalent to the value zero. 200 00:13:54,030 --> 00:13:55,060 I want to do this. 201 00:13:55,070 --> 00:13:59,840 Someone to write two lines and just copy existing lines. 202 00:13:59,940 --> 00:14:06,890 Someone to print a blank line and then I'm going to see in while loop. 203 00:14:06,890 --> 00:14:07,560 All right. 204 00:14:07,590 --> 00:14:13,950 So while the loop is running it's going to print I would say in a while loop and then the blank line. 205 00:14:14,100 --> 00:14:22,110 So it's going to see in side to say inside while loop and we're printing a blank line after that. 206 00:14:22,200 --> 00:14:29,280 No I'm just going to comment this out quickly so that it doesn't get executed while we run this program 207 00:14:29,550 --> 00:14:34,110 because no I want to focus on the while loop and you're going to notice that it's just going to continuously 208 00:14:34,110 --> 00:14:35,190 print and never stop. 209 00:14:35,190 --> 00:14:36,780 So let me execute initially. 210 00:14:37,290 --> 00:14:38,430 And that's what it looks like. 211 00:14:38,430 --> 00:14:42,990 So yes he's just printing printing printing printing printing and seeing inside while loop and you can 212 00:14:42,990 --> 00:14:47,910 see that it can as a blank space you look at a scroll bar I see that it's just going on and on and on 213 00:14:47,910 --> 00:14:49,580 and on and on it's never stopping. 214 00:14:49,680 --> 00:14:55,030 And that's what do we call an infinite loop these infinite loops are known to break computers. 215 00:14:55,050 --> 00:14:58,920 So if your computer crashes who and you do this I'm sorry. 216 00:14:59,400 --> 00:15:04,660 But I have a slightly powerful machine so at least I know that this won't break my machine. 217 00:15:04,830 --> 00:15:11,670 But the fact is if you're right poor code in big applications and you get this or end up with this situation 218 00:15:11,710 --> 00:15:16,740 these situations are in finite loops can bring down systems. 219 00:15:16,740 --> 00:15:17,090 All right. 220 00:15:17,100 --> 00:15:22,290 So you want to make sure that you understand whole while loops work so that you don't make this mistake 221 00:15:22,320 --> 00:15:23,580 at any point. 222 00:15:23,610 --> 00:15:23,840 All right. 223 00:15:23,850 --> 00:15:30,720 So let's exit that and look at how we should write while loops to make the most of it or to take advantage 224 00:15:30,810 --> 00:15:34,710 of the condition why not leading to an infinite loop. 225 00:15:34,780 --> 00:15:35,280 No. 226 00:15:35,340 --> 00:15:39,840 Our condition is based on the fact that n is equivalent to zero. 227 00:15:39,870 --> 00:15:41,470 So it's going to run away. 228 00:15:41,740 --> 00:15:44,190 And sorry is equivalent to zero. 229 00:15:44,220 --> 00:15:52,830 So what if I change this and I said something like While n is less than five. 230 00:15:52,890 --> 00:15:53,500 All right. 231 00:15:53,550 --> 00:15:57,780 So while the value of end is less than five I wanted to do this. 232 00:15:57,780 --> 00:16:05,780 No based on the way it's written this will still be an infinite loop because any 0 0 is less than 5. 233 00:16:05,880 --> 00:16:08,710 And I'm not doing anything to change the value of n. 234 00:16:08,790 --> 00:16:12,960 And so it's going to always be less than five until the continent executes. 235 00:16:12,990 --> 00:16:21,660 So that means the one thing I can do to make sure that this gets violated eventually is to make sure 236 00:16:21,660 --> 00:16:27,990 that I am changing the value or putting something in place that the value changes at some point. 237 00:16:27,990 --> 00:16:36,280 And this comparison will get broken or this condition will be met or violated or other at some point. 238 00:16:36,300 --> 00:16:39,290 That is convenient to how we want our program to work. 239 00:16:39,360 --> 00:16:42,240 So I want this to print five times. 240 00:16:42,240 --> 00:16:47,010 Yes we accomplished something that that with a for loop but I'm just showing you that I can accomplish 241 00:16:47,010 --> 00:16:52,960 something that that with the while loop but the the path to doing so is going to be very different. 242 00:16:52,980 --> 00:17:00,840 So I need to know make sure that I am changing the value of an to be something that eventually will 243 00:17:00,960 --> 00:17:04,910 exceed a value that makes this condition true. 244 00:17:04,920 --> 00:17:11,070 So I'm just going to add a third line to inside my while loop and then I'm going to see an is equal 245 00:17:11,070 --> 00:17:17,340 to N close 5 or sorry n is equal to any plus 1. 246 00:17:17,350 --> 00:17:22,930 So when I say n is equal to N plus 1 remember we discussed that this could be shortened into just seeing 247 00:17:23,170 --> 00:17:25,560 n flows equal one. 248 00:17:25,570 --> 00:17:29,650 So no it's going to be adding 1 to itself each time. 249 00:17:29,650 --> 00:17:30,310 Right. 250 00:17:30,340 --> 00:17:38,380 So it's going not by a one until eventually it will execute and n is going to have a value that is not 251 00:17:38,380 --> 00:17:39,530 one to make. 252 00:17:39,530 --> 00:17:41,060 That's not one to be less than five. 253 00:17:41,110 --> 00:17:43,190 So our while loop will exit. 254 00:17:44,260 --> 00:17:44,930 So let's this. 255 00:17:44,930 --> 00:17:45,300 That's 0. 256 00:17:45,330 --> 00:17:47,290 So I mean execute. 257 00:17:47,500 --> 00:17:51,190 And here we see that's literally printed the right number of times. 258 00:17:51,220 --> 00:17:56,610 So just like with the for loop where we started off a zero and went to it was less than five. 259 00:17:56,620 --> 00:18:04,690 We started off with n being zero and then we're just checking on the condition that n is less than five. 260 00:18:04,690 --> 00:18:11,380 But there is nothing in the loop or in the structure of the loop that at least not naturally that will 261 00:18:11,530 --> 00:18:13,740 help us to change the value of. 262 00:18:13,750 --> 00:18:19,670 And so we had to do that manually by putting an accumulation so we'll call this an accumulator. 263 00:18:19,750 --> 00:18:25,500 When you make a variable increment by itself plus another value. 264 00:18:25,550 --> 00:18:25,900 Right. 265 00:18:25,930 --> 00:18:30,760 So when I said n equals N plus 1 that's making it an accumulator are plus equal. 266 00:18:30,760 --> 00:18:41,860 So now I'm accumulating value each time it runs and then eventually we'll be will meet this condition. 267 00:18:41,860 --> 00:18:48,940 Now this is a very simple and not very practical example I mean there are situations you may use this 268 00:18:49,150 --> 00:18:56,920 but a more useful way for using the while loop is to see compare user input and let that user input 269 00:18:56,920 --> 00:19:03,220 violate the condition and continue to do this until the user violates the condition and the loop will 270 00:19:03,280 --> 00:19:04,030 exit. 271 00:19:04,030 --> 00:19:04,320 All right. 272 00:19:04,600 --> 00:19:10,990 So so let's take your experience with an ABM for example on ABM will probably always ask you do you 273 00:19:10,990 --> 00:19:14,550 want to continue if you say yes it continues if it says no. 274 00:19:14,650 --> 00:19:18,550 And then it repeats the same steps on as long as you keep on seeing yes. 275 00:19:18,640 --> 00:19:19,240 All right. 276 00:19:19,240 --> 00:19:27,340 So literally all they have is really a while loop that is allowing you to do carry out operations and 277 00:19:27,340 --> 00:19:30,130 then you can indicate when you want it to stop. 278 00:19:30,160 --> 00:19:35,800 And then when you give it the condition that C is stopped then you will say OK that condition has been 279 00:19:35,800 --> 00:19:36,610 violated. 280 00:19:36,610 --> 00:19:37,630 Let me move on. 281 00:19:38,320 --> 00:19:44,710 So I'm going to modify this and I'm going to see console the right line and I want to tell the user 282 00:19:45,010 --> 00:19:49,880 input no all right. 283 00:19:49,900 --> 00:19:52,590 And I'm going to say zero to four. 284 00:19:52,630 --> 00:20:01,130 So I'm telling the user to input some number between 0 to 4 and then I'm going to do I read line where 285 00:20:01,130 --> 00:20:12,470 I'm going to see and is equal to convert to sorry spell converts wrong convert to in thirty two and 286 00:20:12,470 --> 00:20:22,620 then I'm going to say a console dot read line so I'm accepting user input and I'm not going to increment 287 00:20:22,650 --> 00:20:28,680 because I have the user determining the value of and each time right and I'm going to take oh this console 288 00:20:28,680 --> 00:20:37,050 that right line Well let me put it back and then I'm going to print the screen you entered and I want 289 00:20:37,050 --> 00:20:38,860 to see you enter that number. 290 00:20:38,880 --> 00:20:39,710 All right. 291 00:20:39,930 --> 00:20:44,000 So let me just put on my dollar sign interpolation. 292 00:20:44,100 --> 00:20:51,120 So what this loop is expected to do is that or it's going to from the user to enter a number if I enter 293 00:20:51,120 --> 00:20:57,750 0 then if I enter 0 then it will print to you entered 0 and then it's going to come back and it's going 294 00:20:57,750 --> 00:21:00,520 to check the condition is 0 less than 5. 295 00:21:00,810 --> 00:21:06,030 No it's not so it's going to see in turn number again and then it's going to continue doing that until 296 00:21:06,030 --> 00:21:15,240 I put in a number that clearly is not less than five or will clearly violate this condition. 297 00:21:15,240 --> 00:21:24,010 So let's try that so input a number and I put in three press Enter you In you entered three and then 298 00:21:24,010 --> 00:21:25,120 it's prompting me again. 299 00:21:25,120 --> 00:21:26,490 So I entered three. 300 00:21:26,590 --> 00:21:28,960 It printed that I entered three and then it came. 301 00:21:28,960 --> 00:21:33,910 Did the comparison realize that this condition is still true because three is less than five. 302 00:21:34,090 --> 00:21:36,610 So it went ahead and asked me for another number. 303 00:21:36,610 --> 00:21:38,770 If I put you in for it we'll do it again. 304 00:21:38,770 --> 00:21:46,600 If I put two in one if I put you negative five as long as a number is not let alone as the number is 305 00:21:46,600 --> 00:21:50,580 less than five then it will continue to do this operation. 306 00:21:50,650 --> 00:21:54,130 If I put in ten it will exit. 307 00:21:54,130 --> 00:21:57,880 All right so let me put in a proper statement to say it has exited. 308 00:21:57,970 --> 00:22:04,960 So I'm just taking this previously written one on commenting it I see keyboard chakras just make it 309 00:22:04,990 --> 00:22:06,130 easy to maneuver right. 310 00:22:06,160 --> 00:22:10,860 So while loop finished all right. 311 00:22:10,870 --> 00:22:12,210 So let's try that again. 312 00:22:12,880 --> 00:22:16,040 So we have zero to four output zero. 313 00:22:16,120 --> 00:22:17,920 I put five and then oil. 314 00:22:17,920 --> 00:22:18,190 OK. 315 00:22:18,220 --> 00:22:19,340 So I entered five. 316 00:22:19,340 --> 00:22:21,310 I did that prematurely but that's fine. 317 00:22:21,310 --> 00:22:25,760 So I entered zero and it just said OK zero is this and five from Tim again. 318 00:22:25,810 --> 00:22:26,940 Then I entered five. 319 00:22:26,950 --> 00:22:29,840 It came to the comparison five is not less than five. 320 00:22:29,890 --> 00:22:35,140 And so it just went down to the square to brace and then continued with the execution where it just 321 00:22:35,140 --> 00:22:37,000 said while loop finished. 322 00:22:37,210 --> 00:22:37,960 All right. 323 00:22:37,960 --> 00:22:44,260 So that's basically how we can use while lose because once again it's going to run against a condition 324 00:22:44,260 --> 00:22:51,490 and we want to make sure that this condition can get violated and legitimately so because if we don't 325 00:22:51,490 --> 00:22:57,610 do that then all the operations that the while loop is repeating will be repeated until that computer 326 00:22:57,610 --> 00:22:58,520 shuts off. 327 00:22:58,630 --> 00:23:04,240 And that can lead to lead to some buffer or a memory overload and all sorts of things that we don't 328 00:23:04,270 --> 00:23:04,690 want. 329 00:23:04,720 --> 00:23:12,460 So when the writing condition controls loops make sure that the condition can be violated at some point 330 00:23:12,490 --> 00:23:14,140 and legitimately so. 331 00:23:14,340 --> 00:23:20,260 Now we move on to our do while and I did say that I would mention the pre vs. post. 332 00:23:20,350 --> 00:23:25,480 So the reason we call The while loop the pre check is that is going to check this condition before it 333 00:23:25,540 --> 00:23:26,700 executes. 334 00:23:26,710 --> 00:23:34,840 So if I initialized n to have a value of five or let's say 10 then the first thing is going to do is 335 00:23:34,840 --> 00:23:36,900 check is this condition true. 336 00:23:36,910 --> 00:23:43,480 That's the pre check is the condition true is if any 10 and 10 is not less than 5 then nothing here 337 00:23:43,480 --> 00:23:46,850 will get done it will just move on to C while loop finished. 338 00:23:46,870 --> 00:23:51,560 So that's a pre check the post check will do it before. 339 00:23:51,580 --> 00:23:55,120 So it will do the action and then check the condition. 340 00:23:55,120 --> 00:23:55,730 Right. 341 00:23:55,750 --> 00:24:02,230 So let me just write the do while and I'm going to do the same operation inside to do. 342 00:24:02,590 --> 00:24:06,850 But then we'll see the difference in the execution. 343 00:24:06,850 --> 00:24:15,280 So the syntax for our do is the word do the or we open and close the curly braces and then on the close 344 00:24:15,280 --> 00:24:20,430 currently brace you have to see while and then you put in your condition check. 345 00:24:20,440 --> 00:24:22,870 So the wild part looks just like the regular while. 346 00:24:22,870 --> 00:24:26,570 So I'm just going to copy this and pasted down here it looks the same way right. 347 00:24:26,590 --> 00:24:32,110 So the only difference is that we see a while and then opened the curly brace the do while we say do 348 00:24:32,140 --> 00:24:38,230 this action do whatever is inside the curly braces and then check the condition afterwards. 349 00:24:38,230 --> 00:24:41,560 And then this one requires a semicolon at the end of it. 350 00:24:41,560 --> 00:24:46,690 So that's where it can get and can get kind of tricky because then you see that oh no semicolon and 351 00:24:46,700 --> 00:24:49,140 semicolon for this one on there and there. 352 00:24:49,150 --> 00:24:53,930 So reinvention is just practice that will make you get used to where to put the semicolons and where 353 00:24:54,040 --> 00:24:59,860 we're not to write and visual studio is always ready to give you that encouraging red line to remind 354 00:24:59,860 --> 00:25:02,240 you that hey there's something missing here. 355 00:25:02,260 --> 00:25:08,530 So then it's telling you semicolon is expected you put it there and all is well in the universe after 356 00:25:08,530 --> 00:25:09,060 that. 357 00:25:09,100 --> 00:25:15,670 So I'm just going to copy these actions quickly put inside the do and put that into space so we can 358 00:25:15,670 --> 00:25:24,500 see exactly what's happening and then output the statement that tells us when the do while loop is finished. 359 00:25:24,890 --> 00:25:28,210 So I'm not actually not want to come in this autumn winter to run it. 360 00:25:28,220 --> 00:25:31,200 So the value of n is 10. 361 00:25:31,400 --> 00:25:37,640 Remember that the pre check is going to check the condition if the condition is true then it will do 362 00:25:37,640 --> 00:25:45,050 what it's what what we told it to do and then it will come back and check the condition again if the 363 00:25:45,050 --> 00:25:48,860 condition is not true then it will not do anything. 364 00:25:48,890 --> 00:25:49,420 All right. 365 00:25:49,850 --> 00:25:55,220 Whereas with the door while it will do something and then check. 366 00:25:55,220 --> 00:26:02,750 So that means I have the opportunity in this one to change the value of n from 10 and then it can change 367 00:26:02,750 --> 00:26:09,410 whether you can violate that or it can make it true so it will just say oh this condition is true let's 368 00:26:09,410 --> 00:26:10,100 do it again. 369 00:26:10,130 --> 00:26:12,530 So we'll do it at least once. 370 00:26:12,530 --> 00:26:13,300 So let's try that. 371 00:26:13,310 --> 00:26:19,580 So I click Start I notice the first thing that's printed the screen is something to tell us to do the 372 00:26:19,580 --> 00:26:20,630 while loop is finished. 373 00:26:20,630 --> 00:26:26,210 So you know it just skipped the doorway as I said it just check the condition 10 less than five. 374 00:26:26,210 --> 00:26:26,940 That's false. 375 00:26:26,940 --> 00:26:27,250 Ah. 376 00:26:27,280 --> 00:26:28,580 Don't bother with that. 377 00:26:28,640 --> 00:26:32,810 Print the blank line and then give me the while loop finished. 378 00:26:32,900 --> 00:26:40,010 But then the door while is asking me for the number so it's doing the action and then it's giving me 379 00:26:40,010 --> 00:26:43,790 the opportunity to put in the number and then it will check the condition. 380 00:26:43,790 --> 00:26:44,340 All right. 381 00:26:44,390 --> 00:26:49,270 So if we want to do something at least once it's a repeatable thing well you want to make sure that 382 00:26:49,270 --> 00:26:51,760 it happens at least once. 383 00:26:51,860 --> 00:26:53,790 Then you want to use a do while. 384 00:26:53,840 --> 00:26:54,170 All right. 385 00:26:54,500 --> 00:27:00,980 So then I want to input a number and let's say I input three it will repeat. 386 00:27:01,010 --> 00:27:01,370 All right. 387 00:27:01,370 --> 00:27:06,590 So when I entered three on prompt then it hit the condition and three is less than five. 388 00:27:06,590 --> 00:27:11,860 So it said we'll do it again and then this on with to enter five or what I've said fifty six. 389 00:27:11,870 --> 00:27:12,740 That's fine. 390 00:27:12,740 --> 00:27:16,100 And then entered 56 which is clearly not less than five. 391 00:27:16,160 --> 00:27:21,180 And so it just went on to say while loop do while loop is finished. 392 00:27:21,200 --> 00:27:27,110 So that's the only difference between the while and the do while the do does it at least once and takes 393 00:27:27,110 --> 00:27:31,980 after whereas a while loop will do it will check before it does. 394 00:27:32,240 --> 00:27:37,370 And then we remember that our for loop and others on Comment is before we wrap this up. 395 00:27:37,670 --> 00:27:44,300 The for loop is set to run for how many times we want it to run. 396 00:27:44,360 --> 00:27:47,740 That's it for petition statements in C sharp. 397 00:27:47,940 --> 00:27:51,820 Remember to practice and think of scenarios where you may need them. 398 00:27:51,860 --> 00:27:54,500 We're going to be using them more in our project. 399 00:27:54,500 --> 00:27:57,110 But for know this is an introduction.