C Tutorial
Estimated reading: 4 minutes 7 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿ” C Control Flow โ€“ Conditions & Loops

Or Copy Link

CONTENTS
Scroll to Top