1.

Jump Statements in Python

Answer»

  • break: break statements are used to break out of the current loop, and allow execution of the next statement after it as shown in the flowchart below:
>>> for i in range(5):
... print(i)
... if i == 3:
... break
...
0
1
2
3

  • continue: continue statement allows us to send the control back to the starting of the loop, skipping all the lines of code below it in the loop. This is explained in the flowchart below:
>>> for i in range(5):
... if i == 3:
... continue
... print(i)
...
0
1
2
4

  • pass: The pass statement is basically a null statement, which is generally used as a placeholder. It is used to prevent any code from executing in its scope.
for i in range(5):
if i % 2 == 0:
pass
else:
print(i)
Output:
1
3

  • return: return statement allows us to send the control of the program outside the function we are currently in. A function can have multiple return statements, but can encounter only one of them during the course of its execution.
def func(x):
if x == 'Hello':
return True
else:
return False


Discussion

No Comment Found