×

Advanced Coding & Software Engineering Program

Duration: 1 Year (12 Months)

Join our premium 1-year program to master cutting-edge technologies and become an industry-ready Software Engineer!

Course Coverage

  • Languages: C, C++, Java, JavaScript, Python
  • Web Technologies: HTML, CSS, Bootstrap 5, MERN Stack, Full Stack Development
  • Databases: MySQL, MongoDB
  • Data Science Libraries: Pandas, NumPy
  • Development Tools: Visual Studio Code, IntelliJ IDEA, PyCharm, Postman, Git, GitHub
  • Cloud Platforms: Vercel, MongoDB Atlas

Program Highlights

  • Live Classes: Interactive sessions with real-time doubt resolution
  • Hands-On Sessions: Practical coding exercises to build real-world skills
  • Industry Experts: Learn from professionals with years of experience
  • Live Project: Work on real-world projects to apply your skills
  • Get Certificate: Earn a professional certificate upon program completion

Course Fee: Only ₹1020 / month
Limited Period Offer!

Jump Statements in C: From Basics to Advanced



Last Updated on: 15th Jul 2025 16:05:13 PM

Jump statements in C are powerful tools that alter the flow of a program, allowing you to skip, exit, or redirect execution. Think of them as shortcuts or emergency exits in your code’s roadmap. In this tutorial, we’ll explore the break, continue, goto, and return statements, with examples ranging from beginner-friendly to advanced. Each example includes code, output, and a detailed explanation to make learning engaging and clear. Let’s jump into controlling program flow like a pro!

 

What Are Jump Statements?

Jump statements in C allow you to redirect program control to specific points in the code. They are used within loops, switch cases, or functions to:

  • Exit a loop or switch (break).

  • Skip part of a loop iteration (continue).

  • Jump to a labeled statement (goto).

  • Exit a function and return a value (return).

These statements give you precise control over your program’s behavior, making it more flexible and efficient.

 

1. The break Statement

The break statement immediately exits a loop or switch case, stopping further execution within that block.

 

Syntax:

break;

 

Basic Example: Exit a Loop Early

This example stops a loop when a specific condition is met.

#include <stdio.h>
int main() {
    for (int i = 1; i <= 10; i++) {
        if (i == 6) {
            break;
        }
        printf("%d ", i);
    }
    printf("\nLoop exited.\n");
    return 0;
}

 

Output:

1 2 3 4 5 
Loop exited.

 

Explanation:

  • The for loop iterates from 1 to 10.

  • When i equals 6, the break statement terminates the loop immediately.

  • Only numbers 1 to 5 are printed, followed by the exit message.

 

Advanced Example: Search in a 2D Array

This program searches for a value in a 2D array and exits nested loops when found.

#include <stdio.h>
int main() {
    int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    int target = 5;
    int found = 0;
    
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            if (matrix[i][j] == target) {
                printf("Found %d at position (%d, %d)\n", target, i, j);
                found = 1;
                break; // Exit inner loop
            }
        }
        if (found) {
            break; // Exit outer loop
        }
    }
    if (!found) {
        printf("%d not found in the matrix.\n", target);
    }
    return 0;
}

 

Output:

Found 5 at position (1, 1)

 

Explanation:

  • The program searches for target (5) in a 3x3 matrix using nested for loops.

  • When the target is found, the inner break exits the column loop, and the outer break exits the row loop, avoiding unnecessary iterations.

  • The found flag tracks whether the target was located, and a message is printed accordingly.

 

2. The continue Statement

The continue statement skips the rest of the current loop iteration and moves to the next one.

 

Syntax:

continue;

 

Basic Example: Skip Odd Numbers

This example prints only even numbers by skipping odd ones.

#include <stdio.h>
int main() {
    for (int i = 1; i <= 10; i++) {
        if (i % 2 != 0) {
            continue;
        }
        printf("%d ", i);
    }
    printf("\n");
    return 0;
}

 

Output:

2 4 6 8 10 

 

Explanation:

  • The for loop iterates from 1 to 10.

  • If i is odd (i % 2 != 0), continue skips the printf statement, moving to the next iteration.

  • Only even numbers are printed.

 

Advanced Example: Process Valid Transactions

This program processes a list of transactions, skipping invalid ones (negative or zero amounts).

#include <stdio.h>
int main() {
    float transactions[] = {100.50, -20.00, 75.25, 0.00, 50.75, 200.00};
    int size = 6;
    float total = 0.0;
    
    for (int i = 0; i < size; i++) {
        if (transactions[i] <= 0) {
            printf("Skipping invalid transaction: %.2f\n", transactions[i]);
            continue;
        }
        total += transactions[i];
        printf("Processed transaction %d: %.2f, Running total: %.2f\n", i + 1, transactions[i], total);
    }
    printf("Final total: %.2f\n", total);
    return 0;
}

 

Output:

Processed transaction 1: 100.50, Running total: 100.50
Skipping invalid transaction: -20.00
Processed transaction 3: 75.25, Running total: 175.75
Skipping invalid transaction: 0.00
Processed transaction 5: 50.75, Running total: 226.50
Processed transaction 6: 200.00, Running total: 426.50
Final total: 426.50

 

Explanation:

  • The program iterates through an array of transactions.

  • If a transaction is invalid (<= 0), continue skips it, printing a message.

  • Valid transactions are added to total, with details printed for each.

  • The %.2f format ensures two decimal places for currency.

 

