Welcome to another Python Programming class, where we teach you python from scratch. In the last class, we have seen what are repetitive control statements or Loops, which is one of the most important topics in Programming. We have learned about various types of loops, their applications, and the way to use them in your programs. If you haven’t seen that, go check it out now – Episode #6.
In Today’s class, we are going to learn about Jump statements in Python and how to use them, so let’s get started.
What are Jump Statements?
Jump Statements are the statements that change the flow of control to a specific location by skipping statements in a program unconditionally.
In other words, these statements or keywords are used to alter the flow of loops, like if you have a loop in which you are going from 1 to 100 but you have to suddenly stop the control of the loop or change it, then at that case, you have to use the Jump Statements.
Types are Jump Statements
There are two types of Jump Statements in Python:
- Break Statement
- Continue Statement
When a Break statement is encountered in a program, the flow of control immediately leaves the enclosing control statement. It causes the enclosing for, while, do-while loop or switch statement to terminate.
In other words, when we use break statement in a loop, then the control comes out of the loop or the loop ends there, as soon as it encounters a break statement.
Here you can see break statement in action:
Here you can see a normal regular while loop, in which i is initialized to 0 at the beginning, then the while loop is used with the condition i < 100, that means the loop will run from 0 to 99. Typical, while loop to print number up to 100. Then there’s a print statement to print the value of i in the program and then an incrementation of i. But after that, there’s an if condition which checks if i == 75 and then the break statement is used.
What happened here, is that every time the loop runs it checks the if condition until it gets true, when it turns 75, the break statement is executed and the control comes out of the loop. That’s how a break statement works. It is usually when, you are not sure, how long the loop will run and you don’t want to keep the loop running after you get the desired result. So you use a condition with a break statement to get the control out of the loop as soon as possible.
The continue statement skips the rest of the statement to be executed in a current loop which is following the continue statement and the flow of control goes to the next iteration in the loop. As soon as the continue statement is encountered in the loop, the statements following the continue statement will not be executed, rather the flow of control goes to the next iteration in the loop.
Here you can see continue statement in action:
Here you can see first we initialize the value of i to 10, then we use a while loop, after that the condition we use here i > 0, that means the loop will run until the value of i is greater than 0, then we decrement the value of i by 1, now if use an if statement to check if the value of i == 5, it is then we use the continue statement, otherwise print the value of i, by that we print the number from 10 to 0 in decreasing order. But as have seen, in the output something is missing and that is, “5”. Well, it is because of the continue statement we use with if. As soon as the value becomes 5, the loop encounters the continue statement and what continue statement does, is that it skip the current iteration and so the statement below it like the printing of “i” here and jumps to the next iteration. Unlike, the break statement which directly jumps out of the loop.
So, that’s all for today guys, Today we have learned what are the jump statements. We have learned about what is break and continue statement and how to use them in your programs. This is professor void, signing off, I will meet you in the next class, till then “Happy Coding”.