Loop Control Statements

Loop Control Statements

Loop Control Statements

Loop Control Statements:

In Python, the loop control statements are break, continue, and pass.

  • break statement: The break statement is used to exit a loop prematurely. When executed within a loop, it immediately terminates the loop and control is transferred to the statement that follows the loop. The break statement can be used with both for and while loops. Here’s an example:
				
					for i in range(10):
    if i == 5:
        break
    print(i)
				
			

Output:

				
					0
1
2
3
4 
				
			
  • continue statement: The continue statement is used to skip over the current iteration of a loop and move on to the next iteration. When executed within a loop, it skips any remaining code in the loop’s body for the current iteration and moves on to the next iteration. The continue statement can be used with both for and while loops. Here’s an example:
				
					for i in range(10):
    if i == 5:
        continue
    print(i)
				
			

Output:

				
					0
1
2
3
4
6
7
8
9
				
			
  • pass statement: The pass statement is used as a placeholder where a statement is required syntactically, but no action is needed. It is often used when you are working on code that is not yet complete or when you need to create a placeholder function that you will implement later. Here’s an example:
				
					for i in range(10):
    if i == 5:
        pass
    print(i)
				
			

Output:

				
					0
1
2
3
4
5
6
7
8
9
				
			

Note that in the above example, the pass statement has no effect on the loop’s execution, and it simply acts as a placeholder.

Join To Get Our Newsletter
Spread the love