Hello there,
When using loops in python, there are situations when an external factor may influence the way your program runs. You may want your program to exit a loop completely, skip part of a loop before continuing, or ignore that external factor. You can do these actions with break, continue, and pass statements respectively.
The "pass" statement is a no-operation (meaning that it doesn't do anything). The program just continues to the next statement, which is why you get an error.
The "break" statement exits the loops and continues running from the next statement immediately after the loop. In this case, there are no more statements, which is why your program terminates.
The "continue" statement restarts the loop but with the next item.
Sample Code
for letter in 'Python':
if letter == 'h':
break # continue pass
print ('Current Letter :', letter)
Testing the above code for: break, continue and pass will yield different results as seen below:-
If we introduce a "Try ... Except" block and intentionally call an error, only pass will print the error statement at the end of the block statement.
Thanks for reading.
When using loops in python, there are situations when an external factor may influence the way your program runs. You may want your program to exit a loop completely, skip part of a loop before continuing, or ignore that external factor. You can do these actions with break, continue, and pass statements respectively.
The "pass" statement is a no-operation (meaning that it doesn't do anything). The program just continues to the next statement, which is why you get an error.
The "break" statement exits the loops and continues running from the next statement immediately after the loop. In this case, there are no more statements, which is why your program terminates.
The "continue" statement restarts the loop but with the next item.
Sample Code
for letter in 'Python':
if letter == 'h':
break # continue pass
print ('Current Letter :', letter)
Testing the above code for: break, continue and pass will yield different results as seen below:-
If we introduce a "Try ... Except" block and intentionally call an error, only pass will print the error statement at the end of the block statement.
Thanks for reading.
No comments:
Post a Comment