Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Jump Statements in C

Last Updated on May 10, 2023 by Prepbytes

Jump statements in C programming language are used to change the flow of the code. Loops, If else, switch statements, and functions all are the type of jump statement in C programming. One by one, we will cover everything in this blog, with their syntax along with their flow charts, and the implementation of jump statement in c. Lastly, in this blog, we will also discuss the advantages and disadvantages of using jumping statements in c.

What is a Jump Statement in C?

The control moves unconditionally to another area of the program when a Jump Statement in C is satisfied. Normally, it is employed to immediately end a loop or switch case. A portion of the program can also be prevented from running by using it.

Jump Statements are typically used to execute the preceding or following block of code, omit loop iterations, and direct control flow to a specific location, among other things.

Elaboration:
In the above flow chart of the jump statement, we can see that when our control reaches statement 1, it can move either to statement 2 or it can move to statement 3 because of the jump statement. Now, the factor which decides the direction of the flow of control is the condition of the jump statement, if the condition specified condition is true then control will flow to statement 3 and if it is false then control will flow to statement 2.

Types of Jump Statements in C

  • Break Statement
  • Continue Statement
  • Goto Statement
  • Return Statement

Break Statement

Break statements quickly end loops like for, while, and do-while, remove control from the loop, and start the next block’s execution. Additionally, it ends the switch statement.
If we use the break statement inside of a nested loop, the break statement will first break the inner loop. The break statement inside the inner loop has no impact on the outside loop, which will then carry on as usual.

Syntax of Break Statement in C

if(Is_condition_is_true) {
    Break;
}

Elaboration:
In the above diagram, we can see that the break statement is written inside the loop.

  • First, we check the entering condition of the loop.
  • If the entering condition for the loop is true, then the code inside the loop executes; otherwise, the loop ends.
  • If the code inside of the loop executes, then the condition for the break statement written inside the body is checked. If it is true, it immediately terminates the loop; otherwise, the loop keeps executing until the break condition becomes true or the entering condition becomes false.

Code Implementation:

#include <stdio.h>

int main(void) {
    // implementation of code starts from here
    printf("Printing numbers from 1 to 10\n");
    for(int i=1;i<=10;i++){
        if(i%4==0){
            printf("Encounter Break Statement\n");
            break;
        }
        printf("%d\n",i);
    }
    printf("came out of loop\n");
    return 0;
}

Output:

Printing numbers from 1 to 10
1
2
3
Encounter Break Statement
came out of loop

Continue Statement

The continue jump statement pauses or changes the control flow during program execution just like any other jump statement does. Loops are where continue is most frequently found.

Instead of completing the loop, it stops the statements that follow it in execution and moves on to the next iteration.

When a loop is nested, the continue statement, like a break statement, passes control to the inner loop where it is present rather than any of the outer loops on the following iteration.

Syntax of Continue Statement in C

// body of loop

if(condition){
continue;
}

// statements beneath

Elaboration:

  • The loop begins by determining whether or not the loop’s condition is true. If it is not true, the loop is terminated immediately.
  • If the loop condition is true, the condition for the continue statement is checked.
  • If the condition for continue is false, the body of the loop can be executed.
  • Otherwise, if the condition for continue is true, the current iteration of the loop is skipped and the next iteration begins.

Code Implementation:

#include <stdio.h>

int main(void) {
    // implementation of code starts from here
    
    printf("printing odd numbers from 1 to 10\n");
    for(int i=1;i<=10;i++){
        if(i%2==0){
            continue;
        }
        printf("%d\n",i);
    }
    return 0;
}

Output:

printing odd numbers from 1 to 10
1
3
5
7
9

Goto Statement

TThe continue statement can only be used in loops, whereas Goto can be used everywhere in the code. The goto statement is identical to the continue statement in the jump statement in C. The goto statement allows us to define where the control should go after skipping, whereas the continue statement skips the current iteration of the loop and carries on to the next iteration.

To instruct the program control on how to move forward, the label notion is used. The goto statement’s programmatic leap or jump occurs within the function.

Syntax of Goto Statement in C
The Syntax of the Goto statement in C can be written in two ways.

Syntax 1: downward jump

  goto label;
      .
      .
      .
      .
   label:

Syntax 2: upward jump

    label:
      .
      .
      .
      .
  goto label;

Code Implementation:

#include <stdio.h>

int main(void) {
    // implementation of code start from here
    
  printf("printing numbers from 1 to 5 using Goto statement\n");
    
  int n = 1; //initialize 

  label: //label code
    printf("%d\n", n++);


  if (n <= 5)
    goto label; //goto statement

  return 0;
}

Output:

printing numbers from 1 to 5 using Goto statement
1
2
3
4
5

Return Statement

Programmers use the return statement as a control flow statement to end a function and return a value to the caller. The return statement’s fundamental syntax is:

Syntax of Return Statement in C

Syntax 1:

return;

Syntax 2:

return expression;

Elaboration:
In this flowchart,

  • we first call the multiply function from the main function.
  • when that called function finds a return statement, the flow returns to the calling function.

Code Implementation:

#include <stdio.h>

int multiply(int a,int b){
    return a * b;
}

int main(void) {
    // implementation of code starts from here
    int a = 10,b = 20;
    printf("Handing control to method name multiply\n");
    int output = multiply(a,b);
    printf("control is handed back to caller method\n");
    return 0;
}

Output:

Handing control to method name multiply

control is handed back to caller method

Advantages of using Jumping Statement in C

  • The program’s flow can be changed to suit our needs.
  • Use jump statements to get around extra code.
  • The loop can be ended by using break statements.
  • Our programming is rather flexible thanks to jump statements.

Disadvantages of using Jumping Statement in C

  • It becomes increasingly difficult to debug.
  • Code modification becomes challenging.
  • Jumps from one area of the code to another make it more difficult to read.

Conclusion

  • The jump statement in C is used in loops such as for, while, and do-while.
  • The jump statement in C simply modifies the flow of program control.
  • Break, continue, goto, and return are the four types of jump statements in c.
  • The jump statement has advantages and disadvantages.

FAQs related to Jump Statement in C

Q1. Is exit a jump statement?
Ans. The C++ language defines the jump statements as break, continue, goto, and return. In addition to these jump statements, a program can be terminated entirely by using the standard library function exit().

Q2. Which loop never ends?
Ans. An infinite loop, also known as an endless loop, is a section of code that doesn’t have a working exit and keeps repeating forever. A loop is a set of instructions that are repeatedly carried out until a specific condition is met in computer programming.

Q3. Is goto a jump statement in C?
Ans. In C, the goto statement is referred to as the jump statement. Goto is used to move program control to a predefined label, as the name implies. For a specific condition, the goto statement can be used to repeat a section of code.

Q4. What is an unconditional jump statement in C?
Ans. The sequential execution of statements is interrupted by jump statements, which cause the execution to continue at a different location in the program. If the jump destination is outside of the scope of the automated variables, the leap destroys them. In C, the four statements break, continue, goto, and return all result in unconditional jumps.

Leave a Reply

Your email address will not be published. Required fields are marked *