Control flow statements

Control flow statements in C are used to control the flow of execution in a program. They allow you to make decisions, repeat blocks of code, and jump to different parts of the program based on certain conditions. There are mainly three types of control flow statements in C

Basic Concepts

In C, there are primarily three types of control flow statements:

  1. Conditional Statements (if, else if, else)
  2. Looping Statements (for, while, do-while)
  3. Jump Statements (break, continue, return)

1. Conditional Statements

if Statement

The if statement is used to execute a block of code if a specified condition is true.

				
					int num = 10;
if (num > 5)
{
    printf("Number is greater than 5\n");
}

				
			

else if Statement

The else if statement is used to specify multiple conditions to execute different blocks of code.

				
					if (condition1)
{
    // code to be executed if condition1 is true
}
else if (condition2)
{
    // code to be executed if condition2 is true
}

				
			

else Statement

The else statement is used to execute a block of code if the condition in the if statement is false.

				
					if (condition)
{
    // code to be executed if condition is true
}
else
{
    // code to be executed if condition is false
}

				
			

Switch Statements

Switch statements are used when you have a single expression that you want to evaluate against multiple possible values. It provides an alternative to a series of if-else if statements, making code more concise and readable.

				
					switch (expression)
{
    case value1:
        // code to be executed if expression matches value1
        break;
    case value2:
        // code to be executed if expression matches value2
        break;
    ...
    default:
        // code to be executed if expression doesn't match any case
}
				
			
				
					#include <stdio.h>

int main() {
    int day = 3;
    switch (day) {
        case 1:
            printf("Monday\n");
            break;
        case 2:
            printf("Tuesday\n");
            break;
        case 3:
            printf("Wednesday\n");
            break;
        case 4:
            printf("Thursday\n");
            break;
        case 5:
            printf("Friday\n");
            break;
        case 6:
            printf("Saturday\n");
            break;
        case 7:
            printf("Sunday\n");
            break;
        default:
            printf("Invalid day\n");
    }
    return 0;
}

				
			

In this example, we have a variable day with the value 3. The switch statement evaluates this variable against different cases. Since day is 3, it matches the case 3, and the corresponding code block prints “Wednesday” to the console. The break statement is crucial to exit the switch statement once a matching case is found, preventing execution from falling through to subsequent cases.

Using Default Case: If none of the cases match the value of the expression, the code inside the default case is executed.

2. Looping Statements:

for Loop

The for loop is used to execute a block of code repeatedly for a fixed number of times.

				
					for (initialization; condition; increment/decrement)
{
    // code to be executed repeatedly
}
				
			
				
					for (int i = 0; i < 5; i++)
{
    printf("%d\n", i);
}

				
			

while Loop

The while loop is used to execute a block of code repeatedly as long as a specified condition is true.

				
					while (condition)
{
    // code to be executed repeatedly
				
			

do-while Loop

The do-while loop is similar to the while loop, but it ensures that the block of code is executed at least once, even if the condition is false.

				
					do
{
    // code to be executed repeatedly
} while (condition);

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

				
			

3. Jump Statements:

Jump statements allow you to transfer control within a program.

break statement

Used to exit the loop or switch statement.

				
					while (condition) {
    if (condition2) {
        break; // exit the loop if condition2 is true
    }
}

				
			

continue statement

Used to skip the current iteration of a loop.

				
					for (int i = 0; i < 5; i++) {
    if (i == 2) {
        continue; // skip iteration if i equals 2
    }
    // code here will not be executed when i equals 2
}

				
			

return statement

Used to exit from a function and optionally return a value.

				
					int add(int a, int b) {
    return a + b; // exit function and return sum of a and b
}

				
			

Control flow statements are essential in C programming as they allow you to make decisions, repeat code, and control the flow of execution in your program. By using conditional statements, looping statements, and jump statements effectively, you can write efficient and structured code to solve various problems. Happy coding! ❤️

Table of Contents