Break statement


In C,the break statement is used to terminate the execution of the nearest enclosing loop which it appears.The break statement is widely used with for loop,while loop and do while loop.When compiler encounters a break statement,the control passes to the statement that follows the loop in which the break statement appears.The keyword break followed with a semicolon.
break;
In switch statement if the break statement is missing then every case from the matched case label till end of the switch,including the default,is executed.the break statement is used to exit a loop any point within its body,bypassing its normal termination expression.when the break statement is encountered inside a loop,the loop is immediately terminated, and program control is passed to the next statement.


Example:

 #include<stdio.h>

void main()

{

int i=0;

while(i<=10)

{

if(i==5)break;

printf("\n %d",i)

i=i+1;

}

getch();

}

Goto statement


The goto statement is used to transfer control to a specified label.The label can be placed anywhere in the program either before or after the goto statement.Whenever the goto statement is encountered the control is immediately transferred to the statements.if the label is placed after the goto statement,then it is called a forward jump and in case it is located before the goto statement,it is said to be a backward jump. If condition Then goto label


Example:

#include<stdio.h>
void main()
{
int num,sum=0;
printf("\n enter the number");
scanf("%d",&num);
if(num!=999)
{
if(num<0)
goto ;
sum=num;
goto;
}
printf("\n sum of the numbers entered by the user is=%d",sum);
getch();
}


       

Comments