Learn what is the usage of loops in code?
The biggest most important usage of loops is the ability to repeat an action but is that all what loops are good for? no actually they have more usages then that... lets jump in and explore.
Video Transcript
00:05Great, fantastic.
00:07All right, so my next question, and Peter,
00:09you could join in as well, and Carol obviously join in.
00:13We were talking about, we said already that loops
00:14could do things over and over, which isn't,
00:17after you get it, it's intuitive, intuitive.
00:20It's like telling someone that's stamping something
00:23with a stamp, do it 100 times on all the pages.
00:26That's actually a really, really great example.
00:28Sorry, I just had to pat myself on the shoulder now.
00:31Okay, so a great way to give an analogy to a loop
00:35is saying you have a bureaucratic clerk
00:38that's sitting in an office,
00:40and I'm gonna train this officer,
00:42I'm gonna train this bureaucrat today,
00:44and I'm gonna tell him how to do their job.
00:46And I'm gonna come to him and say, okay,
00:47look at this paper, right, here's a stamp,
00:50take every paper and just stamp it,
00:51move on to the next paper, stamp it,
00:53move on to the next paper and stamp it.
00:55I just had to tell the operator once, right,
00:58I just told him once, and I told him,
00:59here's a stack of papers, here's 200 papers,
01:01go, start doing it.
01:03I didn't have to say to the person 200 times to do it
01:06because he already understood,
01:08because I gave him a command and they said,
01:09do this for X amount of times.
01:11And that's exactly what loops are.
01:13So when you go into loops, the basics of a loop is,
01:16and a for loop is a perfect example of that.
01:18A for loop basically says, well,
01:19I want you to do it a certain amount of times,
01:22and these are my conditions.
01:23One, let's start counting.
01:24So I'm gonna start from zero,
01:25but I could count from whatever number I want.
01:28I could go up, I could go down,
01:29I could add more, I could jump in digits,
01:32I could say, but the most important part
01:34is basically saying, okay, I wanna do it,
01:36I want I to be smaller than 10 always.
01:38So when it starts with one, it's, okay,
01:40it's the first time, it's just zero,
01:42then when it goes up, it adds this before it,
01:45before it checks the condition, then it does it again.
01:49So in theory, if I wanted to do this step by step,
01:51and I'm gonna put the phone down again for a second,
01:53if I wanted to create a theoretical loop step by step
01:56without actually doing a loop,
01:57so we're gonna try to recreate a loop
01:59that just goes three times, okay?
02:03How would that look like?
02:04So first of all, I would create a new variable I,
02:06which would be an integer equals zero.
02:08The next stage would be, I'm gonna first ask,
02:11if I is smaller than three,
02:16then do something, right?
02:17Let's say this function, how do you retrace?
02:20Trace, let's say I.
02:22All right, so we're gonna trace I.
02:26Now the next step after that,
02:28if that was true, or it's not true,
02:30it doesn't really matter, even though in this state,
02:32it must have been true.
02:34So when that happens, when it was true,
02:37and in the end of our if statement,
02:38I'm gonna say I plus plus, I'm gonna add one to my I.
02:42If I wanted to do this now three times,
02:44I would have to go to this if statement,
02:46copy it, and do it now three times, right?
02:51Does that make sense, Carol?
02:52Correct, oh, yes, yes it does.
02:54And, and-
02:55Can I ask a question?
02:56Yeah, of course.
02:57Let me back, that is a great question.
03:15No, it's not correct,
03:16but it's a really, really great question.
03:17And the reason why it's a great question,
03:19and it's great that you're asking that,
03:21because that's just, it's a great question.
03:23But before I answer that, just I'll say that,
03:24all of this is basically this loop.
03:26It's the exact same thing.
03:27So now let me answer your questions.
03:30So I'm not gonna, now I'm gonna answer your question.
03:31Let me just delete this.
03:33All right, so I'm gonna go back to this function.
03:42Well, I wouldn't even, I wouldn't even need,
03:45I wouldn't even need an else, right?
03:46Because this is way less efficient, right?
03:49Because here I'm gonna ask if I is smaller than three,
03:52it is, right?
03:53Now it's zero.
03:54So I'm gonna do this one.
03:55So now I equals one.
03:57Now I equals two, now I equals three, right?
04:01So then when I'm done, and even if three,
04:04if it happened here, maybe I equals three already,
04:07then it would, and you're actually right.
04:09The probably the more accurate way of saying it
04:12is probably the code would look more like this.
04:15So in a loop, a loop would actually do something more,
04:19that would look more like this.
04:21So it would actually be inside of your,
04:24so your if would be inside of that if statement.
04:27Let me just push that.
04:29And this if would be inside of that if statement as well.
04:33So that would come here.
04:35And that answers your question
04:37because what actually happens is,
04:40if I is greater or equal to three, then that's it.
04:44This is over.
04:45This is over, it skips this lines of code,
04:47it moves on to line 19 and it continues.
04:50If it's still smaller than three,
04:52then it's gonna do all this, right?
04:54Then it's gonna come here and if it's smaller than three.
04:55So basically a loop is really just creating an if inside
04:58of an if inside of an if over and over and over and over
05:00with the same code inside.
05:02So this is basically the exact same thing.
05:05It's basically the exact same thing,
05:06only obviously this is way, way easier to work with,
05:09right?
05:12Especially if you had to do it a hundred times.
