goto statement in C
The goto statement is unconditional control statement that transfers the cursor of current to the another part of the program. goto statement can be used as—
label;
………
………
………
label:
statement
……..
……..
……..
|
label is any valid identifier and it is followed by semicolon. whenever the goto label is encounter then control is transfer to the define label, that is immediately after the label.
/* program to find the number is even or odd using goto statement*/
< iostream.h >
main()
{
num;
cout<<“Enter A number to check even or odd”;
cin>>num;
(num%2==0)
{
even;
}
{
odd;
}
even:
cout<<“number is even \n”;
end;
odd:
cout<<“Number is ODD \n”;
end;
:
cout<<“\n”;
0;
}
|
we can be place label anywhere. if the label is after goto statement then the control is transferred forward and this is know forward jump and if the label is before goto statement then control is transfer to the backwards this is known as backward jumping.
note: The control can be transfer with in the function using goto statement not outside of function
Break keyword in C
Break statement used inside in any loop or switch statements. Sometimes it is necessary to go outside from the loop even before the loop condition becomes false. in such conditions when terminates the loop break statement is used. This break statement cause an immediate exit from the loop in which break statement appear. example it is written as
when break statement is encounter control is transfer immediately out from the loop. If break written a nested loop structure then it causes exit from the innermost loop.
Let take Example to understand the break statement uses
/* program to understand the use of break*/
< iostream.h >
main()
{
i;
(i=0;i<=10;i++)
{
(i==5)
;
cout << i;
}
0;
}
|
in this example its normally executes 11 (0 to 10) times but here the value of i becomes 5 break is encounter and loop is terminated. .