C Tutorial
Estimated reading: 4 minutes 354 views

C Control Flow – Master if…else, Loops, switch, break, continue


Introduction – Managing Program Execution Paths

In C programming, control flow structures like conditions and loops give you the power to decide what happens next. Whether you want to execute code based on a condition, repeat tasks, or jump between statements, control flow is the foundation of logical programming.

In this guide, you’ll learn:

  • How to use conditional statements (if, else, switch)
  • The working of loops (for, while, do-while)
  • Infinite and nested loop usage
  • Loop control with break, continue, and goto

Topics Covered

Topic Description
C if…else StatementsPerform actions based on conditions
🎚️ C switch StatementsMulti-branch conditional logic
C Loops OverviewLooping with for, while, and do-while
♾️ C Infinite & Nested LoopsContinuous and embedded iterations
C break & continue StatementsControl the flow inside loops
C goto StatementJump to specific code blocks

C if…else Statements

Use if…else to run different blocks of code based on conditions.

Syntax:

if (condition) {
    // executes if condition is true
} else {
    // executes if condition is false
}

Example:

int age = 18;
if (age >= 18) {
    printf("Adult");
} else {
    printf("Minor");
}

You can chain multiple conditions using else if.


🎚️ C switch Statements (including nested)

switch is ideal for selecting among multiple options based on a single variable.

Syntax:

switch (expression) {
    case value1:
        // code
        break;
    case value2:
        // code
        break;
    default:
        // fallback code
}

Example:

int day = 2;
switch (day) {
    case 1: printf("Monday"); break;
    case 2: printf("Tuesday"); break;
    default: printf("Invalid day");
}

Nested switch statements allow deeper conditional logic:

switch (dept) {
    case 1:
        switch (year) {
            case 1: printf("First year"); break;
            case 2: printf("Second year"); break;
        }
        break;
}

C Loops Overview (for, while, do-while)

Loops allow repeated execution of code blocks.

for loop:

Used when the number of iterations is known.

for (int i = 0; i < 5; i++) {
    printf("%d ", i);
}

while loop:

Condition is checked before entering the loop.

int i = 0;
while (i < 5) {
    printf("%d ", i);
    i++;
}

do-while loop:

Executes at least once, even if the condition is false.

int i = 0;
do {
    printf("%d ", i);
    i++;
} while (i < 5);

♾️ C Infinite & Nested Loops

♾️ Infinite Loop:

Runs forever unless externally broken (used in systems programming).

while (1) {
    // endless execution
}

Nested Loop:

A loop inside another loop, commonly used for matrix or grid operations.

for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 3; j++) {
        printf("%d %d\n", i, j);
    }
}

Be cautious with nested loops—they can increase time complexity.


C break & continue Statements

break:

Immediately exits the nearest loop or switch.

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

continue:

Skips the current iteration and proceeds with the next.

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

C goto Statement

goto provides an unconditional jump to a labeled statement.

Syntax:

goto label;
...
label:
    // code to execute

Example:

int x = 5;
if (x < 10) goto skip;

printf("This won't print");

skip:
printf("Jumped to here");

🚨 Note: Use goto only when absolutely necessary. Prefer structured alternatives for clarity.


Summary – Recap & Next Steps

Control flow is essential for writing logic-based C programs. Whether it’s choosing between blocks (if, switch) or repeating tasks (for, while), these structures help define how and when things happen in your code.

Key Takeaways:

  • if…else is great for 2–3 decision branches
  • Use switch for clean multi-option comparisons
  • for, while, and do-while allow structured repetition
  • break exits a loop; continue skips one iteration
  • Use goto with caution and only for specific low-level control
  • Nested and infinite loops serve advanced programming needs

Real-World Relevance:
Control flow statements are used in authentication systems, menu-driven programs, data processing loops, robotics, and real-time applications.


Frequently Asked Questions (FAQ)


When should I use a switch instead of if…else?
Use switch when checking one variable against multiple constant values. It’s cleaner and more readable than multiple if...else if.


Can I have an infinite loop in C?
Yes. Use while(1) or for(;;) for infinite loops. These are common in embedded systems and servers.


Is goto a good practice in C?
It’s generally discouraged due to poor readability, but can be useful for exiting deeply nested loops in rare cases.


What’s the difference between break and continue?
break exits a loop or switch; continue skips the current loop iteration.


Can if statements be nested?
Yes. You can nest multiple if statements to evaluate complex conditions.


Which loop guarantees at least one execution?
The do-while loop executes the body at least once before checking the condition.


Share Now :
Share

🔁 C Control Flow – Conditions & Loops

Or Copy Link

CONTENTS
Scroll to Top