Teach me break and continue
- User
Teach me break and continue
- Assistant
Break and continue are flow control statements used in programming to manipulate loops.
Break statement: A break statement is used to terminate the execution of a loop. When a break statement is encountered during the execution of a loop, the program control immediately jumps out of the loop and executes the next statement after the loop. This is useful when you want to exit a loop based on a particular condition.
For example, in the following code, the loop exits when the variable i reaches the value 5.
codefor i in range(1,11): if i == 5: break print(i)Output:
code1 2 3 4Continue statement: A continue statement skips the current iteration of a loop and continues with the next iteration. When a continue statement is encountered during the execution of a loop, the program control immediately jumps to the beginning of the loop for the next iteration. This is useful when you want to skip a particular iteration based on a condition.
For example, in the following code, the loop skips the iteration when the variable i equals 5.