These keywords are all loop control methods giving you the tools to change execution flow away from its normal sequence. Think of these keywords as traffic controllers, deciding the course of your loop execution. This video will explore each of these keywords and demonstrate code examples accompanied with control structure flowcharts.
Hey gang Tim here at Core Electronics and today we're exploring how the keywords “pass”, “break” and “continue” are utilized within the shooting star programming language of Python!
Python has three loop control methods which utilize “pass”, “continue” and “break” statements. These loop control statements are tools to change the execution flow from its normal sequence, the execution flow is a sequence in which individual statements, instructions or functions of a software program are executed and evaluated. Control structures are a flowchart method to represent this control flow within programming languages, these flowcharts will be used in this video to demonstrate the effects of these keywords by having a firm grasp on how to use these keywords effectively you will be able to demolish any programming scenario, as a refresher keywords are the nuts and bolts vocabulary of any programming language, are reserved words and have a special meaning, this special meaning can be commands or parameters which will be executed as soon as the computing device (Such as a Raspberry Pi) encounters the keyword when the code is run.
So, loops in Python repeat and automate tasks in an efficient manner that would otherwise take many lines of code. However, there may arise a situation where you want to exit the loop completely, ignore parts of the internal code for a single iteration cycle or even skip the entire iteration cycle. Perhaps you may want to create a new internal condition or stop a nested loop and start the next iteration for the outside loop, all of these can be done with loop control statements. Now worth noting when the control flow leaves the iteration straight away using these keywords, any variables that would potentially have been created in the future lines inside the loop body won't exist, effectively their scope is destroyed or more correctly never created. Script refers to the coding region from which a Python variable is accessible now this will be dived into further future content so don't worry too much about it right now.
So, let's dive in, the “pass” statement is a null statement, the “pass” statement acts as a placeholder and usually is employed where there is no need for code or commands to be executed but a statement is still required to make the Python syntax correct, this is a great tool to use in places where your code will eventually go, but it just hasn't been written yet, so in these situations the Pythonista simply types in the “pass” keyword, in fact you can use this keyword anywhere inside of Python script, the rules of Python syntax is that empty code is not allowed in loops, function definitions, class definitions or “if” statements by correctly using a “pass” statement you can avoid error messages for all empty code situations. Now, for instance no code within a “while” loop will cause a syntax error but you can still have a “while” loop with no content so to do this simply type the “pass” keyword inside the body of the “while” loop now when the control flow reaches the “pass” statement it will execute the “while” loop without issues. Python is happy with the syntax, the path statement tells the interpreter to do nothing thus the “while” loop will run until the initializing condition is satisfied, the natural demise of a loop. The control flow will then transfer to the next statement immediately following the “while” loop and this continues the program and this demonstrates the “pass” keyword's purpose to maintain the Python syntax needed to permit continuous and persistent execution flow. You can now see a script typed into the Python programming window, the results can be seen in the Python idle shell below as soon as I run this script, this script demonstrates the “pass” keyword allowing the “for” loop structure to exist with no content. This keyword can also be seen again in an “if” statement, as you can see the script has been run and the “pass” keyword did not interrupt or stop the print function from executing.
Now we're going to free fall into the “break” keyword. A “break” statement must be put inside a loop body and are generally employed in combination with an “if” statement the “break” statement is a method inside Python which terminates the current loop and pushes the control flow out of the loop body onto the next lines of code in the script. The most common use for a “break” statement is in conjunction with a unique condition, different from the looping condition which when met requires a rapid cancellation of the loop structure. An “if” statement is an excellent way to set up a condition, with the “break” keyword it makes it possible to stop a loop before the Python interpreter has it executed through all the planned iterations or before the original loop condition has been met, rolling over into the computer you can see a script typed in the Python programming window with the results of the script printed to the Python idle shell below as soon as I run this code.
The script uses a “for” loop, an “if” statement and the “break” keyword, on the table I’ve also brought a control structure flowchart representing this script, the flowchart demonstrates exactly the path execution that the computing device can do when the script is run. Take note in this situation the loop structure is cancelled early and that the element “0” is not printed to the Python idle shell. Let's run through this flowchart to demonstrate exactly why this has happened, so here we have set up the “list” variable here we have “B” set up as well, now for the elements in “A” so we're going through every single one of these, once we've gone through all of those the loop will come to its natural end. “If” element equals “0”, first time it doesn't, so “print element” that's where we get the result, “1” over here it comes back around “for element in A” so we're looking at “42” now if element equals “0”, it doesn't “print element” that's where we get the “42” from. Coming around once more “for element in A” we're up to the “0”, “if element equals 0”, it does we come across over here and we print this and then we “break” and that “break” stops this iteration cycle completely, we've gotten out of it and now we continue on with our script and that's the reason why this “0” and this “2” is not printed.
The “continue” statement is a method inside Python which will return the control flow back to the start of the loop body, the continue statement effectively rejects all the unexecuted statement within the current iteration of the loop and instead pushes that control flow back to the beginning of the loop body. Thus the “continue” statement is an excellent tool to skip the effects of a loop iteration. The continue statement is a loop control statement with a lot of similarities to a “break” statement, a “continued” statement must be put inside a loop body and usually they're employed in combination with an “if” statement. An “if” statement is an excellent way to set up a condition, the “continue” keyword can be used in all Python loops both “while” loops structures and “for” loop structures however, the effects of a “continuous” statement are somewhat opposite to that of a “break” statement the Python interpreter will not abandon the future iterations of the loop instead it will just skip the remaining lines of code of that particular iteration, if a “continue” statement is executed inside a nested loop the keyword will skip one iteration of the inner loop only.
Twisting into the computer universe, you can see a script typed into the Python programming window and when I execute the script you will then see the result in the Python idle shell below, the script uses a “for” loop, the keyword “in”, it also uses an “if” statement and a “continue” keyword. On the table I have a control structure flowchart representing this script. The flowchart demonstrates exactly the path of execution that the computing device can do when the script is run, take note the loop structure completes the initially planned iterations however the element “0” is not printed to the python idle shell.
Let me run through the flowchart to demonstrate why this has happened. As before we have a “list” variable setup and “B” setup, for elements in “A” if element equals “0”, print element so for “1” and “42” it goes through perfectly fine. Now for the iteration cycle where “0” is the target, for “0” in “A”, if “0” equals “0” which is true print. This “ladies and gentlemen we got him” followed by “we're going to keep searching” and the “continue”. Now this “continue” comes around avoiding the print elements and comes up here and it starts again and that is the reason why “0” isn't printed, this repeats for “2” goes through prints the element comes out and that's the end of this code.
If you desire an overall comparison table of these three keyword come check out our written up guide on the Core Electronics website, scroll down to the bottom or click on our pass links and you'll see it just here. This gives you a great reference of these three keywords and that's it! Until next time stay cosy!
Makers love reviews as much as you do, please follow this link to review the products you have purchased.