PYTHON
PYTHON
Syntax:
The break statement is placed inside the loop, typically within an if
statement to check for a condition. Once that condition is satisfied,
break immediately stops the loop.
When to Use:
It is commonly used in situations where searching for a specific item
in a collection or performing a task has been completed, and
continuing the loop is unnecessary.
Loop Exit:
When the break is executed, the loop stops, and the program
continues with the next statement after the loop block.
Use Case:
The break statement helps improve efficiency by avoiding
unnecessary iterations and enabling early termination, which can be
important in scenarios like searching or processing user input.
5 Marks:
Example:
student = {"name": "John", "age": 20, "major": "Computer
Science"}
print(student["name"]) # Output: John
In this example, the dictionary student contains key-value
pairs like "name": "John", where "name" is the key and "John"
is the value.
5 Marks: