break and continue Statement

break and continue Statements

sometimes, we need to alter the normal flow of a loop in response to the occurrence of an event. In such a sitution, we may either want to exit the loop of continue with the next oteration of the loop skipping the remaining statements in the loop. The break statement enables us to exit the loop and transfer the control to the statement following the body of the loop. The continue statement is used to transfer the control to next iteration of the loop. When the continue statement is executed, the code that occurs in the body of the loop after the continue statement is skipped.
To illustrate the use of break statement, we examine the function printSquares intened to print squares of the integers entered by the user until a null string is encountered. This function uses a while loop. The condition in the while loop has been set as True. If the user enters a null string, the control exit the loop and moves to the statement following the while loop, thus, the function terminates with the message 'End of input!!'. in the other case, when the user input is not null, the input string is converted to an integer and the square of the integer so obtained is printed.
def printSquares():
while True:
numString = input('Enter an integer, to end press Enter: ')
if numString == '':
break
number = int(numString)
print(number, '^ 2 =', number ** 2)
print('End of input!!')

in the sscript percentage1 we wish to compute the overall percentage of marks obtained by a student. As the number of subjects on which examination was conducted is not known beforehand, we use a while loop . When the while loop is executed, the user is prompted to enter the marks obtained in different subjects. If the user responds with a null string, the break statement is executed which results in termination of the loop, and the control moves for computationof percentage .
def main(): totalMarks = 0
nSubjects = 0
while True:
marks = input('Marks for subject ' + str(nSubjects + 1) + ': ')
if marks == '': # End of input
break
marks = float(marks)
if marks < 0 or marks > 100:
print('INVALID MARKS !! ')
continue # Marks to be entered again
nSubjects = nSubjects + 1
totalMarks += marks
percentage = totalMarks / nSubjects
print('Total marks: ', int(totalMarks))
print('Number of Subjects: ', nSubjects)
print('Percentage: ', round(percentage,2))
if __name__=='__main__':
main()

Howwever, if marks entered by the user are outside the range [0, 100], the user is prompted again to enter marks. Thus in the case of invalid marks =, the use of continue statement marks it possible to skip the statements that appear after the continue statement in the while loop and continue with the next iteration of the loop for taking the next input from the user. Sample output on executing the script percentage is given below:
Marks for subject 1: 60
Marks for subject 2: 80
Marks for subject 3: 280
INVALID MARKS !!
Marks for subject 3: 70
Marks for subject 4: 800
INVALID MARKS !!
Marks for subject 4: 80
Marks for subject 5:
Total marks: 290
Number of Subjects: 4
Percentage: 72.5

0 Response to "break and continue Statement"

ads