Browse Tutorials
Jump Statements in C: break, continue, goto, return

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

08 Dec 2023
Beginner
1.62K Views
12 min read
Learn via Video Course & by Doing Hands-on Labs

C Programming For Beginners Free Course

Jump Statements in C: An Overview

Jump statements are control flow statements that let you change the order in which programs execute. These statements provide flexibility and control over program logic to the programmer. The following article in this C tutorial gives a detailed insight into types of jump statements and explains them in detail. For more insights, you can even check our C Certification Program.

Types of Jump Statements in C

There are 4 types of jump statements in C language:
  1. break
  2. continue
  3. goto
  4. return
In the section below, we will discuss all the jump statements in C. But, we will discuss the break and continue statements in C in much detail in the next tutorial, Break and Continue Statements in C.

Break Statement in C

The break statement in C language is used to exit from a loop or switch statement, prematurely, before the loop or switch block has been fully executed. When a break statement is encountered inside a loop, it immediately terminates the loop, and control is transferred to the next statement outside the loop. Similarly, when a break statement is encountered inside a switch block, it terminates the block, and control is transferred to the next statement outside the block.

The below image depicts the working of the break statement in C:

break statement in c flowchart

Example to Understand Break Statement in C:


#include <stdio.h>

int main() {
 for (int i = 1; i <= 10; i++) {
 if (i == 5) {
 break; // Terminates the loop when i is equal to 5
 }
 printf("%d\n", i);
 }

 return 0;
}

This C code iterates from 1 to 10 using a for loop. It determines whether the value of i inside the loop equals 5. If it is equal to 5, the break statement is carried out, which abruptly ends the loop. It outputs numbers from 1 to 4 (inclusive) as a result, and when i equals 5, it ends the loop.

Output

1
2
3
4

Continue Statement in C

In C programming, the continue statement is used to skip the current iteration of the C language’s loop (for loop, while loop, or do-while loop) and move to the next iteration. When the continue statement is encountered inside a loop, the control of the program immediately goes to the loop's increment/decrement part, skipping the remaining statements in the current iteration. The loop then continues with the next iteration.

The below image depicts the working of thecontinue statement in C

continue statement in c language

Syntax

//loop statements 
continue; 
//some lines of the code which is to be skipped

Example to Understand Continue Statement in C


#include <stdio.h>

int main() {
 for (int i = 1; i <= 10; i++) {
 if (i == 5) {
 continue; // Skips the rest of the loop body for i equal to 5
 }
 printf("%d\n", i);
 }

 return 0;
}

This C program iterates from 1 to 10 times using a for loop. The continue statement is encountered when i is equal to 5, skipping the remainder of the loop body for that iteration. As a result, it only prints the numbers 1 through 4 and 6 through 10, skipping out 5.

Output

1
2
3
4
6
7
8
9
10

Goto Statement in C

The goto statement is used to jump to a certain location within a function from any other location. The program control is moved to a labeled statement inside the same function using this technique.

The below image depicts the working of the goto statement in C

goto statement in c

Syntax

goto label;
// ...
label:
// Statement(s) to execute

Example


#include <stdio.h>
int main() {
 int choice;
 printf("Select an option:\n");
 printf("1. Print 'Hello'\n");
 printf("2. Print 'World'\n");
 printf("3. Exit\n");
 choice=3;
 switch (choice) {
 case 1:
 printf("Hello\n");
 break;
 case 2:
 printf("World\n");
 break;
 case 3:
 printf("Exiting...\n");
 goto end; // Jump to the 'end' label to exit
 default:
 printf("Invalid choice\n");
 break;
 }
 // This is where the 'end' label is defined
 end:
 printf("Program ends here.\n");
 return 0;
}

This C program gives the user a menu so they can select an option. Depending on what they select, the program either prints "Hello," "World," or ends. When the user chooses the exit option (3), the goto statement is used to leap to the 'end' label, enabling the program to end and output "Programme ends here."

Output

Select an option:
1. Print 'Hello'
2. Print 'World'
3. Exit
3
Exiting...
Program ends here.

Return Statement in C

When a function is finished running, the return statement in C is used to give the caller a value. It is often used to return a result to the caller code.

The below image depicts the working of the return statement in C

Return Statement in C

Syntax

return expression;

Example


#include <stdio.h>
int multiply(int a, int b) {
 return a * b;
}
int main() {
 int result = multiply(5, 7);
 printf("The product is: %d\n", result);
 return 0;}

The multiply function is a basic C function that accepts two integer inputs, multiplies them with the * operator, and returns the outcome. The multiply function in the main function executes multiply(5, 7), stores the outcome in the result variable, & prints "The product is: [result]" to the console, which will display "The product is: 35" because 5 times 7 is 35.

Output

The product is: 35

FAQs:

1. What is a Jump Statement?

A jump statement in C language allows a program to alter the normal flow of execution and skip or repeat specific sections of code.

2. What are the 4 Jump Statements in C?

The C language has four jump statements: "break," "continue," "return," and "goto."

3. What are the Jump keywords in C?

The precise words break, continue, return, and goto that are utilized to construct jump instructions in the language are referred to as the jump keywords in C.

Summary

Jump statements in C programming, like 'break' and 'continue,' provide programmers more power to manage the flow of their code. This article explores these commands, including "return" and "goto," looking at how they affect the execution pathways. 'Break' abruptly ends loops, 'continue' skips iterations, 'return' ends functions, and 'goto' moves to specific labels inside functions. These jump-related keywords are crucial instruments for modifying program logic and improving code effectiveness. You can learn more about C programming in our C Certification Training.

Share Article
About Author
Sakshi Dhameja (Author and Mentor)

She is passionate about different technologies like JavaScript, React, HTML, CSS, Node.js etc. and likes to share knowledge with the developer community. She holds strong learning skills in keeping herself updated with the changing technologies in her area as well as other technologies like Core Java, Python and Cloud.

Accept cookies & close this