C langugae : Unconditional Jumps

Jump statements interrupt the sequential execution of statements, so that execution continues at a different point in the program. A jump destroys automatic variables if the jump destination is outside their scope. There are four statements that cause unconditional jumps in C: break , continue, goto, and return.
The general form of the if statement is
Break Statement
The break statement goes in the body of the for loop. Programmers rarely put break on a line by itself, and it almost always comes after an if test. If the break were on a line by itself, the loop would always quit early, defeating the purpose of the for loop.
Expression : - break;
Thus the break statement can be used to end the execution of a loop statement at any position in the loop body. For example, the while loop in may be ended either at the user's request (by entering a non-numeric string), or by a numeric value outside the range that the programmer wants to accept.
The following program shows what can happen when C encounters an unconditional break statement. A break causes an exit from only the innermost loop.
// To Find the Factorial Series upto .................n terms & use of break statements
#include< stdio.h >
#include< conio.h >
void main()
{
int r,c=1,k=1,s;
printf(" enter the number");
scanf("%d",&r);
clrscr();
for(s=1;s {
if(c>=2)
k=s-c;
for(c=1;c {
k=k+k*c;
}
printf("%d\n",k);
if(c>7)
{
printf("Factorial number is out of Integer range 32767 to -32767");
break;
}
}
getch();
}
The following shows you the result of running this program. Notice that the break immediately terminates the for loop. It asks users if they want to see another number. If so, the for loop continues its next iteration. If not, the break statement terminates the for loop.
Continue Statement
This statement can be used in the three loop forms. When encountered, it causes the rest of an iteration to be skipped and the next iteration to be started. If the continue statement is inside nested structures, it affects only the innermost structure containing it.The continue statement can be used only within the body of a loop, and causes the program flow to skip over the rest of the current iteration of the loop:
continue;
The continue statement works somewhat like the break statement. Instead of forcing termination, however, continue forces the next iteration of the loop to take place.skipping any code in between. For the for loop, continue causes the increment and then the conditional test portions of the loop to execute. For the while and do-while loops, program control passes to the conditional tests.For example, the following program print the Fibonacci Sequence of a given n terms.
\\ To evaluate the Fibonacci Sequence of a given n terms using continue statement
#include
#include
void main()
{
int r=0,c=1,s=1,x,t;
printf(" enter the number");
scanf("%d",&x);
clrscr();
printf("%d\n",r);
printf("%d\n",c);
for(t=1;t {
s=c+r;
r=c;
c=s;
if(t==((x-2)/2))
{
printf("This is Middle point of Fibonacci Sequence\n");
continue;
}
printf("%d\n",s);
}
getch();
}

An advantage of using continue in this case is that you can eliminate one level of indentation in the main group of statements. Being concise can enhance readability when the statements are long or are deeply nested already.
Another use for continue is as a placeholder. For example, the following loop reads and discards input up to, and including, the end of a line.
Such a technique is handy when a program has already read some input from a line and needs to skip to the beginning of the next line. The problem is that the lone semicolon is hard to spot. The code is much more readable if you use
goto statements
The goto statement causes an unconditional jump to another statement in the same function. The destination of the jump is specified by the name of a label:
goto label_name;
A label is a name followed by a colon:
label_name: statement
A statement label is named just as variables are . A statement label cannot have the same name as a C command, a C function, or another variable in the program. If you use a goto statement, there must be a statement label elsewhere in the program that the goto branches to. Execution then continues at the statement with the statement label.
The statement label precedes a line of code. Follow all statement labels with a colon (:) .A statement label is optional, unless you have a goto that branches to one.
The goto statement has two parts—the goto and a label name. The label is named following the same convention used in naming a variable, as in this example:
//To calculate the total salary using goto statement
#include< stdio.h >
#include< conio.h >
void main( )
{
int sa,da=0,ta=1,hra=0,pf=0,tsa=0;
clrscr();
printf("enter the salary");
scanf("%d",&sa);
if(sa>=100)
goto a;
else if(sa<100 && sa>=50)
goto b;
if(sa<50 && sa>=10)
goto c;
else
goto d;
a:
da=(sa*30)/100;
ta=(sa*25)/100;
hra=(sa*35)/100;
pf=(sa*40)/100;
tsa=sa+da+ta+hra-pf;
goto next;
b:
da=(sa*25)/100;
ta=(sa*20)/100;
hra=(sa*30)/100;
pf=(sa*35)/100;
tsa=sa+da+ta+hra-pf;
goto next;
c:
printf("enter the salary");
scanf("%d",&sa);
da=(sa*20)/100;
ta=(sa*15)/100;
hra=(sa*25)/100;
pf=(sa*30)/100;
tsa=sa+da+ta+hra-pf;
goto next;
d:
da=(sa*15)/100;
ta=(sa*10)/100;
hra=(sa*20)/100;
pf=(sa*25)/100;
tsa=sa+ta+hra-pf;
goto next;
next:
printf("Basic Salary = %d\n",sa);
printf("Dally allowance = %d\n",da);
printf("Taravelling allowance = %d\n",ta);
printf("House Rent Allowance = %d\n",hra);
printf("Providend Fund %d\n",pf);
printf("Total Salary = %d\n",tsa);
getch();
} The advantages of using them are that their names tell you what they are supposed to do and that, because they don't use labels, there is no danger of putting a label in the wrong place.
The goto statement permits only local jumps; that is, jumps within a function. C also provides a feature to program non-local jumps to any point in the program, using the standard macro setjmp( ) and the standard function longjmp( ). The macro setjmp( ) marks a location in the program by storing the necessary process information, so that execution can be resumed at that point at another time by a call to the function longjmp( ).
return Statement
The return statement ends execution of the current function, and jumps back to where the function was called:
return [expression];
expression is evaluated and the result is given to the caller as the value of the function call. This return value is converted to the function's return type, if necessary.
A function can contain any number of return statements:
// Return the smaller of two integer arguments.
#include< stdio.h >
#include< conio.h >
void main()
{
int x=0;
x=fun();
clrscr();
printf("max =%d",x);
getch();
}
fun()
{
int r,c;
printf("enter two numbers");
scanf("%d %d",&r,&c);
return(r>c?r:c);
}
The parentheses do not affect the behavior of the return statement . However, complex return expressions are often enclosed in parentheses for the sake of readability.
A return statement with no expression can only be used in a function of type void. In fact, such functions do not need to have a return statement at all. If no return statement is encountered in a function, the program flow returns to the caller when the end of the function block is reached.


Free Web Hosting