๐ 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
, andgoto
๐ Topics Covered
๐ข Topic | ๐ Description |
---|---|
๐ง C ifโฆelse Statements | Perform actions based on conditions |
๐๏ธ C switch Statements | Multi-branch conditional logic |
๐ C Loops Overview | Looping with for , while , and do-while |
โพ๏ธ C Infinite & Nested Loops | Continuous and embedded iterations |
โ C break & continue Statements | Control the flow inside loops |
๐งญ C goto Statement | Jump 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
, anddo-while
allow structured repetitionbreak
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 :