In C language, control flow statements determine the order in which the statements or blocks of code are executed in a program. These statements are crucial for making decisions, looping, and handling different scenarios. There are several types of control flow statements in C:
1. Conditional Statements
Conditional statements allow a program to choose between different paths based on a condition.
if
Statement: Executes a block of code if a condition is true.if (condition) { // Code to execute if condition is true }
if-else
Statement: Executes one block of code if the condition is true, and another block if the condition is false.if (condition) { // Code if condition is true } else { // Code if condition is false }
else if
Ladder: Used to test multiple conditions.if (condition1) { // Code if condition1 is true } else if (condition2) { // Code if condition2 is true } else { // Code if none of the conditions are true }
switch
Statement: Tests the value of a variable or expression against multiple cases, executing the corresponding code block.switch (expression) { case constant1: // Code for case constant1 break; case constant2: // Code for case constant2 break; default: // Code if no case matches }
2. Looping Statements
Looping statements repeatedly execute a block of code as long as a condition is true.
for
Loop: Used when the number of iterations is known.for (initialization; condition; update) { // Code to execute in each iteration }
Example:
for (int i = 0; i < 5; i++) { printf("%d\n", i); // Prints numbers from 0 to 4 }
while
Loop: Executes a block of code as long as the condition is true. Useful when the number of iterations is not known beforehand.while (condition) { // Code to execute while condition is true }
Example:
int i = 0; while (i < 5) { printf("%d\n", i); i++; }
do-while
Loop: Similar to thewhile
loop, except that it ensures the code block is executed at least once, regardless of the condition.do { // Code to execute } while (condition);
Example:
int i = 0; do { printf("%d\n", i); i++; } while (i < 5);
3. Jump Statements
Jump statements are used to alter the normal flow of control.
break
Statement: Terminates the loop orswitch
statement immediately and passes control to the next statement following the loop orswitch
.for (int i = 0; i < 10; i++) { if (i == 5) { break; // Stops the loop when i is 5 } printf("%d\n", i); }
continue
Statement: Skips the current iteration of the loop and proceeds with the next iteration.for (int i = 0; i < 10; i++) { if (i % 2 == 0) { continue; // Skips even numbers } printf("%d\n", i); // Prints only odd numbers }
goto
Statement: Transfers control to a labeled statement within the same function. Althoughgoto
can be useful in certain scenarios, it is generally considered bad practice as it can make code difficult to read and maintain.int i = 0; start: if (i < 5) { printf("%d\n", i); i++; goto start; // Jumps back to the label 'start' }
return
Statement: Exits the current function and optionally returns a value to the calling function. It is commonly used in functions to send the result back.int sum(int a, int b) { return a + b; // Returns the sum of a and b }
Examples
Below is a simple program that uses different control flow statements to illustrate their usage:
#include <stdio.h>
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
// Using if-else
if (number > 0) {
printf("The number is positive.\n");
} else if (number < 0) {
printf("The number is negative.\n");
} else {
printf("The number is zero.\n");
}
// Using for loop
printf("Numbers from 1 to 5: ");
for (int i = 1; i <= 5; i++) {
printf("%d ", i);
}
printf("\n");
// Using while loop
int count = 0;
while (count < 3) {
printf("Count: %d\n", count);
count++;
}
// Using switch-case
switch (number) {
case 0:
printf("You entered zero.\n");
break;
case 1:
printf("You entered one.\n");
break;
default:
printf("You entered a different number.\n");
}
return 0;
}
Summary
- Conditional Statements (
if
,if-else
,else if
,switch
) control decision-making in a program. - Looping Statements (
for
,while
,do-while
) are used to execute a block of code repeatedly. - Jump Statements (
break
,continue
,goto
,return
) alter the normal flow of control.
Control flow statements allow you to create dynamic, flexible programs that can handle complex logic and respond appropriately to user input or changing conditions. They are fundamental to programming in C and are used in almost every program to control the execution path.