1 00:00:05,450 --> 00:00:07,880 In this video, we'll do a quick overview of looping in 2 00:00:07,880 --> 00:00:10,950 general and specifically in c++. 3 00:00:11,760 --> 00:00:14,930 Looping is also called iteration or repetition. 4 00:00:15,429 --> 00:00:18,160 It's the third basic building block of programming. 5 00:00:18,850 --> 00:00:22,310 After this section of the course, you'll have all you need to begin to 6 00:00:22,310 --> 00:00:26,999 solve very complex problems, using sequence, selection and iteration. 7 00:00:28,460 --> 00:00:30,880 In this course, we're covering c++. 8 00:00:31,080 --> 00:00:34,309 However, all programming languages must support sequence, 9 00:00:34,320 --> 00:00:35,629 selection and iteration. 10 00:00:36,160 --> 00:00:39,370 Once you understand these concepts completely, then it's just a matter 11 00:00:39,370 --> 00:00:42,600 of picking up the syntax of another programming language and you'll 12 00:00:42,610 --> 00:00:45,900 be able to implement solutions to problems in those languages as well. 13 00:00:46,860 --> 00:00:49,379 Loops allow the execution of a statement or block 14 00:00:49,380 --> 00:00:50,720 of statements repeatedly. 15 00:00:51,630 --> 00:00:53,279 And they're made up of two sections. 16 00:00:53,529 --> 00:00:56,430 First, the loop condition which determines when we 17 00:00:56,430 --> 00:00:57,630 iterate and when we don't. 18 00:00:58,420 --> 00:00:59,830 Second is the loop body. 19 00:01:00,070 --> 00:01:03,420 That's the statement or block of statements that will be repeated. 20 00:01:03,620 --> 00:01:04,390 That's it. 21 00:01:04,450 --> 00:01:06,210 Conceptually, it's very simple. 22 00:01:06,650 --> 00:01:08,649 Let's look at a few use cases for looping. 23 00:01:10,330 --> 00:01:12,489 Here are some typical use cases for looping. 24 00:01:13,190 --> 00:01:17,080 We want to execute a loop or repeat statements a specific number of times. 25 00:01:17,190 --> 00:01:21,420 For example, we want to do something 10 times or we ask the user how many 26 00:01:21,420 --> 00:01:22,980 times they wish to do something. 27 00:01:23,490 --> 00:01:28,130 Another use case is iterating one time for each element in a collection. 28 00:01:28,440 --> 00:01:31,550 So suppose we have items in an array or a vector, and we want 29 00:01:31,550 --> 00:01:33,300 to process each of those items. 30 00:01:34,110 --> 00:01:37,330 Other times we want to loop until a specific condition becomes true. 31 00:01:37,750 --> 00:01:40,990 So we loop until we see a specific value then we stop. 32 00:01:41,679 --> 00:01:43,439 Of course, we can also loop forever. 33 00:01:43,880 --> 00:01:47,170 This isn't as common as the other use cases, but operating systems 34 00:01:47,170 --> 00:01:50,980 are an example of programs that loop forever executing system processes. 35 00:01:51,700 --> 00:01:54,190 There are many other use cases and variations of 36 00:01:54,190 --> 00:01:55,680 these existing use cases. 37 00:01:56,570 --> 00:01:59,450 Let's look at the c++ looping constructs now. 38 00:02:01,080 --> 00:02:05,429 C++ has three main looping structures: the for loop, the 39 00:02:05,430 --> 00:02:07,300 while loop and the do while loop. 40 00:02:08,320 --> 00:02:11,820 The for loop has a ranged base variant, which is really great. 41 00:02:12,350 --> 00:02:13,850 First, let's talk about the for loop. 42 00:02:14,540 --> 00:02:17,109 The for loop is typically used when you want to iterate 43 00:02:17,110 --> 00:02:18,740 a specific number of times. 44 00:02:18,880 --> 00:02:22,620 For example you want to loop 10 times or end times, where n 45 00:02:22,620 --> 00:02:24,049 is determined by the program. 46 00:02:25,150 --> 00:02:28,260 The range-based for loop makes it really easy to iterate through 47 00:02:28,260 --> 00:02:30,930 a range of items or through the elements of a collection. 48 00:02:31,610 --> 00:02:34,030 With arrays vectors and other collections, this 49 00:02:34,030 --> 00:02:35,429 is a great loop to use. 50 00:02:36,799 --> 00:02:40,410 The while loop iterates while a specific condition remains true. 51 00:02:41,079 --> 00:02:43,660 When the condition becomes false, we terminate the loop. 52 00:02:44,640 --> 00:02:47,370 In the case of the while loop, the condition is checked at the 53 00:02:47,370 --> 00:02:49,130 beginning of every iteration. 54 00:02:50,020 --> 00:02:53,450 The do while loop is similar to the while loop, except that the condition 55 00:02:53,450 --> 00:02:55,299 is checked after each iteration. 56 00:02:55,759 --> 00:02:58,579 With only these basic loops, we can implement any 57 00:02:58,600 --> 00:03:00,120 iteration pattern we want. 58 00:03:00,859 --> 00:03:03,450 All programming languages have loop constructs. 59 00:03:03,770 --> 00:03:06,200 The c++ loops are clear and concise. 60 00:03:06,910 --> 00:03:09,390 Let's go through the c++ loops one at a time. 61 00:03:09,730 --> 00:03:11,670 Let's start with the for loop in the next video.