Break statement when encountered in the loop it immediately break the loop and program resumes at the line following the loop. It also terminate the case statement in switch.
For example, if we are writing a program to search a name among 1000 of given names, we may have applied loop for this program, . But if the program finds it in 10th iteration and at this point, we don't need to travel for remaining 990 iteration, we just need to terminate the loop and resume the program outside the loop. For this, only break statement is useful.The break statement is used in conjunction with condition.
For example,
In this program, i increases by 1 and when it encounters with number 5, it gets broken and the loop gets terminated.And thus, the output will be 1 2 3 4
Continue statement is used to omit a particular statement and proceed to next iteration. The continue statement escape the statement and move to next iteration.
For example, if we have to escape 10th number from 1000, continue statement is used. It is used in conjunction with condition.
Here, the for loop continues and when i is equal to 5, it step escape the statement and move to another iteration. Hence, from the program the output is 1 2 3 4 6 7 8 9
No comments:
Post a Comment
If you have any doubt, let me know...