Break, Continue & Exit statement

 

Break Statement:

—It is used to terminate the execution of given loop statements either conditionally or unconditionally.

—Once break statement is executed in loop, the statements specified after break statement are not executed and the loop is terminated immediately.

—If break is used unconditionally, loop is terminated in first iteration otherwise when the condition used for break becomes true.

 

Using unconditional break

for(i=0;i<10;i++)

{

printf(“\n %d”,i);

break;

printf(“\n ok”);

}//end

 

—This loop will be terminated immediately after printing i=0.

—It will not print the word OK.

—Using conditional break

 

for(i=0; ;i++)

{

printf(“\n %d”,i);

if(i>=10)

break;

}//end

 

—This loop will be terminated after printing 0 to 9 values.

 

 

continue

Continue Statement:

—It is used to transfer a control to next iteration of the loop statement.

—Like break statement, it is also used either conditionally or unconditionally.

—Once continue statement is executed in loop, immediately the control is transferred to loop header to evaluate the expression given for next iteration.

—All statements after continue statement in the loop are skipped.

i=1;

while(i<10)

{

printf(“\n %d”, i);

i++;

if(i%2 == 0)

continue;

printf(“\n odd”);

}//end

 

—In this case, continue statement is used to transfer the control to next iteration of loop, if current value of I is even.

 

 

Exit statement

In a similar way, we can jump out of a program by using the library function exit().

When execution reach to exit statement it break out the program & return to the operating system.

The exit() function takes an integer value as its argument.

Normally zero(0) is used to indicate normal termination & a nonzero value to

indicate termination due to some error.

The use of exit() function requires the inclusion of the header file <stdlib.>.

 

 

 

 

 

 

Registration


A password will be e-mailed to you.

Feedback Form

Name (required)

Email (required)

Feedback