Jump Statements in C – break, continue, goto, return

Jump Statement makes the control jump to another section of the program unconditionally when encountered. It is usually used to terminate the loop or switch-case instantly. It is also used to escape the execution of a section of the program.

In our previous tutorial of IF- ELSE statements we saw that there are 4 jump statements offered by C Programming Language:

  • Break
  • Continue
  • Goto
  • Return

In this tutorial, we will discuss Jump Statements in detail.


1. Break Jump Statement

A break statement is used to terminate the execution of the rest of the block where it is present and takes the control out of the block to the next statement.

It is mostly used in loops and switch-case to bypass the rest of the statement and take the control to the end of the loop. The use of the break keyword in switch-case has been explained in the previous tutorial Switch – Control Statement.

Another point to be taken into consideration is that the break statement when used in nested loops only terminates the inner loop where it is used and not any of the outer loops.

Let’s implement an example to understand how break statement works in C language.

#include <stdio.h>

int main() {
  int i;
  for (i = 1; i <= 15; i++) {
    printf("%d\n", i);
    if (i == 10)
      break;
  }
  return 0;
}
Output:-
1
2
3
4
5
6
7
8
9
10

In this program, we see that as soon as the condition if(i==10) becomes true the control flows out of the loop and the program ends.


2. continue Jump Statement

The continue jump statement like any other jump statement interrupts or changes the flow of control during the execution of a program. Continue is mostly used in loops.

Rather than terminating the loop it stops the execution of the statements underneath and takes control to the next iteration.

Similar to a break statement, in the case of a nested loop, the continue passes the control to the next iteration of the inner loop where it is present and not to any of the outer loops.

Let’s implement an example to understand how continue statement works in C language.

#include <stdio.h>

int main() {
  int i, j;
  for (i = 1; i < 3; i++) {
    for (j = 1; j < 5; j++) {
      if (j == 2)
        continue;
      printf("%d\n", j);
    }
  }
  return 0;
}
Output:-
1
3
4
1
3
4

In this program, we see that the printf() instruction for the condition j=2 is skipped each time during the execution because of continue. We also see that only the condition j=2 gets affected by the continue. The outer loop runs without any disruption in its iteration.


3. goto Jump Statement

goto jump statement is used to transfer the flow of control to any part of the program desired. The programmer needs to specify a label or identifier with the goto statement in the following manner:

goto label;

This label indicates the location in the program where the control jumps to. Let’s implement an example to understand how goto statement works in C language.

#include <stdio.h>

int main() {
  int i, j;
  for (i = 1; i < 5; i++) {
    if (i == 2)
      goto there;
    printf("%d\n", i);
  }
  there:
    printf("Two");
  return 0;
}
Output:-
1
Two

In this program, we see that when the control goes to the goto there; statement when i becomes equal to 2 then the control next goes out of the loop to the label(there: ) and prints Two.


4. Return Jump Statement

Return jump statement is usually used at the end of a function to end or terminate it with or without a value. It takes the control from the calling function back to the main function(main function itself can also have a return).

An important point to be taken into consideration is that return can only be used in functions that is declared with a return type such as int, float, double, char, etc.

The functions declared with void type does not return any value. Also, the function returns the value that belongs to the same data type as it is declared. Here is a simple example to show you how the return statement works.

Let’s implement an example to understand return statement in C language.

#include <stdio.h>

char func(int ascii) {
  return ((char) ascii);
}
int main() {
  int ascii;
  char ch;
  printf("Enter any ascii value in decimal: \n");
  scanf("%d", & ascii);
  ch = func(ascii);
  printf("The character is : %c", ch);
  return 0;
}
Output:-
Enter any ascii value in decimal: 
110
The character is : n

In this program, we have two functions that have a return type but only one function is returning a value[func()] and the other is just used to terminate the function[main()].

The function func() is returning the character value of the given number(here 110). We also see that the return type of func() is char because it is returning a character value.

The return in main() function returns zero because it is necessary to have a return value here because main has been given the return type int.


Helpful Links

Please follow C Programming tutorials or the menu in the sidebar for the complete tutorial series.

Also for the example C programs please refer to C Programming Examples.

All examples are hosted on Github.


Recommended Books


An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! ?

Recommended -

Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Fun
3 years ago

Good

Tata bhargav ram Kumar

Jumping

2
0
Would love your thoughts, please comment.x
()
x
Index