What is jumping statements or Transfer statements?

C provides three transfer statements (also known as jump statements) that allow you to interrupt the normal processing of flow control statements. These include break, continue and goto statements. we shall now be discussing break , continue and goto statements.





The break Statement:

The break statement terminates the execution of the loop in which it is defined and the control is transferred immediately to next executable statement after the loop. The break statement is normally used with either while , do-while and for or switch statement. It is mostly used to exit early from the loop by skipping are remaining statements of loop or switch control statements.

It is simply written as :

break;

The break statement causes a transfer of control out of the loop instantly, without waiting to get back to the conditional test, and control passes to the first statement after loop.





A program to show importance of break?





#include<stdio.h>

#include<conio.h>

main()

{

int i;

clrscr();

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

{

printf("%d\t",i);

if(i==5)

{

printf("

bye");

break;

}}

getch();

}





OUTPUT:

1 2 3 4 5

bye

Explanation:Here, you can see that we have for loop such that initial value of i=1 and final value is 10. Now when (i==5) we have break statement this causes transfer of control out of the loop. It breaks the control only from the for loop in which it is placed.

The break statement, transfer the control out of the loop in which it is being placed , it has nothing to do with the rest of the surrounding looping statements.





The continue statement:

Like the break statement, the continue statement also skip the remaining statements of the body of the loop where it is defined but instead of terminating the loop, the control is transferred to the start of the next loop iteration. The program continues with the next iteration if there is one, and if not with the statement following the end of the loop block.

The continue statement when used in the while and do while loop causes the test condition to be evaluated immediately after it. But in case of for loop, the increment/ decrement expression evaluates immediately after the continue statement and then the test condition is evaluated.

It is generally written as:

continue;





A program to demonstrate how continue work?





#include<stdio.h>

#include<conio.h>

main()

{

int i;

clrscr();

printf("

ODD numbers between 1 to 20

");

for(i=1;i<=20;i++)

{

if(i%2==0)

continue; /*skip if number is even */

printf("%d\t",i);

}

getch();

}





OUTPUT:

ODD number between 1 to 20

1 3 5 7 9 11 13 15 17 19





Explanation: On execution, the for loop will executed until the condition i<=20 evaluates to true . During each iteration, the if statement checks whether the loop counter variable i is divisible by 2 or not. If it is divisible , the continue statement will execute which skips the statement printf("%d\t",i); placed between the continue and closing brace of the for loop continues with the next iteration. Otherwise, the value of i is printed , Thus , it result in a list of odd numbers between 1 to 20.





The goto statement:

The goto statement is an application of switch statement. The goto statement is an unconditional control transfer statement that causes the control to jump to a different location in the program without checking any condition as specified by the goto. It is normally used to alter the normal sequence of program execution by transferring control to some other part of the program Like switch, break and continue statements, the goto statement also causes the control to jump over the statements. So it is called a control transfer statement.

It is generally written as:

goto label;

Here, label is a valid identifier used to label the target statement to which control will be transferred. Control may be transferred to any other statement within the current function . you cannot jump between functions. The target statement must be followed by a colon(:). It is written as:

label : statement

The label can be set anywhere in the program above or below the goto statement so the goto statement causes the control to be shifted either in a forward direction or in backward direction which is controlled by the position of the label which is called jump .





There are two types of jumps

*Forward jump

*Backward jump





Sometimes it may be necessary to skip a set of statements in a program. To accomplish this, process we use a goto statement. When the label is defined sfter the goto statement the process of skipping is called forward jump . Forward jump using goto can be also be used for an early exit from a loop.

Example of forward jump is ;

goto label

.............

............

............

label:

statement;

Backward jump means to shift the control to a statement which is executed before. This is accomplished by a goto statement in which label used is defined before the goto statement. It causes a certain set of statements to be repeated again and again . If this statement is used without any condition it causes repetition of part of program infinitely, which form an infinite loop.

Example of backward jump is ;

label:

statement;

.........................

........................

...........................

goto label;





The goto statement when combined with a decision making statement i.e. if statement works similar to looping statements ,

Consider the following example :

#include<stdio.h>

#include<conio.h>

main()

{

int i=1;

clrscr();

up : printf("

hello ");

i++;

if(i<=5)

goto up;

getch();

}





The statement causes hello to be printed 5 times which works similar to a loop . Thus if statement used with goto statement froms loop.





A program of goto statement to display numbers from m to n provided m<n?

#include<stdio.h>

#include<conio.h>

main()

{

int m,n;

clrscr();

printf("enter two numbers: ");

scanf("%d%d",&m,&n);

if(m<n)

goto output1;

else

goto output2;

output1:

printf("out of %d and %d ,%d is smaller ",m,n,m);

goto stop;

output2:

printf("out of %d and %d, %d is smaller ",m,n,m);

stop :

getch();

}

OUTPUT:

enter two numbers : 30 50

out of 30 and 50, 30 is smaller









APPLICATIONS OF GOTO STATEMENT:

1. The goto statements is basically used to skip or repeat a set of statements.

2. It is used to exit from deeply nested loop when an error occurs.





THANKS



