Jump Statements in C: break in C, continue in C and goto in C

Hello there, fellow coder! Today, we’re going to dive into the world of jump statements in C. You might be wondering, “What are jump statements?” or “Why do I need to know about them?” Well, sit tight, because we’re about to embark on a journey that will answer all your questions and more.

Jump statements in C, including break, continue, and goto, are like the secret sauce of programming. They help control the flow of your program, making it more efficient and easier to understand. So, let’s get started!

Break Statement

The break statement is like the red stop sign on the road of your program. When the program encounters a break, it halts the execution of the current loop and jumps out, continuing with the next line of code after the loop.

Example

Let’s look at an example to understand how the break statement works:

#include <stdio.h>

int main() {
    for(int i=1; i<=10; i++) {
        if(i == 5) {
            break;
        }
        printf("%d \n", i);
    }
    return 0;
}
C

In this code, the break statement is inside a for loop. The loop starts with i=1 and is supposed to continue until i=10. However, we have a condition inside the loop that checks if i is equal to 5. If it is, the break statement is executed, and the loop is terminated. So, the output of this program will be:

1
2
3
4

Continue Statement

The continue statement is like a hurdle in the race of your program. When the program encounters a continue, it skips the rest of the current loop iteration and jumps to the next iteration.

Example

Here’s an example to illustrate the continue statement:

#include <stdio.h>

int main() {
    for(int i=1; i<=10; i++) {
        if(i == 5) {
            continue;
        }
        printf("%d \n", i);
    }
    return 0;
}
C

In this code, the continue statement is inside a for loop. The loop starts with i=1 and continues until i=10. However, we have a condition inside the loop that checks if i is equal to 5. If it is, the continue statement is executed, and the rest of the loop iteration is skipped. So, the output of this program will be:

1
2
3
4
6
7
8
9
10

Notice that 5 is missing from the output. That’s the continue statement in action!

Goto Statement

The goto statement is like a teleportation device in your program. It allows you to jump from one part of your program to another by transferring control to a labeled statement.

Example

Let’s look at an example to understand how the goto statement works:

#include <stdio.h>

int main() {
    int i = 1;
    CODESCRACKER:
    i++;
    if(i <= 10) {
        printf("i = %d. Going to CODESCRACKER  \n", i);
        goto CODESCRACKER;
    }
    return 0;
}
C

In this code, we have a label named CODESCRACKER. When the goto statement is encountered, the control jumps to the CODESCRACKER label. This process continues until the condition i <= 10 is false## Wrapping Up

So there you have it! We’ve covered the break, continue, and goto jump statements in C. These statements are powerful tools that can help you control the flow of your program. However, remember that with great power comes great responsibility. Use these statements wisely to avoid creating confusing code that’s hard to debug and maintain.

Frequently Asked Questions (FAQ)

  1. How to jump from one line to another in C?

    You can use the goto statement to jump from one line to another in C.

  2. What are jump statements? Can you explain them with an example?

    Jump statements in C, including break, continue, and goto, control the flow of the program. We’ve provided examples of each in the tutorial above.

  3. How to skip a statement in C?

    You can use the continue statement to skip the current iteration of a loop in C.

  4. Is the goto statement known as the jump statement in C?

    Yes, the goto statement is one type of jump statement in C.

Scroll to Top