Continue and break statements C

We have seen the break statement in relation to the switch-case statements. Similarly we can use break statements in the while and for loops to exit the loop before the predetermined condition. It can be used to handle error or any exceptional condition.

E g.

The above program skeleton shows the use of break. ‘ ’ character entered as input is the terminating condition for inner while loop. But if character entered is ‘*’, break statement causes exit from the while loop.
Break causes exit only from the loop in which it is used. i. e. in our program though while is nested in for, break, exit only while loop, causing execution to resume from statement following while.

There is another statement known as continue statement, which is used to skip remainder of current run through the loop and continue execution of loop from the beginning with the next value of index.

E .g.

The above example illustrates use of continue statement. j runs from 0 to n. if a[2] is less than 0, then continue statement causes remaining body of for loop to be skipped and execution again begins from for statement i.e. j = 3. i.e. all values in the array, which are negative, are skipped.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

C Topics