Goto Statement in C Language: Syntax, Examples & Rules (Unit 2)



BCA Sem 1  |  Unit 2  |  Computer Concepts & C Programming

Goto Statement in C Language — BKNMU Junagadh

Unconditional Jump Control, Forward vs. Backward Jumps, Label Syntax, and Why to Avoid Spagetti Code

BKNMU Junagadh NEP 2020 Unit 2 Goto Statement Jump Control Label Syntax
In simple words: Welcome to Unit 2! The goto statement in C is an unconditional jump statement that instantly transfers control to a user-defined label located within the same function scope. It allows altering normal sequential execution by jumping directly forward or backward in code.
📖 Core Features of Goto Statement

The goto statement functions based on four major mechanics and structural elements:

label_name:

1. Label Declaration

A valid C identifier followed by a colon (:) that specifies the destination target where control will jump.

Backward Jump

2. Backward Transfer

When the target label appears BEFORE the goto line, causing execution to loop back upwards in code.

Forward Jump

3. Forward Transfer

When the target label appears AFTER the goto line, allowing execution to skip intermediate lines down below.

Function Scope

4. Local Scope Limit

A goto statement can only jump to a label declared inside the exact same function block (cannot jump across functions).

1️⃣ Master Table: Backward Jump vs. Forward Jump

Here is a structural comparison showing the direction and usage differences of goto jumps:

Jump DirectionPosition of LabelFunctional Purpose & Behavior
Backward JumpLabel defined ABOVE the goto statement.Acts like a custom loop by repeatedly sending control back up until a condition halts execution.
Forward JumpLabel defined BELOW the goto statement.Skips intermediate lines of code or error blocks to jump directly to cleanup routines down below.
ℹ️ Label Syntax Rule: A label name must follow standard variable naming rules in C and MUST end with a colon (:). Example: start: or error_exit:.
💻 Code Examples & Practical Applications

1. Backward Jump Example (Simulating a Loop from 1 to 5):

#include <stdio.h>

int main() {
    int i = 1;

start: // Label declaration
    printf("Value of i: %d\n", i);
    i++;

    if (i <= 5) {
        goto start; // Backward jump to 'start'
    }

    return 0;
}

2. Forward Jump Example (Bypassing Lines on Error):

#include <stdio.h>

int main() {
    int age = 15;

    if (age < 18) {
        goto invalid; // Forward jump to 'invalid' label below
    }

    printf("Welcome! You are eligible to proceed.\n");
    return 0;

invalid:
    printf("Access Denied: You must be at least 18 years old.\n");
    return 0;
}
🔄 Step-by-Step Execution Sequence
1

Step 1: Declare Label Keyword

A label is placed at the target destination in code followed by a colon (e.g., repeat:).

2

Step 2: Trigger Goto Keyword

Execution encounters goto label_name; (usually nested inside an if condition check).

3

Step 3: Immediate Target Jump

Control bypasses all sequential statements in between and instantly transfers to the specified target label.

💡 When is Goto Acceptable? While generally discouraged, goto is legally used in professional C programming to break out of multiple deeply nested loops at once in a single jump, which standard break cannot do!
⚠️ Spaghetti Code Danger: Overusing goto statements leads to unreadable, unmaintainable, and hard-to-debug code known as "Spaghetti Code". Software engineering principles strongly recommend using structured loops (for, while) instead.
📝 Quick Revision — Key Exam Points
  • Statement Category: Unconditional Jump Control Statement.
  • Syntax Format: goto label_name; ... label_name:
  • Jump Directions: Forward (skip code down) and Backward (repeat code up).
  • Scope Restriction: Local to the function in which it is defined (cannot jump across functions).
  • Major Drawback: Destroys program structure and creates Spaghetti Code.

Post a Comment

Thanks for comment.

Previous Post Next Post