﻿[Script Info] Title: [Events] Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text Dialogue: 0,0:00:00.01,0:00:04.36,Default,,0000,0000,0000,,You've just worked with Sarah to write and understand several loops, and what Dialogue: 0,0:00:04.36,0:00:08.33,Default,,0000,0000,0000,,will often happen to you that you have a loop that's really mysterious. And Dialogue: 0,0:00:08.33,0:00:12.38,Default,,0000,0000,0000,,I'll give you an example. Here's some code that comes from a program that you Dialogue: 0,0:00:12.38,0:00:16.35,Default,,0000,0000,0000,,will improve in a bit to solve a common problem, namely to identify reverse Dialogue: 0,0:00:16.35,0:00:20.51,Default,,0000,0000,0000,,digits in a credit card. So let's see what this code does. What you always want Dialogue: 0,0:00:20.51,0:00:24.58,Default,,0000,0000,0000,,to do is get out a sheet of paper. Here's my sheet of paper. You make a table. Dialogue: 0,0:00:24.58,0:00:29.54,Default,,0000,0000,0000,,One column for every variable. There's a variable n, there's a variable sum. Dialogue: 0,0:00:29.54,0:00:34.32,Default,,0000,0000,0000,,Now, it's always a good idea to take a marker, I like to use a paper clip, to Dialogue: 0,0:00:34.32,0:00:38.95,Default,,0000,0000,0000,,mark where we are. So, we set n to 365 and we set the sum to 0. Now we enter Dialogue: 0,0:00:38.95,0:00:43.66,Default,,0000,0000,0000,,the loop. Is n greater than 0? It sure is. We get to this statement. Now we Dialogue: 0,0:00:43.66,0:00:48.76,Default,,0000,0000,0000,,need to compute n modulo 10. That's the last digit of n. That would be 5. and Dialogue: 0,0:00:48.76,0:00:53.67,Default,,0000,0000,0000,,now we have a new variable! We record it, we put in the 5. Moving on, sum is Dialogue: 0,0:00:53.67,0:00:59.10,Default,,0000,0000,0000,,sum plus digit, n is n divided by 10, it's an integer division, so we discard Dialogue: 0,0:00:59.10,0:01:04.62,Default,,0000,0000,0000,,the remainder. We move to the top of the loop. Is n greater than zero? Compute Dialogue: 0,0:01:04.62,0:01:10.15,Default,,0000,0000,0000,,n MOD 10, 36 MOD 10 is 6, store that in the digit. Sum is sum plus digit. It is Dialogue: 0,0:01:10.15,0:01:14.84,Default,,0000,0000,0000,,11. N is n what 10, now n is 3. Back to the top of the loop. Is 3 greater than Dialogue: 0,0:01:14.84,0:01:18.81,Default,,0000,0000,0000,,zero? It barely is, so we stay in the loop. Now we need to take 3 Dialogue: 0,0:01:18.81,0:01:19.61,Default,,0000,0000,0000,,[INAUDIBLE] Dialogue: 0,0:01:19.61,0:01:24.57,Default,,0000,0000,0000,,10. That's 3. Add it to the sum. Divide n by 10, that's an integer division, so Dialogue: 0,0:01:24.57,0:01:29.40,Default,,0000,0000,0000,,now we get 0. Zero is no longer greater than zero. We fall out of the loop, and Dialogue: 0,0:01:29.40,0:01:34.13,Default,,0000,0000,0000,,go to this statement. And what are we printing? You're priting 14. Okay, well Dialogue: 0,0:01:34.13,0:01:38.92,Default,,0000,0000,0000,,what's 14? Well, we've computed all of the digits of the number 365. 3, the 6, Dialogue: 0,0:01:38.92,0:01:43.73,Default,,0000,0000,0000,,the 5, and we've computed their sum. 14 is the sum of the digits. And that's Dialogue: 0,0:01:43.73,0:01:48.42,Default,,0000,0000,0000,,almost what one needs to do in order to verify a credit card. You'll see in Dialogue: 0,0:01:48.42,0:01:53.23,Default,,0000,0000,0000,,your next programming assignment what sum of digits you exactly need to do.