22
DecJump Statements in C: break, continue, goto, return
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:break
continue
goto
return
1. 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:
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 in the C Compiler 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
Read More - Top 50 Mostly Asked C Interview Questions and Answers
2. 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 the continue statement in C
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
3. Goto Statement in C
The goto statement is used to jump to a certain location within a function from any other location. Using this technique, the program control is moved to a labeled statement inside the same function.
The below image depicts the working of the 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 in the C Editor 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.
4. 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
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
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.