Python Programming - Loops - Discussion

Discussion Forum : Loops - General Questions (Q.No. 24)
24.
In Python, what is the purpose of the break statement in a loop?
Skips the remaining code in the loop and moves to the next iteration.
Exits the loop prematurely.
Repeats the loop indefinitely.
Jumps to the next loop iteration.
Answer: Option
Explanation:
The break statement is used to exit a loop prematurely, regardless of the loop's condition. Here's an example of using the break statement in a loop:
for i in range(1, 6):
    if i == 3:
        break
    print(i)
In this example, when the value of i becomes 3, the break statement is executed, causing the loop to terminate prematurely. As a result, only the numbers 1 and 2 are printed before the loop is exited.
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.