WEBVTT 00:02.190 --> 00:08.610 Welcome in his coding lecture, I'm going to show you how to create a pagination using stylized expressions. 00:09.120 --> 00:10.230 All right, let's get started. 00:11.180 --> 00:16.730 You know, when you search on Google, it shows your results page, then you move through the pages 00:16.760 --> 00:17.850 or the next results. 00:18.260 --> 00:20.120 It's called pagination. 00:20.750 --> 00:26.300 Now we're going to imitate the same thing using the items that we saw in the previous lecture. 00:27.050 --> 00:31.080 But before that, they removed the previous slices here except the items. 00:32.450 --> 00:35.750 OK, let me show you how the pagination is going to work. 00:36.580 --> 00:43.700 Let's say I want to display four items at a time, so let me print the first page by slicing it up to 00:43.700 --> 00:47.870 the first four elements named Duplicate this three times. 00:48.640 --> 00:50.210 OK, here is the second page. 00:50.360 --> 00:53.740 It is between the fourth index and the eight item. 00:54.560 --> 01:00.170 Here is the third page is between the eight index and the twelfth item. 01:00.990 --> 01:02.340 And here is the last page. 01:03.390 --> 01:08.650 Limerence, as you can see, there are four items per page, right, however. 01:08.670 --> 01:10.290 Pay attention to the last page. 01:10.530 --> 01:11.760 It has only one item. 01:12.360 --> 01:16.080 So we will need to handle this case in the court as well. 01:16.830 --> 01:19.260 For example, this will result in an error. 01:20.870 --> 01:23.870 It is because the 14th element doesn't exist. 01:24.460 --> 01:25.720 OK, let me take it back. 01:26.450 --> 01:33.020 If you compare the pages to all the elements here, you can easily see that each page corresponds to 01:33.020 --> 01:33.760 a line here. 01:34.370 --> 01:38.120 I've just slice it the original slice into four different slices. 01:38.900 --> 01:39.380 All right. 01:39.530 --> 01:40.760 Let's get back to the real. 01:41.290 --> 01:43.370 Let me remove these court first. 01:44.730 --> 01:48.030 OK, for the pizza guys, I'm going to declare a constant. 01:49.010 --> 01:51.270 Because I'm going to use it multiple times. 01:51.340 --> 01:51.650 OK? 01:52.580 --> 01:58.790 I'm going to use a loop to print all the pages, I'll use the front variable as a starting index for 01:58.790 --> 01:59.420 each page. 02:00.510 --> 02:06.810 To print the elements page by page, I need to increment the from variable by the page size like this. 02:07.760 --> 02:10.640 Now we know where to start slicing the elements. 02:11.150 --> 02:14.850 Let's also find out the starting position to do that. 02:14.870 --> 02:20.450 I'm going to declare a variable for this top position, then I'm going to sum the from variable and 02:20.450 --> 02:21.800 the size like this. 02:22.490 --> 02:26.150 So this allows me to get the next four elements for each page. 02:26.930 --> 02:29.630 OK, let me get the items for the current page. 02:30.770 --> 02:34.520 OK, let me print it, I will print the page header afterwards. 02:36.590 --> 02:39.710 As you can see, until the third page, everything goes fine. 02:40.190 --> 02:44.040 However, after that, the program crashes can guess why. 02:44.540 --> 02:46.460 Please pass the video and try to answer. 02:51.600 --> 02:53.720 OK, let me print indexes here. 02:54.680 --> 02:55.280 Like this. 02:56.170 --> 03:01.520 By the way, printing the details about an error is one of the best practical ways of debugging when 03:01.570 --> 03:09.010 you are prototyping, OK, as you can see it first prints the elements from zero to four than four to 03:09.010 --> 03:11.650 eight than eight to 12. 03:11.680 --> 03:14.220 And lastly, from 12 to 16. 03:14.530 --> 03:16.380 So we need to limit this right. 03:16.780 --> 03:20.160 It should never exceed the length of the item slice. 03:21.280 --> 03:27.220 To do that, I'm going to check whether the two variable is larger than the length of the items like 03:27.220 --> 03:27.580 this. 03:28.050 --> 03:32.980 If so, I'm going to limit it by assigning the length to the two variable like this. 03:33.700 --> 03:35.450 OK, this will fix the problem. 03:35.530 --> 03:36.190 Let's try it. 03:36.820 --> 03:37.220 Cool. 03:37.540 --> 03:39.790 Let's check out the last slicing position again. 03:39.910 --> 03:44.590 Now it slices between 12 and 13 instead of 12 and 16. 03:44.960 --> 03:48.150 Dude, I don't need these debugger printf anymore. 03:48.190 --> 03:48.940 Then remove it. 03:49.420 --> 03:55.120 OK, let me also bring together for each page I'm going to call the splinter function. 03:55.120 --> 03:59.630 For that you can think of the splinter function as the print function. 03:59.650 --> 04:05.350 The only difference is that instead of printing directly to the command line, it returns a formatted 04:05.350 --> 04:06.220 string value. 04:06.220 --> 04:10.450 Instead, it works just like print F, so you already know how it works. 04:10.600 --> 04:15.220 Now I'm going to type the formatting string just like a regular printer formatting string, then I'm 04:15.220 --> 04:16.290 going to pass the page. 04:16.300 --> 04:20.170 No, of course I need to find out the current page number first. 04:20.170 --> 04:24.610 To do that I can simply divide the from variable by the page size like this. 04:25.450 --> 04:27.760 OK, let me pass it to the show function. 04:29.290 --> 04:30.920 Good, but not perfect. 04:31.150 --> 04:37.450 The first page number starts at zero and increments from there, I can quickly solve this by adding 04:37.450 --> 04:39.430 one to this expression like this. 04:40.630 --> 04:42.010 Cool, now it works. 04:42.520 --> 04:43.930 All right, that's all for now. 04:44.050 --> 04:44.830 Thank you for watching. 04:44.830 --> 04:46.920 So far, seeing the next picture of a.