98-381 Microsoft Introduction to Python – Loops and Iteration
Hey guys, welcome back. So after seeing sequence and if statements, we have to check loops because loops are a very fundamental structure for programming. Actually, let’s consider a situation when you want to print “Hello World” five times. Here is a C programme that allows us to print five times. Hello world! Actually, when this programme is executed, it produces the expected result. It was simple, but let’s consider that we want to write “Hello World” 1000 times. Of course, it’s not easy to write and print the same F statement 1000 times.
So almost all the programming languages provide a concept called a “loop,” which helps in executing one or more statements up to a desired number of times. All high-level programming languages provide various forms of loops, which can be used to execute one or more statements repeatedly. So let’s write this C programme with the help of a while loop, and let’s discuss how this loop works. So let’s see if this programme actually produces exactly the same result. It writes five times, “Hello, world.” So this program, as you can see, makes use of this actual command, which is a while loop, which is being used to execute a set of programming statements that are enclosed in these brackets. So here the computer first checks whether the given condition, for example, this variable, is less than five or not. And if it finds that the condition is true, then the loop body is entered to execute the given statements. Actually, here we have the following two statements in the loop barre. The first statement is the text.
So the print action is so that we print hello world, and the second statement is “I equals I plus one,” which is actually used to increase the value of a variable one by one. I’m sorry. As a result, the variable’s initial value is less than five. This method prints “Hello World,” changes lines, and then increments variable I by one. So the new value is actually one, which is again less than five. So it prints again using this method, and the new value of I changes again; the new value would be two. So again, it’s less than five. It will print the method again, then go to three, which is less than five.
So this will be until I am five. So actually, after executing all the statements given in the loop body, the computer goes back to while, the given condition is checked again, and the loop is executed again. If the condition holds, the process repeats itself as long as the given condition remains true, which means variable I has a value less than five. So we can see what happens when we use the actual loop statement. There is a condition, as you could see here, and each time the condition is true, we have a repeated cycle again and again, and we want to break this loop only if the condition is false. As a result, this code can be easily represented in the flow diagram shown.
So this is what the while loop says. A while loop actually checks a given condition before executing any statement given in the body part. So actually, the C programming language provides another form of loop called DUI that we will see later. However, before proceeding, it is necessary to review some critical points. In almost all programming languages, a while loop begins with the keyword “while,” followed by a condition enclosed in parentheses. So it seems like an if statement, but while it’s going on, repeatedly check if the condition is valid or not. In addition to the while statement, you will have the body of the loop enclosed in curly braces enclosed in brackets. A while loop can have one or more lines of source code to be executed repeatedly, the same as the if statement. If the body of a while loop is just online, then it’s optional to use curly braces like if statements. Again, a while loop keeps executing its body till a given condition holds true. As soon as the condition becomes full, the while loop comes out and continues executing from the immediate next statement after the while loop body.
Actually, this is something that is a very common error when writing programs, because in many cases, when a programme is stuck in a specific situation, it is stuck because we have something called an “endless loop.” So if the condition holds true forever and the condition never becomes false, then the while loop continues to be executed again and again without stopping. So in this case, we have a bug. We have something that keeps our programme from moving forward. It’s something that we should consider when we are trying to debug our program. Finally, a conditional statement is usually a relational statement that is evaluated as either true or false. A value that is equal to zero is treated as false, and any non-zero value works like true. So, what’s the difference with the do while loop?
The syntax is almost the same. It has some differences. The main difference is that the condition is at the end of the loop. This means that we will have at least one loop while in the while loop, with the condition being the initial statement. In this loop, the condition is at the end. So this is actually a big difference. A while loop checks a given condition.
As I said before, it executes any statement given in the body part. So in this do-while loop, we have to execute a loop body before checking a given condition. Actually, this is an example where, as we can see, it also produces exactly the same result. The initial value of variable I is zero. We have this message printed, and again, the value of the variable I goes to one, which is less than five. And again, the loop is in these brackets. So we repeat this loop until I is equal to five. So we want to be out of the loop. I believe that these two examples are very representative of this in order to understand loops. And this is the third, actual structure of programming that is used by almost all the high-level programming languages. So I’m looking forward to seeing you in the next lesson.
Hey guys, welcome back. Now we will see loops. A loop statement allows us to execute portions of or groups of statements multiple times. So actually, we can have while loops, but we repeat a statement or group of statements. While a given condition is true, we can also have a loop around that. This means that we can execute a sequence of statements multiple times, and actually, it is the code that manages the loop variable. Finally, we can have nested loops, which allow us to nest one or more loops inside another. So we can either use while 4 or a while loop.
So let’s see some examples. So, for example, the first thing we have to do is write here a comment and say that we’re going to have some loop statements right here. So the first one, the first loop, is an example of a while first loop. So here we can say that we will use an integer variable called i. And I will say that while I am less than five, then print, but be careful. Here’s an inside look from the time. So I typed a tab right here, and I will say that the value of I is I will use a comma after the brackets, and then I will type i, which means that this text will be static and will be repeated five times because I say that while I is less than five, the initial value is zero. So zero is less than five, and it will be printed. I has the value, and I has the value zero. Then we will go up, and the value will also be zero. So this is actually a loop that will be executed forever. So I have to increment the value of the variable i. So I’m going to say that the new value of the variable I will be I plus one. So the initial value will be zero.
But the next value will be zero plus one, which is one. So the new value of the I is one. So one is less than five. It will be printed. The value of I is an empty space here. And then one plus one makes two. So the new value of the variable will be two. It will go up. Two is less than five. So it will be three, four, and five until I am eight years old. I am not less than five. It’s equal to five. As a result, it will stop executing these commands and proceed to the end. So I will print here the end of the program. So, let’s see, I’ll copy this command, paste it right here, and write a while loop.
So I right-clicked “run python file” in the terminal, and I have an error because, as to the if statements here, we need this symbol that is actually used to say that. Here is where we begin executing the commands, so I will right-click run Pythonfile in Terminal, and as you can see, it begins the while loop. The value of I is zero. The value of I is 1234, and five is not printed because five is not less than five. So the block here consisting of the print and increment statements is executed repeatedly until the I variable is no longer less than five. So with each iteration, the current value of the index I is displayed and then increased by one. Actually, if I were to delete this command, as I said, we would have an infinite loop. And this is one of the most basic errors made in computer programming encoding. So if I’m going to run it right here, I will have all the time. The value of the I is zero, and it never ends.
So in order to stop the terminal, you can just click “Kill Terminal” right here and then right-click and again run the Python file in the terminal. So I will kill it again, and I will bring back this command to increment the value of the variable I. Then I’ll right-click run Python file in terminal, and as you can see, it’s now okay, so an infinite loop might only be useful in programming if we want something to run continuously, so that client programs, for example, can communicate with it as needed.
So I could also have another statement in Python. And there are not a lot of languages that support this feature, but Python supports having an L statement associated with a loop statement. So if the L statement is used, for example, for a while loop, the L statement is executed when the condition becomes false. So if the value of I is not less than five, then I can print that the value of I is, I’m sorry, more than five. If I’m going to execute it, it will produce the following result: equal Actually, it is correct, or more than five. But everything else will be done only once.
So I can’t just use else to create an infinite loop. Because, if I could teach you, if something is not true, it will always be false. So this is the example of the first loop example, the while example. We will see now how we can use the second-loop example, which is the four-loop example. So this means that we have the ability to iterate over the items of any sequence, such as a list or a string. So I can use, for example, a list right here with names, and I will have christened my names Maria and John. And then I can say for name, I’ll use a variable name with the name name, as well as the names list above, so that I can say four names for each name in the names list. I want to print the current name as well as the names in the program. So now if I’m going to execute it, I will see that its name is being printed, and then I have the end of the programme right here, so it produces the result. But I can also actually use another example.
We can use the I variable. So I can also have this example, the third loop example, with loop I can also use this example. I will write here for the loop, but I will say four in the range, which ranges from zero to five. I would like to print these values, and I can also use the L statement with a for loop. When the L statement is used in conjunction with a for loop, the else statement is executed once the loop has finished iterating the list. So now I will run it again. So, for I in rates zero to five, I have to right-click and type this symbol. And as you can see with this for loop, I have exactly the same example, the same print, and the same statements being printed right here. And actually, what I’m saying to Python is to use an Invariable with the initial value of the variable set to zero. And then, as the value is less than five, it’s inside this range. Then print the value of I if this is it. So this is an example of iterating over a sequence list. We can also have nested loops. So an example would be a fourth nest, while an example loop would be to have something. I’m going to duplicate this nested while loop and then zero. I will actually use the first example, and I’m going to say exactly the same things. while I am zero, then five.
But here I will use another value, another variable, and I will say that j is equal to two. And while j is less than i, then print. I’d also like to see the j value printed. And I’m pretty sure I left out the comma. The comma is here. This is j, and then j equals j plus one. So this means exactly the same with I: in its iteration, we will increase j by one. So, right-click and select Run Python in the terminal. We have a problem right here. We have an invalid syndax because I have to use this symbol that I always forget. And as you can see, the value of variable I is zero, one is two, and while two is j, j is not less than I. But when I was three, I printed the j value. And, once again, when it’s four, I’m always printed.
So, for example, if I say here that j equals zero, you will see and run a Python file in the terminal that the j value begins with zero, and one is not greater than zero. So each time I have printed the values of j, when it reaches the value of the variable i, then it goes out of this loop. And then the value of the variable I is incremented, and the j is from zero to three, for example, until the value of the variable I is four. Well, and then I have the final print, the else print, and the end of the program. So I think with these examples, you have an overview of how we can use these loop examples. So I’m looking forward to seeing you in the next lesson.
Popular posts
Recent Posts