English

Differentiate between break and continue statements using examples. - Computer Science (Python)

Advertisements
Advertisements

Question

Differentiate between break and continue statements using examples.

Distinguish Between

Solution

The 'break' statement alters the normal flow of execution as it terminates the current loop and resumes execution of the statement following that loop.

Example:

num = 0
  for num in range(5):
    num = num + 1
    if num == 3:
      break
    print('Num has value ' , num)
  print('Encountered break!! Out of loop')

OUTPUT:

Num has value 1
Num has value 2
Encountered break!! Out of loop

When a 'continue' statement is encountered, the control skips the execution of remaining statements inside the body of the loop for the current iteration and jumps to the beginning of the loop for the next iteration. If the loop’s condition is still true, the loop is entered again, else the control is transferred to the statement immediately following the loop.

#Prints values from 0 to 5 except 3
num = 0
for num in range(5):
  num = num + 1
  if num == 3:
    continue
  print('Num has value ' ,num)
print('End of loop')

OUTPUT:

Num has value 1
Num has value 2
Num has value 4
End of loop
shaalaa.com
Types of Statements in Loop - Break Statement
  Is there an error in this question or solution?
Chapter 6: Flow of Control - Exercise [Page 139]

APPEARS IN

NCERT Computer Science [English] Class 11
Chapter 6 Flow of Control
Exercise | Q 3. | Page 139
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×