3. The goto Statement

The goto statement jumps to a labeled statement in the same function. While powerful, it’s used sparingly due to potential code complexity.

Syntax:

goto label;
// ...
label:
    // Code

 

Basic Example: Error Handling

This example uses goto to handle an error condition.

#include <stdio.h>
int main() {
    int num;
    printf("Enter a positive number: ");
    scanf("%d", &num);
    
    if (num <= 0) {
        goto error;
    }
    printf("You entered: %d\n", num);
    return 0;
    
error:
    printf("Error: Please enter a positive number.\n");
    return 1;
}

 

Output (if user enters -5):

Enter a positive number: -5
Error: Please enter a positive number.

 

Explanation:

  • If num <= 0, goto error jumps to the error label, printing an error message.

  • Otherwise, the program prints the valid input.

  • The return 1 indicates an error exit status.

 

Advanced Example: Retry Mechanism

This program allows multiple attempts to enter a valid PIN, using goto for flow control.

#include <stdio.h>
int main() {
    int pin, attempts = 0;
    const int max_attempts = 3;
    const int correct_pin = 1234;
    
retry:
    if (attempts >= max_attempts) {
        goto locked;
    }
    printf("Enter your PIN (attempt %d/%d): ", attempts + 1, max_attempts);
    scanf("%d", &pin);
    attempts++;
    
    if (pin != correct_pin) {
        printf("Incorrect PIN. Try again.\n");
        goto retry;
    }
    printf("Access granted! PIN is correct.\n");
    return 0;
    
locked:
    printf("Account locked. Too many incorrect attempts.\n");
    return 1;
}

 

Output (if user enters 1111, 2222, 1234):

Enter your PIN (attempt 1/3): 1111
Incorrect PIN. Try again.
Enter your PIN (attempt 2/3): 2222
Incorrect PIN. Try again.
Enter your PIN (attempt 3/3): 1234
Access granted! PIN is correct.

 

Output (if user enters 1111, 2222, 3333):

Enter your PIN (attempt 1/3): 1111
Incorrect PIN. Try again.
Enter your PIN (attempt 2/3): 2222
Incorrect PIN. Try again.
Enter your PIN (attempt 3/3): 3333
Account locked. Too many incorrect attempts.

 

Explanation:

  • The program uses goto retry to loop back for another PIN attempt if incorrect.

  • If attempts reaches max_attempts, goto locked jumps to the failure message.

  • The goto statement simplifies the retry logic, though loops could also be used (modern practice often avoids goto).

 

4. The return Statement

The return statement exits a function, optionally returning a value to the caller.

Syntax:

return expression; // For functions with a return type
return; // For void functions

 

Basic Example: Function with Return

This example calculates the square of a number and returns it.

#include <stdio.h>
int square(int num) {
    return num * num;
}
int main() {
    int num = 4;
    int result = square(num);
    printf("Square of %d is %d\n", num, result);
    return 0;
}

 

Output:

Square of 4 is 16

 

Explanation:

  • The square function returns num * num.

  • The main function calls square(4), stores the result (16), and prints it.

  • The return 0 in main indicates successful program execution.

 

Advanced Example: Recursive Factorial with Early Return

This program calculates the factorial of a number, using return to handle base cases and recursion.

#include <stdio.h>
unsigned long long factorial(int n) {
    if (n < 0) {
        printf("Error: Negative input is invalid.\n");
        return 0; // Early return for invalid input
    }
    if (n == 0 || n == 1) {
        return 1; // Base case
    }
    return n * factorial(n - 1); // Recursive case
}
int main() {
    int num;
    printf("Enter a number to calculate factorial: ");
    scanf("%d", &num);
    
    unsigned long long result = factorial(num);
    if (result != 0) {
        printf("Factorial of %d is %llu\n", num, result);
    }
    return 0;
}

 

Output (if user enters 5):

Enter a number to calculate factorial: 5
Factorial of 5 is 120

 

Output (if user enters -3):

Enter a number to calculate factorial: -3
Error: Negative input is invalid.

 

Explanation:

  • The factorial function uses recursion, with return 1 for base cases (n == 0 or n == 1).

  • For negative inputs, it returns 0 after printing an error, allowing the caller to handle invalid cases.

  • The unsigned long long type handles large factorials. The recursive case returns n * factorial(n - 1).

  • The main function checks the result to avoid printing invalid factorials.

 

Why Jump Statements Are Powerful

Jump statements enhance your program’s flexibility by:

  • Optimizing Flow: break and continue improve efficiency by avoiding unnecessary iterations.

  • Handling Errors: goto and return provide ways to manage errors or redirect execution.

  • Enabling Complex Logic: Advanced examples like the retry mechanism or recursive factorial show how jump statements solve real-world problems.

 

Caution: Use goto sparingly, as it can make code harder to read. Prefer loops or functions for structured control flow when possible.

Pro Tip: Experiment with these examples! Try modifying the transaction processor to log skipped transactions to a file, or enhance the factorial program to handle larger numbers with overflow checks. Jump statements are your toolkit for creative control flow!

Happy coding, smiley

 


Online - Chat Now
Let’s Connect

Inquiry Sent!

Your message has been successfully sent. We'll get back to you soon!

iKeySkills Logo