Explain Break and Continue keyword with appropriate program.


Q.) Explain Break and Continue keyword with appropriate program.

Subject: Object Oriented Programming

I. Introduction

The break and continue keywords are control flow statements in Object Oriented Programming (OOP) languages like Java, C++, Python, etc. They are used to alter the normal flow of a program.

II. Break Keyword

The break keyword is used to terminate the loop or statement in which it is present. After that, the control will pass to the statements that are present after the break statement, if available. If the break statement is used in nested loops, the innermost loop will be terminated, and control will go to the next line following the loop.

Let's illustrate this with a simple program:

for i in range(1, 10):
    if i == 5:
        break
    print(i)

In this program, we have a loop that iterates over a range of numbers from 1 to 10. Inside the loop, we have a condition that checks if the current number is equal to 5. If it is, the break statement is executed and the loop is terminated. The print statement after the break statement will not be executed when i is equal to 5.

The output of the program will be:

1
2
3
4

The break statement is often used when we want to exit a loop prematurely, such as when a certain condition is met.

III. Continue Keyword

The continue keyword is used to skip the current iteration of the loop and control goes to the beginning of the loop for the next iteration. It does not terminate the loop; instead, it just skips the current iteration.

Let's illustrate this with a simple program:

for i in range(1, 10):
    if i == 5:
        continue
    print(i)

In this program, we have a loop that iterates over a range of numbers from 1 to 10. Inside the loop, we have a condition that checks if the current number is equal to 5. If it is, the continue statement is executed and the current iteration of the loop is skipped. The print statement after the continue statement will not be executed when i is equal to 5.

The output of the program will be:

1
2
3
4
6
7
8
9

The continue statement is often used when we want to skip a certain iteration of a loop, such as when a certain condition is met.

IV. Difference between Break and Continue Keywords

Break Keyword Continue Keyword
Terminates the loop Skips the current iteration of the loop
Control goes to the next line following the loop Control goes to the beginning of the loop for the next iteration
Used when we want to exit a loop prematurely Used when we want to skip a certain iteration of a loop

V. Conclusion

The break and continue keywords are important control flow statements in OOP. They allow us to have more control over the flow of our program. The break keyword is used when we want to exit a loop prematurely, while the continue keyword is used when we want to skip a certain iteration of a loop.

VI. Examples

Here are more examples of programs using break and continue keywords:

  1. Break keyword:
for letter in 'Python':
   if letter == 'h':
      break
   print('Current Letter :', letter)

Output:

Current Letter : P
Current Letter : y
Current Letter : t
  1. Continue keyword:
for letter in 'Python':
   if letter == 'h':
      continue
   print('Current Letter :', letter)

Output:

Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : o
Current Letter : n

In the first example, the loop is terminated when the letter 'h' is encountered and in the second example, the letter 'h' is skipped and the loop continues with the next iteration.

Summary

The break and continue keywords are control flow statements used in programming languages like Java, C++, Python, etc. The break keyword is used to terminate a loop or statement, while the continue keyword is used to skip the current iteration of a loop. They provide more control over the flow of a program.

Analogy

Think of a race where the participants encounter obstacles. The break keyword is like a participant who decides to quit the race when faced with a difficult obstacle. The continue keyword is like a participant who skips a particular obstacle and continues with the race.

Quizzes
Flashcards
Viva Question and Answers

Quizzes

What is the purpose of the `break` keyword?
  • To terminate a loop or statement
  • To skip the current iteration of a loop
  • To continue with the next iteration of a loop
  • To exit the program