In C, jump statements are used to jump from one part of the code to another altering the normal flow of the program. They are used to transfer the program control to somewhere else in the program.
In this article, we will discuss the jump statements in C and how to use them.
Types of Jump Statements in C
There are 4 types of jump statements in C:
- break
- continue
- goto
- return
1. break in C
The break statement exits or terminates the loop or switch statement based on a certain condition, without executing the remaining code.
Syntax of break in C
break;
Flowchart of break Statement
Uses of break in C
The break statement is used in C for the following purposes:
- To come out of the loop.
- To come out from the nested loops.
- To come out of the switch case.
Note: If the break statement is used inside an inner loop in a nested loop, it will break the inner loop without affecting the execution of the outer loop.
Example of break Statement
The statements inside the loop are executed sequentially. When the break statement is encountered within the loop and the condition for the break statement becomes true, the program flow breaks out of the loop, regardless of any remaining iterations.
C
// C program to illustrate the break in c loop
#include <stdio.h>
int main()
{
int i;
// for loop
for (i = 1; i <= 10; i++) {
// when i = 6, the loop should end
if (i == 6) {
break;
}
printf("%d ", i);
}
printf("Loop exited.\n");
return 0;
}
Output1 2 3 4 5 Loop exited.
Explanation:
- Loop Execution Starts and goes normally till i = 5.
- When i = 6, the condition for the break statement becomes true and the program control immediately exits the loop.
- The control continues with the remaining statements outside the loop.
Note: The break statement only break a single loop per usage. If we want to exit multiple loops in nested loops, we have to use multiple break statements for each of the loop.
The break statement is also used inside the switch statement to terminate the switch statement after the matching case is executed.
2. Continue in C
The continue statement in C is used to skip the remaining code after the continue statement within a loop and jump to the next iteration of the loop. When the continue statement is encountered, the loop control immediately jumps to the next iteration, by skipping the lines of code written after it within the loop body.
Syntax of continue in C
continue;
Note: Just like break, the continue statement also works for one loop at a time.
Flowchart of continue Statement
Example of continue Statement
C
// C Program to illustrate the continue statement
#include <stdio.h>
int main()
{
int i;
// loop
for (i = 0; i < 5; i++) {
if (i == 2) {
// continue to be executed if i = 2
printf("Skipping iteration %d\n", i);
continue;
}
printf("Executing iteration %d\n", i);
}
return 0;
}
OutputExecuting iteration 0
Executing iteration 1
Skipping iteration 2
Executing iteration 3
Executing iteration 4
Explanation: The for loop iterates from 0 to 4. Inside the loop, we check if i is equal to 2. If the condition is true, the continue statement is executed, and it skips the remaining code within the loop for that iteration. If the condition is false, the code proceeds normally.
Note: While using continue in loop, we have to make sure that we put the continue after the loop variable updation or else it will result in an infinite loop.
3. Goto Statement in C
The goto statement is used to jump to a specific point from anywhere in a function. It is used to transfer the program control to a labeled statement within the same function.
Syntax of goto Statement
goto label;
.
.
label:
//code
Flowchart of goto Statement
Flow Diagram of goto in CExample of goto Statement
Check if a number is odd or even using goto statement.
C++
// C program to check if a number is
// even or not using goto statement
#include <stdio.h>
// function to check even or not
void checkEvenOrNot(int num)
{
if (num % 2 == 0)
// jump to even
goto even;
else
// jump to odd
goto odd;
even:
printf("%d is even", num);
// return if even
return;
odd:
printf("%d is odd", num);
}
int main()
{
int num = 26;
checkEvenOrNot(num);
return 0;
}
Note: The use of goto is generally discouraged in the programmer's community as it makes the code complex to understand.
4. Return Statement in C
The return statement in C is used to terminate the execution of a function and return a value to the caller. It is commonly used to provide a result back to the calling code.
return expression;
Example
C
#include <stdio.h>
int add(int a, int b)
{
int sum = a + b;
return sum; // Return the sum as the result of the
// function
}
void printMessage()
{
printf("GeeksforGeeks\n");
return; // Return from the function with no value (void)
}
int main()
{
int result = add(5, 3);
printf("Result: %d\n", result);
printMessage();
return 0;
}
OutputResult: 8
GeeksforGeeks
Similar Reads
goto Statement in C The goto statement in C allows the program to jump to some part of the code, giving you more control over its execution. While it can be useful in certain situations, like error handling or exiting complex loops, it's generally not recommended because it can make the code harder to read and maintain
4 min read
Continue Statement in C The continue statement in C is a jump statement used to skip the current iteration of a loop and continue with the next iteration. It is used inside loops (for, while, or do-while) along with the conditional statements to bypass the remaining statements in the current iteration and move on to the ne
4 min read
Dino Game in C The Dino game is a simple hurdle-based game. In this game a dinosaur has to jump hurdles, if the dinosaur successfully jumps a hurdle, the score point is incremented by one. And if the dinosaur hits the hurdle then the Game is Over. Approach for GameThe dinosaur is represented by ASCII art and moves
4 min read
C setjump and longjump âSetjumpâ and âLongjumpâ are defined in setjmp.h, a header file in C standard library. setjump(jmp_buf buf) : uses buf to remember the current position and returns 0.longjump(jmp_buf buf, i) : Go back to the place buf is pointing to and return i. C // C program to demonstrate // working of setjmp()
1 min read
Number System Conversion in C Number system conversion is a fundamental concept in computer science and programming. It involves changing the representation of a number from one base to another, such as converting a decimal number to binary or a hexadecimal number to binary.In this article, we will create a console program in th
8 min read