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
Switch Statement in C
C switch statement is a conditional statement that allows you to execute different code blocks based on the value of a variable or an expression. It is often used in place of if-else ladder when there are multiple conditions.Example:C#include <stdio.h> int main() { // Switch variable int var =
5 min read
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
Break Statement in C
The break statement in C is a loop control statement that breaks out of the loop when encountered. It can be used inside loops or switch statements to bring the control out of the block. The break statement can only break out of a single loop at a time.Example:C#include <stdio.h> int main() {
5 min read
C if else Statement
The if else in C is an extension of the if statement which not only allows the program to execute one block of code if a condition is true, but also a different block if the condition is false. This enables making decisions with two possible outcomes.Example:C#include <stdio.h> int main() { in
3 min read
Tokens in C
In C programming, tokens are the smallest units in a program that have meaningful representations. Tokens are the building blocks of a C program, and they are recognized by the C compiler to form valid expressions and statements. Tokens can be classified into various categories, each with specific r
4 min read
Strings in C
A String in C programming is a sequence of characters terminated with a null character '\0'. The C String is work as an array of characters. The difference between a character array and a C string is that the string in C is terminated with a unique character '\0'.DeclarationDeclaring a string in C i
5 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
scanf in C
In C, scanf() is a function is used to read data from stdin (standard input stream i.e. usually keyboard) and stores the result into the given arguments. It is defined in the <stdio.h> header file.Example:C#include <stdio.h> int main() { int n; // Reading an integer input scanf("%d",
3 min read
Queue in C
A queue is a linear data structure that follows the First In First Out (FIFO) order of insertion and deletion. It means that the element that is inserted first will be the first one to be removed and the element that is inserted last will be removed at last.In this article, we'll learn how to implem
7 min read