Break,Pass and Continue.pptx

144 views 13 slides Feb 07, 2024
Slide 1
Slide 1 of 13
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13

About This Presentation

Break,Pass and Continue in python


Slide Content

BREAK Statements It terminates the current loop and resumes execution at the next statement, just like the traditional break statement in C. The most common use for break is when some external condition is triggered requiring a hasty exit from a loop. The  break  statement 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.

Advantages Of Using Break Statements: Using a break statement is the most evident and easiest way to exit out of intensely nested loops. It increases the code readability as it is used to avoid any complex nested loops. it increases the control over the program flow.

SYNTAX for variable in sequence: # Loop body if condition: break # Exit the loop if the condition is met

Example of break:                                                                                  Output :- #Use of break statement inside loop - for val in "string":        if val == " i ":             break        print ( val )  print ("The end")

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. The  continue  statement can be used in both  while  and  for  loops.

Advantages Of Using Continue Statements: Continue statements make your code more reliable and readable by skipping the conditions based on specific conditions. It is a versatile tool that can be incorporated into the use of for and while loops. Complex programs take the most advantage out of the continued statements. It customizes the manner of the code by specifying the lines or iterations to be skipped in the loops.

SYNTAX for variable in iterable : # some code if condition: continue # Skips the current iteration and proceeds to the next one # more code

Example of continue: Code-                                                                                                             # Program to show the use of continue statement inside loops for val in "string": Output-        if val == " i ":              continue        print ( val ) print ("The end")

PASS STATEMENT It is used when a statement is required syntactically but you do not want any command or code to execute. The  pass  statement is a  null  operation; nothing happens when it executes. The  pass  is also useful in places where your code will eventually go, but has not been written yet 

Advantages Of Using Pass Statements: The pass statement is used as a placeholder when the program does not yet have any functionality or it may be in the construction phase. Allows to write code that compiles without providing an exact implementation. For instance, including at least one method is necessary when creating a new class. The pass statement allows you to create an empty method without initiating any syntax error.

SYNTAX for variable in iterable : # some code if condition: pass # Placeholder statement that does nothing # more code

Example of pass: CODE:-                                         OUTPUT-          pass is just a placeholder for             #functionality to be added later.     sequence=({'p', 'a', 's', 's'}                     for val in sequence:            pass                                                                                                           
Tags