Loop Control Statements

Loop control statements deviates loops from its normal flow behaviour. All the loop control statements are attached to some condition inside the loop.

break statement

break statement is used to break the loop on some condition.

Example

for i in range(6):
	if i is 3:
		break
	print(i,end=' ')

Output:
0 1 2

Here we have created a loop from 0 to 5. Inside loop we have given a condition that if value of i equals to 3, then break out of loop.

Remember break burst the nearest for loop within which it is contained.

continue statement

continue statement is used to skip a value.

Example

for i in range(6):
	if i is 3:
		continue
	print(i,end=' ')

Output:
0 1 2 4 5

In the code above when the value of i becomes 3, continue forces the loop to skip without further executing any instructions inside the for loop in that current itertion.

pass statement

pass is a null operator. It doesn't do anything. It acts as a placeholder when a statement is required syntactically, but no code needs to be executed.

You can use it when you are undecided what to do when certain condition occurs.

Example

for i in range(6):
	if i is 3:
		pass
	print(i,end=' ')

Output:
0 1 2 3 4 5

<< Loops Functions >>