130 likes | 138 Views
Inu00a0python,u00a0weu00a0haveu00a0loopu00a0controlu00a0statementsu00a0They canu00a0beu00a0usedu00a0tou00a0alteru00a0oru00a0controlu00a0theu00a0loop executionu00a0flow dependingu00a0onu00a0specifiedu00a0conditions. We have 3 transfer statements in Python namely break, continue and pass. All the 3 statements are discussed in detail along with the example and the flow chart for better understanding.ttt<br><br> To learn more about transfer statements, IBM certified data science courses, or online data science courses, Visit: http://bit.ly/DatascienceCourse<br>or Follow us on our social media platforms mentioned in the above document.
E N D
Transfer Statements • In python, loop statements or transfer statementsallows you to run the code block repeatedly. But sometimes you will need to exit the loop entirely or skip the particular section of the loop when it reaches the specified condition. It can be accomplished using the loop control mechanism. • In python, we have loop control statements can be used to alter or control the loop execution flow depending on specified conditions.
The loop control statements supported by Python are: • break statement:Terminates the loop statement and transfers execution to the statement immediately following the loop. • continue statement:Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating. • pass statement:The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute.
Break statement • The break statement terminates the loop containing it. Control of the program flows to the statement immediately after the body of the loop. • Thebreakstatement can be used in both while and for loops. • If you are using nested loops, the break statement stops the execution of the innermost loop and start executing the next line of code after the block. • Syntax:break
Example • for i in range(10): if i == 7:break else: print(i) • O/P:0 12 3 45 6
Continue Statement • It returns the control to the beginning of the while loop. • The continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop. • Continue statement forces the loop to continue or execute the next iteration. When the continue statement is executed in the loop, the code inside the loop following the continue statement will be skipped and the next iteration of the loop will begin. • The continue statement can be used in both while and for loops. • Syntax: continue
Example • fori in range(10): if i ==7 or i == 4: continue else: print(i) • O/P:0 1 2 3 5 6 8 9
Pass statement • As the name suggests, the pass statement actually doesn't do anything. The Python Pass statement is used when a statement is required syntactically, but you do not want any command or code to be executed. • It's like zero operation, because nothing happens when it's executed. • Pass statement can also be used to write empty loops. Pass is also used for empty control statements, functions and classes. • It is used to write boilerplate code. • Syntax: pass
Example-pass boilerplate code • fori in range(10):passdef f1(): pass var = 0whilevar < 100:pass var += 1print(i) • O/P:9
Thanks for watching! • Follow us for more such content on our social media platforms: • Facebook: Learnbay • Instagram: Learnbay_datascience • Twitter: Learnbay1 • LiknkedIn: Learnbay • Youtube (Tutorials): https://www.youtube.com/channel/UC-ntE_GnjjiUuKYqih9ENYA