Control Flow
Definition: Control flow, also known as flow of control, refers to the order in which individual statements, instructions, or function calls are executed or evaluated in a programming language. It dictates the direction and sequence of execution of the code, determining how the program’s statements are executed in relation to conditions, loops, and other structures.
1. Conditional Statements
Conditional statements allow the execution of specific code blocks based on certain conditions.
if Statement
The if statement is the simplest form of decision-making statement. It executes a block of code only if a specified condition is true.
Syntax: if (condition) {
// code to be executed if condition is true
}
Example: if (a > b) {
printf(“a is greater than b”);
}
if-else Statement
The if-else statement provides an alternative path of execution when the if condition is false.
Syntax: if (condition) {
// code to be executed if condition is true
} else {
// code to be executed if condition is false
}
Example: if (a > b) {
printf(“a is greater than b”);
} else {
printf(“a is not greater than b”);
}
else-if Ladder
The else-if ladder allows multiple conditions to be checked in sequence. If one condition is true, its corresponding block of code is executed, and the rest are skipped.
Syntax:
Example:
switch Statement
The switch statement evaluates an expression and executes the code block corresponding to the matching case label. It is often used as an alternative to an else-if ladder when checking a variable against multiple constant values.
Syntax:
Example:
2. Loops
Loops allow repetitive execution of a block of code as long as a specified condition is true.
while Loop
The while loop executes a block of code as long as the specified condition is true. The condition is checked before entering the loop body.
Syntax: while (condition) {
// code to be executed
}
Example:
for Loop
The for loop is used when the number of iterations is known. It consists of three parts: initialization, condition, and increment/decrement.
Syntax: for (initialization; condition; increment/decrement) {
// code to be executed
}
Example:
do-while Loop
The do-while loop is similar to the while loop, but the condition is checked after executing the loop body, ensuring that the loop is executed at least once.
Syntax: do {
// code to be executed
} while (condition);
Example:
3. Control Transfer Statements
Control transfer statements allow altering the flow of execution in loops and conditional structures.
break Statement
The break statement exits the nearest enclosing loop or switch statement immediately.
Syntax:
Example:
continue Statement
The continue statement skips the remaining code in the current iteration and proceeds to the next iteration of the loop.
Syntax:
Example:
goto Statement and Labels
The goto statement transfers control to the labeled statement. It is generally discouraged as it can lead to unstructured and hard-to-read code.
Syntax:
Example:
Summary
- Conditional Statements (if, if-else, else-if, switch): Control the flow based on conditions.
- Loops (while, for, do-while): Execute code blocks repeatedly based on conditions.
- Control Transfer Statements (break, continue, goto): Alter the normal flow of loops and other control structures.