1 00:00:05,670 --> 00:00:08,080 In this video, we'll learn about infinite loops. 2 00:00:08,870 --> 00:00:11,429 An infinite loop is often called an endless loop. 3 00:00:12,070 --> 00:00:15,839 An infinite loop is a loop whose control expression always evaluates 4 00:00:15,860 --> 00:00:18,720 to true, so it iterates over and over. 5 00:00:19,590 --> 00:00:22,320 Usually, infinite loops are mistakes by the programmer. 6 00:00:22,590 --> 00:00:25,220 If you have an infinite loop, it's pretty easy to know since 7 00:00:25,240 --> 00:00:28,519 the program does something or does nothing over and over again. 8 00:00:28,770 --> 00:00:30,349 So we find and fix the problem. 9 00:00:30,780 --> 00:00:33,939 However, sometimes infinite loops are written as infinite loops on 10 00:00:33,940 --> 00:00:37,980 purpose, and programmers use break statements as strategic places to 11 00:00:37,980 --> 00:00:39,300 eventually break out of the loop. 12 00:00:40,110 --> 00:00:42,880 There are cases where this makes sense and could be justified. 13 00:00:43,019 --> 00:00:45,730 However, writing an infinite loop and using break statements to 14 00:00:45,730 --> 00:00:48,949 terminate the loop is usually bad practice and should be rewritten 15 00:00:48,950 --> 00:00:53,209 so the loop condition expression is descriptive of what the loop is doing. 16 00:00:54,219 --> 00:00:56,860 Here are a couple of examples of infinite loops that are 17 00:00:56,860 --> 00:00:58,830 appropriate and in fact necessary. 18 00:00:59,240 --> 00:01:02,260 Infinite event loops are commonly used in event-driven programs 19 00:01:02,269 --> 00:01:05,399 such as those you find on mobile devices and embedded systems. 20 00:01:05,700 --> 00:01:08,429 In these programs, the program loops forever listening for mouse 21 00:01:08,429 --> 00:01:11,869 clicks, mouse movements, touches and so forth and reacting to them. 22 00:01:12,490 --> 00:01:14,630 This continues as long as the program is running. 23 00:01:15,520 --> 00:01:18,149 Another example of an endless loop is an operating system. 24 00:01:18,320 --> 00:01:21,700 An operating system is constantly looping handling input/output, 25 00:01:21,809 --> 00:01:23,469 handling resources and so forth. 26 00:01:23,639 --> 00:01:25,860 It only shuts down when you shut down your computer. 27 00:01:26,770 --> 00:01:29,920 Let's see a few examples of infinite loops in c++. 28 00:01:31,610 --> 00:01:33,610 First, let's look at an infinite for loop. 29 00:01:34,030 --> 00:01:36,950 Remember that all three expressions in the for loop are optional. 30 00:01:37,100 --> 00:01:40,090 So if you omit all three, you get just two semicolons. 31 00:01:40,260 --> 00:01:42,970 By definition in c++ that's an infinite loop. 32 00:01:43,929 --> 00:01:47,130 The output statement will be displayed as long as the program is running. 33 00:01:48,000 --> 00:01:49,050 Let's look at a while loop. 34 00:01:50,030 --> 00:01:51,990 Here's an example of an infinite while loop. 35 00:01:52,160 --> 00:01:54,200 It's probably exactly what you were thinking of. 36 00:01:54,730 --> 00:01:57,110 While true will always be true. 37 00:01:57,360 --> 00:01:59,830 And the output statement will execute over and over again. 38 00:02:00,370 --> 00:02:02,600 Of course, we don't have to use the true keyword. 39 00:02:03,000 --> 00:02:04,859 We can use an expression that's always true. 40 00:02:04,859 --> 00:02:09,320 So if we say while 10 is less than 12, since 10 is always less than 12, 41 00:02:09,580 --> 00:02:11,170 it'll loop forever, you get the idea. 42 00:02:11,660 --> 00:02:13,910 Finally, let's look at an infinite do while loop. 43 00:02:14,940 --> 00:02:17,170 Again, the loop condition will always be true. 44 00:02:17,190 --> 00:02:19,169 So the output statement repeats forever. 45 00:02:19,809 --> 00:02:20,630 Okay, so what. 46 00:02:20,630 --> 00:02:21,849 Why would i ever do this? 47 00:02:22,389 --> 00:02:25,480 Well, you might write an event loop like this but more often you see code 48 00:02:25,480 --> 00:02:26,740 that looks more like the following. 49 00:02:28,220 --> 00:02:30,710 Note that in this example, we have an infinite loop since 50 00:02:30,720 --> 00:02:32,630 while true will always be true. 51 00:02:32,920 --> 00:02:35,800 However, we have a strategically placed break statement 52 00:02:35,800 --> 00:02:36,859 that terminates the loop. 53 00:02:37,130 --> 00:02:38,459 Let's walk through the example. 54 00:02:38,929 --> 00:02:41,740 Inside the loop, we declare a character variable named again. 55 00:02:42,260 --> 00:02:45,720 We ask the user if they want a loop again and read the response as a 56 00:02:45,720 --> 00:02:47,460 character into the variable again. 57 00:02:47,930 --> 00:02:51,290 If again is uppercase n or lowercase n, then we break 58 00:02:51,290 --> 00:02:52,160 and terminate the loop. 59 00:02:52,630 --> 00:02:54,629 If they enter anything else, we loop again. 60 00:02:55,620 --> 00:02:58,390 This is an example of an endless loop that might be okay. 61 00:02:58,490 --> 00:03:02,180 However, best practice suggests that the loop condition be more informative 62 00:03:02,180 --> 00:03:04,350 of when the loop iterates if. 63 00:03:04,350 --> 00:03:06,250 I was doing the code review of this code, I would 64 00:03:06,260 --> 00:03:07,730 refactor it to do just that. 65 00:03:07,960 --> 00:03:11,730 However, you do see code like this out there, and now you know how it works. 66 00:03:12,340 --> 00:03:13,499 That completes this video. 67 00:03:13,650 --> 00:03:16,830 In the next video, we'll see how we can nest loops one within the other.