1 00:00:00,600 --> 00:00:11,150 Hello and welcome to this late show on for loops a for loop is basically a repetition control structure 2 00:00:13,780 --> 00:00:20,950 so there are times you may want setting codes to run repeatedly within an application. 3 00:00:20,950 --> 00:00:25,340 For example you can have an application that's called several arrays. 4 00:00:25,360 --> 00:00:34,060 You may want the number you may want a mechanism or a structure that all makes setting codes execute 5 00:00:34,480 --> 00:00:40,580 by iterating true the arrays or some parts of the application. 6 00:00:40,690 --> 00:00:46,460 So the for loop can be used as that kind of repetition control structure. 7 00:00:47,010 --> 00:00:47,500 OK. 8 00:00:47,860 --> 00:00:58,600 So with the for loop you can specify how many times you want a piece of code or a loop to execute. 9 00:00:58,610 --> 00:01:07,750 You can specify the number of times you want the loop to execute the syntax syntax basically refers 10 00:01:07,750 --> 00:01:12,260 to the structure of a loop how you define the loop. 11 00:01:12,280 --> 00:01:15,410 So this is how you basically to find the loop. 12 00:01:15,670 --> 00:01:22,190 You start with the word for and then he takes in three parameters. 13 00:01:22,200 --> 00:01:26,760 So this is basically the control flow of the loop. 14 00:01:26,800 --> 00:01:31,100 So you've got the first condition which is the initial. 15 00:01:31,720 --> 00:01:34,110 And then you've got the condition. 16 00:01:34,120 --> 00:01:44,110 This in this one is this step is executed first and only once then you've got the condition. 17 00:01:44,110 --> 00:01:44,800 All right. 18 00:01:44,800 --> 00:01:53,050 The condition is evaluated if it is true the body of the loop is executed if it is false the body of 19 00:01:53,050 --> 00:02:00,550 the loop does not execute and the flow control jumps to the next statement after that loop. 20 00:02:00,760 --> 00:02:09,970 This the increment after the body of the after the body for the for loop was that is executed. 21 00:02:09,970 --> 00:02:19,120 The increment the flow control jumps back to the increment statement and then allows you to update any 22 00:02:19,120 --> 00:02:27,160 loop control within that with any control variable within the loop some someone to illustrate this with 23 00:02:27,160 --> 00:02:28,420 a. 24 00:02:28,450 --> 00:02:32,980 So that makes more sense. 25 00:02:32,980 --> 00:02:39,460 So basically you specify a loop like that and then whatever statement or block of course you need to 26 00:02:39,460 --> 00:02:43,300 write goes in between this curly braces. 27 00:02:43,300 --> 00:02:55,010 So let me illustrate that this is a flow diagram of how the loop process works. 28 00:02:55,030 --> 00:03:03,580 So this is the syntax here for the loop and this is where the conditional statement goes. 29 00:03:03,580 --> 00:03:09,760 So basically the first first step in the loop is the inmate this inmate here. 30 00:03:10,570 --> 00:03:19,870 So this step this step here is executed first and only once. 31 00:03:19,870 --> 00:03:28,570 So this step allows you to declare and initialize any loop control variables you are not required to 32 00:03:28,570 --> 00:03:30,100 put a statement here. 33 00:03:30,100 --> 00:03:32,880 You don't need to put any statement in this part. 34 00:03:33,040 --> 00:03:33,580 OK. 35 00:03:33,730 --> 00:03:36,980 As long as the semicolon appears OK. 36 00:03:37,000 --> 00:03:41,830 This is the semicolon is longer the semicolon appears that you don't need to pull any. 37 00:03:41,830 --> 00:03:42,180 All right. 38 00:03:42,190 --> 00:03:45,550 So the next is the condition which is this condition here. 39 00:03:45,790 --> 00:03:46,750 That's the condition. 40 00:03:47,350 --> 00:03:57,610 So if the condition set is true it will execute this code block all of our conditional state code you've 41 00:03:57,610 --> 00:04:04,390 written here that code will execute if the condition is false. 42 00:04:04,390 --> 00:04:09,130 The code will execute here. 43 00:04:09,370 --> 00:04:09,780 All right. 44 00:04:09,810 --> 00:04:13,780 So if the condition set once the. 45 00:04:13,900 --> 00:04:21,970 The code has been evaluated if the conditions set is true the body of the loop which is this code block 46 00:04:21,970 --> 00:04:30,370 here the body of the loop is executed if it is false the body of the loop does not execute and the flow 47 00:04:30,370 --> 00:04:35,130 control jumps to the next statement after the loop. 48 00:04:35,660 --> 00:04:36,060 OK. 49 00:04:36,100 --> 00:04:42,940 So it would jump to the next statement after the Look if you've got several statements set up after 50 00:04:42,940 --> 00:04:52,240 the body for the loop executes the flow control jumps back up to the increments statement which is the 51 00:04:52,240 --> 00:04:58,330 statement yet this is increment it jumps back up to the increment statement this statement allows you 52 00:04:58,330 --> 00:05:01,470 to update any loop control variables. 53 00:05:01,570 --> 00:05:09,820 So this statement can be left blank as long as a semicolon appears after the condition this condition. 54 00:05:10,250 --> 00:05:11,020 All right. 55 00:05:11,050 --> 00:05:19,510 So the condition is now evaluated again and if it is true the loop will execute and the process repeats 56 00:05:19,510 --> 00:05:20,490 itself. 57 00:05:20,650 --> 00:05:22,960 Body of the loop then increment. 58 00:05:23,020 --> 00:05:24,300 That is the body of the loop. 59 00:05:24,310 --> 00:05:24,830 Execute. 60 00:05:24,850 --> 00:05:25,980 Then the increment. 61 00:05:26,670 --> 00:05:28,060 All right. 62 00:05:28,060 --> 00:05:31,860 And this process keeps on going again. 63 00:05:31,870 --> 00:05:39,400 Once the condition is tested if it's true the loop continues until the condition becomes false and then 64 00:05:39,400 --> 00:05:41,440 the loop terminates. 65 00:05:41,440 --> 00:05:41,930 All right. 66 00:05:41,950 --> 00:05:44,750 That's basically how a loop works. 67 00:05:44,800 --> 00:05:55,520 So let's try and implement that quickly if we can I have created a basic example here of the for loop 68 00:05:55,970 --> 00:05:59,580 so what I've done have created a console app. 69 00:05:59,820 --> 00:06:12,190 So the way you create a console log basically you go to file new projects make your C sharp is selected 70 00:06:12,280 --> 00:06:18,580 and then select console application and then give your console a name and then you click Okay. 71 00:06:18,610 --> 00:06:23,310 Once you click Okay this code here will be generated for you. 72 00:06:23,320 --> 00:06:26,110 This bit will be generated. 73 00:06:26,110 --> 00:06:30,260 The only code I have written is this code here. 74 00:06:30,290 --> 00:06:36,190 One highlighter so the other the other parts of the code will be auto generated. 75 00:06:36,220 --> 00:06:45,820 So between this code here this opening one here and this one here is where you write your code. 76 00:06:45,850 --> 00:06:49,980 So this is where I have written my code 77 00:06:53,850 --> 00:06:57,300 between this two curly braces. 78 00:06:57,300 --> 00:06:59,680 This one this one. 79 00:06:59,970 --> 00:07:02,730 Let me explain the code to you here. 80 00:07:02,760 --> 00:07:09,560 So with a for loop there are three parts of the code. 81 00:07:09,630 --> 00:07:14,240 The first part of the code is the initialization where you initialize it. 82 00:07:14,280 --> 00:07:15,660 So that's it here. 83 00:07:15,730 --> 00:07:21,020 I've created a variable called X. given its value of 17 so this is the first part. 84 00:07:21,030 --> 00:07:28,890 The initialization second part is a condition I've set I've set a condition here saying as long this 85 00:07:28,950 --> 00:07:32,130 I want the code to lose value. 86 00:07:32,130 --> 00:07:37,820 This code to execute as long as X is less than twenty seven. 87 00:07:37,830 --> 00:07:39,640 So that's the condition. 88 00:07:39,750 --> 00:07:41,910 And then here is the increment. 89 00:07:41,940 --> 00:07:52,650 So I want X to increase by 1 each time until it gets to a value where it is less than twenty seven. 90 00:07:52,680 --> 00:07:56,390 So this is the code here that will execute. 91 00:07:56,580 --> 00:08:04,200 So it will display on the screen it will show this text the value of x. 92 00:08:04,200 --> 00:08:08,400 It says This here is the initialize. 93 00:08:08,430 --> 00:08:13,230 So the first value will be zero before the counting starts. 94 00:08:13,230 --> 00:08:20,660 So once this code is executed this value will not change until it gets to 26. 95 00:08:20,670 --> 00:08:25,370 Remember the condition here is that X has to be less than twenty seven. 96 00:08:25,380 --> 00:08:36,700 So we will expect the value to be displayed each number to be displayed on the screen from 17 to 26. 97 00:08:36,780 --> 00:08:38,100 OK. 98 00:08:38,140 --> 00:08:44,880 And this console the red line is basically code basically to enable the code that you execute to stay 99 00:08:44,880 --> 00:08:45,510 on the screen. 100 00:08:45,540 --> 00:08:46,830 So let's check that out. 101 00:08:46,950 --> 00:08:54,090 So we'll click on Start and as you can see here the code is executing. 102 00:08:54,600 --> 00:08:58,480 So the condition here is that E2 it would do. 103 00:08:58,500 --> 00:09:05,580 Each would keep displaying the numbers of the value of X on till it gets to 26. 104 00:09:05,580 --> 00:09:11,100 So as long as this is less than twenty seven so twenty six would be the highest. 105 00:09:11,370 --> 00:09:18,660 So that means we could keep executing seventeen sixteen and so on until it gets to twenty six. 106 00:09:18,660 --> 00:09:23,260 So that's basically how you implement a for loop. 107 00:09:23,330 --> 00:09:24,050 All right. 108 00:09:24,210 --> 00:09:26,430 I hope it has made sense if it hasn't. 109 00:09:26,430 --> 00:09:32,440 Please feel free to contact me or be more than happy to explain more. 110 00:09:32,460 --> 00:09:33,050 Take care. 111 00:09:33,330 --> 00:09:34,020 Bye for now.