Understanding Jump Statements in C language – Goto, Switch. Break, Continue

The jump statements in C language unconditionally transfer program control within a function. Such statements are used on programs where certain results are waited to made a unconditional jump to a certain part of the program control.

There are four types of jump statements; some where you may find Switch – Case statement as conditional statement, but we are describing it as Jump Statements.

The goto statement

The goto statement in C language allows making jump to another point in the program.

goto tcs;
tcs:

tcs is known as label. It is a user defined identifier. After the execution of goto statement, the control transfers to the line after label tcs.

To further clarify the concept of goto statement study the C Source Code below -

#include <stdio.h>

int main (void)// Print value 0 to 9

{

a = 1;

loop:;  // label stament

printf ("\n%d",a);

a++;

if (a < 10) goto loop ; // jump statement

retrun 0;

}

Switch Statement

Switch is a selection statement provided by C. It has a built in multiple – branch structure and work similar to if else ladder generally. The input value to the switch statement construct is an int or char variable. Note that no float or any other data is allowed. This input variable is compared against a group of integer constant. All the statements in and after the case in which there is a match is executed until a break statement is encountered or end of switch is reached. We are also allowed to give a default case which will be executed if no other statement match is found. Also use of standard indenting is recommended.

General representation of switch in C can be -

switch (input) {

case constant1 :

statement1;

break;

case constant2 :

statement2;

break;

case constant3 :

statement3;

break;

.

... n cases

case constant n+1 :

statement n+1;

break;

case default :

statement n+2;

break;

Common use of switch is in for menus and other selection interfaces.

Also break in the switch statements is optional and if no break is present in a case the flow of statements transfer to next case and so on till the break statement is encountered or switch case ends.

#include <stdio.h>

int main (void)

{

char ch;

printf ("\nEnter Character A,B or C: ");

ch = getchar ();

switch (ch)

{

case 'A' : printf ("You Entered A");

break;

case 'B' : printf ("You Entered B");

break;

case 'C' : printf ("You Entered C");

break;

default : printf ("You Didnot Entered A, B or C");

}

return 0;

}

The break statement

The break statement, when executed in a switch structure, provides an immediate exit from the switch structure. Similarly, Break statement when encountered within a loop immediately terminates the loop by passing condition check and execution transfer to the first statement after the loop.

break statement is a jump statement in C and is generally used for breaking from a loop or breaking from a case as discussed in switch statement.

#include <stdio.h>

int main ()

{

for (int i = 0; i<100; i++)

{

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

if (i == 10); // This code prints value only upto 10.

break;

}

return 0;

}

The continue statement

The continue statement is used in loops and causes a program to skip the rest of the body of the loop.

while (condition)
{
Statement 1;
If (condition)
continue;
statement;
}

The continue statement skips rest of the loop body and starts a new iteration.

#include <stdio.h>

int main ()

{

for (int i = 0; i<100; i++)

{

if (i == 10); // This code prints value only upto 9 even though loop executes 100 times.

continue ;

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

}

return 0;

}

The exit ( ) function

The execution of a program can be stopped at any point with exit ( ) and a status code can be informed to the calling program. The general format is

exit (code) ;

where code is an integer value. The code has a value 0 for correct execution. The value of the code varies depending upon the operating system.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>