Open In App

Jump Statements in C

Last Updated : 14 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. break
  2. continue
  3. goto
  4. 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

flowchart of break in c

 

Uses of break in C

The break statement is used in C for the following purposes:

  1. To come out of the loop.
  2. To come out from the nested loops.
  3. 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;
}


Output

1 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

flowchart of continue in c

 

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;
}


Output

Executing 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

flowchart of goto in c

Flow Diagram of goto in C

Example 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;
}


Output

26 is even

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;
}


Output

Result: 8
GeeksforGeeks



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads