Welcome to another Python Programming class, where we teach you python from scratch. In the last class, we have seen what are control statements. What are its types and primarily if-else statements and switch-case statements and how to use them in your program. If you were absent from that class, Don’t worry! Here, Check it out: Episode #5.
In today’s class, we will learn about Iterative statements or “Loops”.
What are Iterative Statements?
Iterative statements are also known as repetitive statements or looping statements. When the program requires executing a statement or a set of statements to be repeated until the condition becomes false and each time the value of a variable changes from the previous value, then the repetition flow of control is used. For repeating a group of statements, loops are used.
Well, that was the theory or the defining part, what are loops are. Basically, Loops are what they are actually is. A never-ending cycle. But in Computer Programming, Loops are a cycle of repetitive instructions until you get a condition where you have to stop and comes out of the loop.
For Ex – If I ask you to write a program to print your name. then what will you do? Simple, as it sounds. You print your name using the print(“ Name “) Statements. That’s it.
But if I ask you to print your name 10 times, then? Then also you can write print statements but if ask you for a 100 times or a 1000 times, then what you will do? Theoretically, you can still write the print statements for the x amount of time. But how feasible it is? Certainly, It is not at all feasible. In this case, you will use loops, you will initiate a loop and the program executes the statements according to loop until a certain condition is met. This condition is mostly the range of loop or the number of times, it has to run.
Types of Loops
There are three types of loops:
- While Loop
- Do-while Loop
- For Loop
The while loop is also known as entry controlled loop. The condition or expression must be given in parenthesis with a while keyword. The condition is checked first, and if it evaluates to true, then only the statements inside the while loop gets executed, otherwise not. As soon as the condition becomes false, the control comes out of the while loop.
While ( condition ):
Here is an example of while loop in action
Here you can see we initialize a variable i = 0 and then we initiate a while loop, we put the condition that, while loop will run until i < 10 and then we print “i” and then we iterate the value of i by 1 using i = i + 1 statement. So the loop work like this – initially, the value of i is 0, then it goes to the while loop, it checks the condition “ is i is less than 10 ?” and the answer is YES. Then it goes to another line, it prints the value of i which is 0. Now it iterate the value of i by 1. Now the value of i is 1, it again went to loop and check it again found YES and that’s how the loop keeps on running until the value gets 10. Now if it checks, is the value of i is less than 10, now it’s not. You get out of the loop. Remember, the intendetion from the previous class. Here also, the rule applies.
Do- while Loop
Do-while Loop is an exit control loop. In the do-while loop, the control enters the loop, executes the statements enclosed in the loop, and then the condition is checked. In other words, if the condition is given inside the parenthesis with while the keyword becomes false for the first time, even then the body of the loop has already been executed.
i = i + 1
while ( i < 10 )
It is just different from while loop is that, in do-while one statement is always executed as it is exit controlled loop, the condition is checked in the end. It comes handy sometimes, but most of the time, we don’t use it.
For loop is the easiest and range based iterative control statement. It is the loop in which all three control statements such as initialization of variable, condition, and update expression can be given together in one place.
for i in range ( 0, 10 ):
Here is the For loop in action:
Here first we ask for the user’s name, then it went it to loop after initializing the “for” keyword. We write the variable name that was “i” within the “range” of 0 to 10 and then we print the name afterward in the following statement. This works like this, we set a range of loop from 0 to 10 to run and we ask for an input, so it keeps on executing the statement inside the for loop until the counter of loop count reaches 10, as soon it reaches 10, it comes out of the loop. The endpoint of the loop is n-1 always less than the last range value.
Nested Loops are the loops inside another loop. For a quick example have a look at this. These are two nested for loops.
Well, that’s all for today. Today we have learned what iterative control statements or Loops are. What are different types of loops, which include while loop, do-while loop, and for loop. We also see Nested loops. So, that’s it, now you know, what can you do with loops and Loops are one of the most interesting and important topics of any programming language. In the next class, we will see some “Jump statements”. This is Dolores Haze, Signing off, I will meet you in the next one, till then “ Happy Coding”.