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