Jump Statements in C (break, continue, goto)

Jump statements are used to change the normal flow of program execution. They allow immediate transfer of control to another part of the program.
Available Jump Statements:
1. break
2. continue
3. goto

1. break Statement

The break statement immediately exits from a loop or a switch block.

break;

Example – Stop loop when number found

#include <stdio.h>

int main() {

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

    return 0;
}

Example – Stop switch case

#include <stdio.h>

int main() {

    int ch = 2;

    switch(ch){
        case 1: printf("One"); break;
        case 2: printf("Two"); break;
        case 3: printf("Three"); break;
    }

    return 0;
}

2. continue Statement

The continue statement skips the current iteration of a loop and jumps to the next iteration.

continue;

Example – Skip even numbers

#include <stdio.h>

int main() {

    for(int i = 1; i <= 10; i++){
        if(i % 2 == 0)
            continue;
        printf("%d ", i);
    }

    return 0;
}

Example – Skip negative number input

#include <stdio.h>

int main() {

    int n;

    for(int i = 1; i <= 5; i++){
        scanf("%d", &n);

        if(n < 0){
            printf("Skipping negative value\n");
            continue;
        }

        printf("You entered: %d\n", n);
    }

    return 0;
}

3. goto Statement

The goto statement transfers control to a labeled statement. Use only when necessary (not recommended usually).

goto label;
...
label:

Example – Simple goto usage

#include <stdio.h>

int main() {

    printf("Start\n");

    goto jump;

    printf("This line is skipped\n");

    jump:
    printf("Jumped here using goto");

    return 0;
}

Example – Retry input using goto

#include <stdio.h>

int main() {

    int n;

    retry:
    printf("Enter a positive number: ");
    scanf("%d", &n);

    if(n < 0){
        printf("Invalid! Try again.\n");
        goto retry;
    }

    printf("You entered: %d", n);

    return 0;
}

Example 1 – Stop loop when value reaches 7

#include <stdio.h>

int main() {

    for(int i = 1; i <= 20; i++){
        if(i == 7)
            break;
        printf("%d ", i);
    }

    return 0;
}

Example 2 – Exit inner loop only

#include <stdio.h>

int main() {

    for(int i = 1; i <= 3; i++){
        for(int j = 1; j <= 5; j++){
            if(j == 3)
                break;
            printf("%d ", j);
        }
        printf("\n");
    }

    return 0;
}

Example 3 – Skip numbers divisible by 4

#include <stdio.h>

int main() {

    for(int i = 1; i <= 20; i++){
        if(i % 4 == 0)
            continue;
        printf("%d ", i);
    }

    return 0;
}

Example 4 – Skip negative inputs only

#include <stdio.h>

int main() {

    int n, count = 0;

    while(count < 5){
        scanf("%d", &n);

        if(n < 0){
            printf("Skipped negative\n");
            continue;
        }

        printf("Accepted: %d\n", n);
        count++;
    }

    return 0;
}

Example 5 – Stop at first even number

#include <stdio.h>

int main() {

    int n;

    while(1){
        scanf("%d", &n);
        if(n % 2 == 0)
            break;
        printf("Odd: %d\n", n);
    }

    printf("Stopped at even number!");

    return 0;
}

Example 6 – Simple goto jump

#include <stdio.h>

int main() {

    printf("Start\n");

    goto skip;

    printf("This line is skipped\n");

    skip:
    printf("Jumped using goto");

    return 0;
}

Example 7 – Retry PIN until correct

#include <stdio.h>

int main() {

    int pin;

    retry:
    printf("Enter PIN: ");
    scanf("%d", &pin);

    if(pin != 1234){
        printf("Incorrect! Try again.\n");
        goto retry;
    }

    printf("Login successful!");

    return 0;
}

Example 8 – Simple calculator using switch

#include <stdio.h>

int main() {

    int a = 12, b = 4;
    char op = '/';

    switch(op){
        case '+': printf("%d", a + b); break;
        case '-': printf("%d", a - b); break;
        case '*': printf("%d", a * b); break;
        case '/': printf("%d", a / b); break;
        default: printf("Invalid Operator");
    }

    return 0;
}

Example 9 – Skip printing 3 in inner loop

#include <stdio.h>

int main() {

    for(int i = 1; i <= 3; i++){
        for(int j = 1; j <= 5; j++){
            if(j == 3)
                continue;
            printf("%d ", j);
        }
        printf("\n");
    }

    return 0;
}

Example 10 – Emergency exit using goto

#include <stdio.h>

int main() {

    int x = 1;

    while(x <= 10){
        if(x == 5)
            goto emergency;
        printf("%d ", x);
        x++;
    }

    emergency:
    printf("\nEmergency Exit Reached!");

    return 0;
}

Difference Between break, continue, goto

break:
- Exits loop/switch completely

continue:
- Skips current iteration, continues loop

goto:
- Jumps to a specific label
- Should be used carefully

Practice Questions

  1. What is the use of break? Give two examples.
  2. Write a program to print numbers from 1 to 50 but skip multiples of 5.
  3. Create a login retry system using goto.
  4. Explain how continue works inside nested loops.
  5. Why should goto be used carefully?

Practice Task

Write a program to process 10 numbers: • Skip negative numbers • Stop completely when number 0 is entered • Display total of valid numbers Use break and continue inside the same loop.