C Programming - Beginner to Brilliance / Control Flow Statements

Jump Statements

Jump statements are used when you want to interrupt or control the normal sequential flow of your program.

They help with:

  • 🔄 Exiting loops early (break)

  • ⏭ Skipping to the next iteration (continue)

  • 🚀 Exiting functions and returning values (return)

  • ⚠ Jumping unconditionally to another code block (goto) — use with caution


🔴 break Statement

🧠 Purpose:

Used to break out of:

  • Loops (for, while, do-while)

  • switch cases

Once break is encountered, the control exits the loop/switch immediately and continues after it.


📌 Syntax:

break

✅ Example 1: break in for loop

#include <stdio.h>

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

🔎 Explanation:

The loop breaks as soon as i == 5. Remaining values (5–10) are skipped.


✅ Example 2: break in switch statement

int day = 3;

switch(day) {
    case 1: printf("Monday"); break;
    case 2: printf("Tuesday"); break;
    case 3: printf("Wednesday"); break;
    default: printf("Invalid");
Wednesday

🔎 Explanation:

If you forget break, C will execute all remaining cases — this is called fallthrough.


🔵 continue Statement

🧠 Purpose:

Used to skip the current iteration and jump to the next iteration of the loop.

  • Only used inside loops (for, while, do-while)

  • It does not exit the loop — only skips one cycle


📌 Syntax:

continue

✅ Example: Skipping multiples of 3

#include <stdio.h>

int main() {
    for (int i = 1; i <= 10; i++) {
        if (i % 3 == 0)
            continue;
        printf("%d ", i);
    }
    return 0;
}
1 2 4 5 7 8 10

🔎 Explanation:

Whenever i is a multiple of 3, continue skips the printf() for that iteration.


🌀 goto Statement (⚠ Not recommended)

🧠 Purpose:

goto is used to jump unconditionally to another point in the code marked by a label.

Using goto makes code harder to read, trace, and debug. Avoid it unless absolutely needed (e.g., in low-level programming or for breaking out of nested loops).

📌 Syntax:

goto label_name; // somewhere else in code
label_name: // code block

⚠️ Example:

#include <stdio.h>

int main() {
    int num = 5;

    if (num == 5)
        goto skip;

    printf("This will be skipped");

    skip:
    printf("You jumped to here!");
    return 0;
}
You jumped to here!

🔎 Explanation: goto bypasses the printf("This will be skipped"); line.


🔚 return Statement

🧠 Purpose:

  • Ends the execution of a function

  • Can return a value to the caller (or nothing if the function returns void)

📌 Syntax:

return;       // for void functions
return value; // for value-returning functions

✅ Example 1: Returning from main()

#include <stdio.h>

int main() {
    printf("Hello\\n");
    return 0;
}

🔎 return 0; indicates the program finished successfully.

✅ Example 2: Return from a function

int add(int a, int b) {
    return a + b;
}

int main() {
    int result = add(5, 3);
    printf("%d", result); // Output: 8
}

🔁 Bonus: break vs continue vs return vs goto

Statement

Purpose

Works In

Effect

break

Exit loop/switch

Loops, switch

Stops the loop

continue

Skip current iteration

Loops

Skips to next cycle

return

Exit a function

Functions

Ends function, returns control/value

goto

Jump to label

Anywhere

Jumps to labeled line (bad practice)