1 00:00:03,180 --> 00:00:06,300 Most programs will need a loop throughout some code. 2 00:00:06,720 --> 00:00:13,920 C plus plus provides a several ways to do this, either by iterating with an index value or testing 3 00:00:13,920 --> 00:00:15,360 a logical condition. 4 00:00:16,360 --> 00:00:21,880 There are two versions of the for statement, iteration and range based. 5 00:00:21,910 --> 00:00:28,810 The latter was introduced in C plus plus 11 and the iteration iteration version. 6 00:00:29,470 --> 00:00:34,780 In this lecture you're going to learn the iteration version of for statement. 7 00:00:34,810 --> 00:00:43,360 So in iteration version you're going to add firstly init expression, then condition. 8 00:00:45,710 --> 00:00:48,830 And then loop expression. 9 00:00:50,500 --> 00:00:53,140 And after that you're going to enter the code here. 10 00:00:53,140 --> 00:00:56,320 So this means loop statement. 11 00:00:58,440 --> 00:01:05,220 So you can provide one or more loop statements and for more than one statement you should provide a 12 00:01:05,220 --> 00:01:07,980 code block using braces here. 13 00:01:08,070 --> 00:01:15,660 So the purpose of the loop may be served by the loop expression, in which case you may not want a loop 14 00:01:15,660 --> 00:01:17,250 statement to be executed. 15 00:01:17,280 --> 00:01:23,580 Here you'll use the null statement, which means nothing here, as you can see. 16 00:01:23,580 --> 00:01:23,880 Here. 17 00:01:23,880 --> 00:01:24,390 Here. 18 00:01:24,980 --> 00:01:31,520 You can use just nothing here and you won't have the loop, uh, expression here. 19 00:01:31,520 --> 00:01:38,990 So within the parentheses are three expressions here by semicolons divided by separated by semicolons. 20 00:01:38,990 --> 00:01:39,290 Here. 21 00:01:39,500 --> 00:01:46,340 The first expression here allows you to declare an initialize a loop variable. 22 00:01:46,790 --> 00:01:55,160 This variable is scoped to the for statement, so you can only use it in the for expression or in the 23 00:01:55,160 --> 00:01:57,290 loop statement that follow here. 24 00:01:57,290 --> 00:02:05,960 So if you want more than one loop variable, you can declare them in this expression using the comma 25 00:02:05,990 --> 00:02:07,580 operator here in it. 26 00:02:07,580 --> 00:02:10,820 Two expression here, as you can see here. 27 00:02:11,540 --> 00:02:19,790 So the fourth statement here will loop while the condition expression, condition, expression is true. 28 00:02:19,790 --> 00:02:26,400 So if you are using a loop variable, you can use this expression to check the value of the loop variable. 29 00:02:26,400 --> 00:02:30,960 So the third expression is called at the end of the loop. 30 00:02:30,960 --> 00:02:38,010 So after the loop has been called, um, following this, this condition expression is called to see 31 00:02:38,010 --> 00:02:42,300 if the loop should continue here. 32 00:02:42,300 --> 00:02:49,260 So this final expression is often used to update the value of the loop variable here. 33 00:02:49,260 --> 00:02:54,870 So now let's create an example loop here for loop integer. 34 00:02:54,870 --> 00:02:56,490 We're going to assign integer. 35 00:02:56,970 --> 00:02:59,550 Integer E to zero. 36 00:02:59,550 --> 00:03:01,410 And the second is condition. 37 00:03:01,410 --> 00:03:05,400 As you know, while E is less than. 38 00:03:06,830 --> 00:03:08,510 Ten and. 39 00:03:10,910 --> 00:03:18,190 Increment e by one at the end of the every loop here and stdcout. 40 00:03:18,200 --> 00:03:26,150 So we have to include the standard library here, your stream input output stream and here. 41 00:03:27,050 --> 00:03:27,830 E here. 42 00:03:29,650 --> 00:03:30,370 Arrested. 43 00:03:30,430 --> 00:03:31,720 And now here. 44 00:03:34,820 --> 00:03:35,300 Here. 45 00:03:40,750 --> 00:03:43,800 As you can see here, the output is as follows here. 46 00:03:43,810 --> 00:03:48,760 So in this code, the loop variable is here. 47 00:03:49,450 --> 00:03:52,720 And it is initialized to zero. 48 00:03:57,130 --> 00:04:06,160 So next the condition is checked here and signs is less than ten. 49 00:04:06,190 --> 00:04:12,520 This statement will be executed and the statement is printing value to the console. 50 00:04:12,550 --> 00:04:14,040 Here, as you can see here. 51 00:04:14,050 --> 00:04:22,810 So the next action is the loop expression here, the plus plus E count, which increments the loop variable 52 00:04:22,810 --> 00:04:27,010 E, and then the condition is checked and so on. 53 00:04:27,010 --> 00:04:37,090 And since the condition E here is less than ten, this means that this loop will return ten times with 54 00:04:37,090 --> 00:04:41,920 the value of E between 0 and 9. 55 00:04:42,940 --> 00:04:45,880 So you will see this in console. 56 00:04:46,710 --> 00:04:50,100 So the loop expression can be any expression you like. 57 00:04:50,100 --> 00:04:57,870 So but often it increments or decrement the value so you don't have to change the loop variable value 58 00:04:57,870 --> 00:04:58,980 by one. 59 00:04:58,980 --> 00:05:08,850 For example, you can use e equals five as the loop expression to decrease the variable by five on each 60 00:05:08,850 --> 00:05:09,330 loop here. 61 00:05:09,330 --> 00:05:15,660 So the loop variable can be any type you like, so it doesn't have to be integer, it doesn't have to 62 00:05:15,660 --> 00:05:18,990 be numeric or anything here. 63 00:05:18,990 --> 00:05:24,600 And the condition and the loop expression do not have to use the loop variable here. 64 00:05:24,720 --> 00:05:28,680 So in fact you do not have to declare a loop variable at all. 65 00:05:28,860 --> 00:05:35,820 If you do not provide a loop condition, the loop will infinite unless you provide a check in the loop 66 00:05:35,820 --> 00:05:36,240 here. 67 00:05:36,240 --> 00:05:41,310 So, for example, let's delete this condition here and run our code here. 68 00:05:42,220 --> 00:05:45,640 As you can see here, we have infinite loop here. 69 00:05:45,670 --> 00:05:50,640 The loop won't stop and keeps on printing and printing. 70 00:05:50,650 --> 00:05:57,280 So if we write it here, if E equals 20. 71 00:05:58,300 --> 00:06:02,500 Then break our for loop here. 72 00:06:10,730 --> 00:06:11,180 Here. 73 00:06:13,460 --> 00:06:21,260 As you can see here, this loop started from five because E equals to five and keep incremented five 74 00:06:21,290 --> 00:06:22,100 by one. 75 00:06:22,100 --> 00:06:25,190 And at every loop this condition is checked. 76 00:06:25,190 --> 00:06:33,530 So first it was not equal to 20 and that's why the loop won't break here. 77 00:06:33,530 --> 00:06:43,550 And while the E equals to 20, the condition the if condition turned true and the the break statement 78 00:06:43,550 --> 00:06:44,900 executed here. 79 00:06:46,870 --> 00:06:49,030 So this uses the brick statement. 80 00:06:49,030 --> 00:06:52,810 So as you know, we introduced the brick statement in the previous lecture. 81 00:06:52,810 --> 00:06:55,750 So with the switch statement, as you know. 82 00:06:55,750 --> 00:07:02,390 So and you can also use the return go to or trough, you will rarely see statement that finishes the 83 00:07:02,470 --> 00:07:03,850 using go to here. 84 00:07:03,850 --> 00:07:13,060 But however you may see this code here for example for here and code. 85 00:07:14,950 --> 00:07:15,330 Quote. 86 00:07:16,300 --> 00:07:20,030 So in this case, there is no loop variable. 87 00:07:20,050 --> 00:07:22,780 No loop expression and no conditional. 88 00:07:22,780 --> 00:07:25,360 So this is an everlasting loop. 89 00:07:25,870 --> 00:07:29,370 And the code within the loop determines when the loop finishes. 90 00:07:29,380 --> 00:07:34,270 So the third expression in the fourth statement, the loop expression can be anything you like. 91 00:07:34,270 --> 00:07:39,130 So the only property is that it is executed at the end of the loop here. 92 00:07:39,130 --> 00:07:42,400 So you may choose to change the another variable in this expression. 93 00:07:42,400 --> 00:07:46,870 Or you can even provide several expressions separated by comma operators. 94 00:07:46,870 --> 00:07:53,230 So for example, if you have two function, one is called pull data that returns true. 95 00:07:53,260 --> 00:08:00,640 If there is a more data available and false when there is no more data and the function called get data 96 00:08:00,640 --> 00:08:08,320 that returns the next available data item you could use for as well here. 97 00:08:08,320 --> 00:08:09,040 So. 98 00:08:12,960 --> 00:08:14,300 C plus plus here. 99 00:08:14,310 --> 00:08:20,700 Introduce another way to use the for loop which is intended to be used with the containers. 100 00:08:22,160 --> 00:08:29,060 So this Cplusplus standard library contains templates for container classes, which you will learn in 101 00:08:29,060 --> 00:08:30,260 next lecture.