VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
Break,Pass and Continue.pptx
1. 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.
2. 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.
3. SYNTAX
for variable in sequence:
# Loop body
if condition:
break # Exit the loop if
the condition is met
4. Example of break:
Output:-
#Use of break
statement inside loop -
for val in "string":
if val == "i":
break
print (val)
print ("The end")
5. 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.
6. 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.
7. SYNTAX
for variable in iterable:
# some code
if condition:
continue # Skips the current
iteration and proceeds to the next one
# more code
8. 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")
9. 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
10. 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.
11. SYNTAX
for variable in iterable:
# some code
if condition:
pass # Placeholder statement
that does nothing
# more code
12. 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