Loops

5 minutes
Share the link to this page
Copied
  Completed
In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached. Typically, a certain process is done, such as getting an item of data and changing it, and then some condition is checked such as whether a counter has reached a prescribed number.

Transcript

Hello, in this video we are going to look at loops. loops essentially are a method or method a piece of functionality within Python and many other programming languages that allow you to essentially iterate over code again and again and again and again. So it does, it really just allows you to loop over the same code again and again and again, without having to you know, repeat the code as a percentage 100,000 plus times that what we're gonna do is create something very simple. We're going to create a ball while one equals hello world. And then the format of alluded to that, a couple of different types of loops, and the first one is for, so you just do for and now, there's two parts to this. First, is like in explaining the second one is, so the left hand side is what you're essentially getting from the right hand side.

So the right hand side Think of it as like the object like a string or a list, or like a dictionary or, you know, something along those lines, you know, some sort of sequence, essentially. And now, what we're going to do is put the string into Gawain. And so for netta might be a character for Colton Baldwin character is just a name that we made up. So for every character involved, that's what this is doing. And put a colon and then here make sure it's indented very similar to decision making is for print and we are going to print the letter character, click Run and see what we get. There we go.

We are now done. So it says hello world. So you can also iterate using sequence indexes as well. So you can essentially provide a index for a particular lights sequence, such as a list, for example. And you can do that as you normally would in many other languages. So the next leg we're going to look at is the wild.

The Wild is sort of similar to the for loop. And the while loop is essentially saying while something is true, then you know do a bit of code. So if we do while so while you can tell this in brackets, well no recommend. So while this professional variable var two equals 100, while var two is less than 200, let's do less than one. Less than 110 do cola and will going to do is print, we're simply going to print out less than 110. But what we're going to do is at the end of each volume, we only bought two plus plus.

So this will actually increment our now let's do this operator the one we've already learned. So we are going to increment the both one at the end of each loop. So we'll have a run see what result we get. Okay, we got a problem line. Definitely long lines. Wrong.

I made mistakes than me just trying to figure this out too long, six lines. Now think is this you can actually record my bad Okay, there we go Hello World in less than one to 110 minute lengthy stop because it gets to above 110. The next test here, you can print off the value of our two at the end maybe inside the wall as well. So another thing that you can do is attach a else statement. So essentially, when this isn't true, we could print off something else so we could say, print not 10 Let's run it. See what we got.

Yet not less than 110 at the end, so when this condition is no longer true, so that could actually occur straightaway. So if I were to change the value as an extra 10 points to do this, which is one to one for me, then this isn't true, you know from the start, so the house will get cold. So that's the for loops, you'll be using loops all over the place, you'll be using them everywhere. You can also nested loops. So within like a for loop, you could do a no for loop or a while loop. You can nest like quite a lot of things.

Like with decision making. You can nest if statements inside if statements and you know if inside the elsif, cetera, et cetera, along with a properly indented, you can nest and extend within a loop, you could in net, nest, a loop inside of an if statement and so forth and so forth. And this is just the wonderful world of loops fast watching, and I look forward to seeing you in the next live video.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.