Robotics & Artificial Intelligence
Answer
The break statement in Python is used to terminate a loop immediately and transfer the control outside the loop, even if the loop condition is still true.
Example:
numbers = [1, 2, 3, 4, 5]
for n in numbers:
if n == 4:
break
print(n)
print("The end")
Output:
1
2
3
The end
In this example, the loop iterates through the list numbers. When the value 4 is encountered, the break statement is executed, which stops the loop immediately and the control comes out of the loop. After the loop ends, the statement print("The end") is executed.
Related Questions
What would be the result of the following expression?
15 > 12 and 2 < 1- True
- False
- Error
- None of these
Fill in the blanks:
- ……………. is used to repeat a block of code until a certain condition is met.
- ……………. statement in a loop is used to break the execution of the loop.
- ……………. keyword is used to create an empty loop.
- ……………. statement is used to skip the current iteration of the loop and move to the next.
- ……………. loop is used to iterate over a sequence of items.
Write a program to find the smallest number among three numbers by using the nested 'if-else' statements.
Write a Python program to check that if a given number is even or odd.