Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Branching Statements in C

Last Updated on August 18, 2023 by Mayank Dham

The C programming language is a procedural programming language that runs programs sequentially from top to bottom. However, due to certain requirements, we may need to skip some lines of code. For this, Branching Statements in C are useful. We are going to discuss different types of branching statements in C, along with examples of each statement.

Branching Statements in C

Branching Statements in C are those statements in C language that helps a programmer to control the flow of execution of the program according to the requirements. These Branching Statements are considered an essential aspect of the Programming Languages and are essentially used for executing the specific segments of code on basis of some conditions.

Types of Branching Statements in C

The Branching Statements in C are categorized as follows.

  • Conditional Branching Statements in C
    • if Statement
    • else Statement
    • else-if Statement
    • switch Statement
  • Unconditional Branching Statements in C
    • goto Statement
    • break Statement
    • continue Statement

All of these statements are explained below in detail. So, let’s start.

Conditional Branching Statements in C

Conditional Branching Statements in C are used to execute the specific blocks of code on the basis of some condition (as per requirements). These type of Branching statements in C enables the programmers to execute the code only and only certain statements are met. The following are different types of conditional branching statements in C.

  • if statement
  • else statement
  • else-if statement
  • switch statement

if Statement

This statement is used to execute the specific block of code if a certain condition is evaluated to be true.

Syntax of if Statement in C

Here is the syntax of the if statement in C.

if (condition) {
    //code to be executed if condition specified evaluates is true
}

Here in the above syntax “condition” is a logical expression that is evaluated for being true or false. If the condition is evaluated to be true, the code within curly braces will be executed but if the condition is false, the code in braces will be skipped.

Example of if Statement in C

Here is an example demonstrating the if statement in action.

#include <stdio.h>


int main() {
    int x = 10;


    if (x > 5) {
        printf("x is greater than 5");
    }


    return 0;
}

Output

x is greater than 5

Explanation:
In the above example, the "if" statement checks whether the value of "x" is greater than 5. Since the condition is true (x is indeed greater than 5), the code within the curly braces is executed, and the message "x is greater than 5" is printed to the console.

else Statement

The else Statement in C is considered just the opposite of the if statement. This statement is used to execute the code if the condition specified in the if statement evaluates to false.

Syntax of else Statement in C

Here is the syntax of the else Statement.

if (condition) {
    // statemnets
} else{
    // code executed if condition is false
}

Example of else Statement in C

Here is an example of how the else statement works in C.

#include <stdio.h>


int main() {
   int x = 10;


   if (x < 5) {
      printf("x is less than 5\n");
   } else {
      printf("x is greater than or equal to 5\n");
   }


   return 0;
}

Output

x is greater than or equal to 5

Explanation:
In this example, the if statement in C checks whether the value of the variable x is less than 5 or not. Since the value of x (which is 10), this condition is false, and the code within the "if" block is not executed. Instead, the code within the "else" block is executed, which prints the message "x is greater than or equal to 5" to the console.

else if Statement

The else if statement in Cis used when we want to check multiple conditions. It follows an "if" statement and is executed if the previous "if" statement’s condition is false.

Syntax of else-if Statement in C

Here is the syntax for the "else if" statement:

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

The "else if" statement can be repeated multiple times to check for multiple conditions. If all conditions are false, the code within the final "else" block will be executed.

Example of else-if Statement in C

Here is an example of the "else if" statement in action.

#include <stdio.h>


int main() {
    int x = 10;


    if (x > 15) {
        printf("x is greater than 15");
    }
    else if (x > 5) {
        printf("x is greater than 5 but less than or equal to 15");
    }
    else {
        printf("x is less than or equal to 5");
    }


    return 0;
}

Output

x is greater than 5 but less than or equal to 15

Explanation:
In the above code, we have used a complete block of if, else if and else statements. The if statement first checks whether the value of x is greater than 15 or not. Since x(which is 10) is less than 15, so the control moves to the else if block. Now the program checks whether x is greater than 5 or not. This time the condition is true, so the code block inside this else-if block will be executed and we get the required output on the screen i.e., "x is greater than 5 but less than or equal to 15".

switch Statement

The switch statement in C is used when we have multiple conditions to check. It is often used as an alternative to multiple "if" and "else if" statements.

Syntax of switch Statement in C

The syntax for the switch statement is as follows:

switch (expression) {
    case value1:
        // code to be executed if expression equals value1
        break;
    case value2:
        // code to be executed if expression equals value2
        break;
    ...
    default:
        // code to be executed if none of the cases match
        break;
}

The "expression" in the above syntax is the value being evaluated. Each "case" statement represents a possible value of the expression. If the expression matches one of the "case" statements, the code block within the matching "case" statement is executed. If none of the "case" statements match, the code within the "default" block is executed.

Example of switch Statement in C

Here is an example how the switch statement in C works.

#include <stdio.h>


int main() {
    int x = 2;


    switch (x) {
        case 1:
            printf("x is 1");
            break;
        case 2:
            printf("x is 2");
            break;
        case 3:
            printf("x is 3");
            break;
        default:
            printf("x is not 1, 2, or 3");
            break;
    }


    return 0;
}

Output

x is 2

Explanation:
In the above example, the "switch" statement checks the value of "x". Since "x" is equal to 2, the code within the second "case" statement is executed, and the message "x is 2" is printed to the console.

Unconditional Branching Statements in C

Unconditional branching statements are used in C to change the normal flow of program execution. These statements allow programmers to jump to a specific point in their code regardless of any condition.

There are the following types of unconditional branching statements in C.

  • goto Statement
  • break Statement
  • continue Statement

Let us discuss these in detail one by one.

goto Statement

The "goto" statement is an unconditional branching statement that allows programmers to jump to a specific labeled statement within their code.

Syntax of goto Statement in C

Here is the syntax for the "goto" statement:

goto label;

...

label:
    // code to be executed

The "goto" statement jumps to the statement labeled "label" and executes the code within that block. The "label" can be any valid identifier followed by a colon (:).

Example of goto Statement in C

Here is an example of the goto statement.

#include <stdio.h>


int main() {
    int x = 0;


    start:
    x++;


    if (x < 10) {
        goto start;
    }


    printf("x is %d", x);


    return 0;
}

Output

x is 10

Explanation:
In the above example, the program uses a goto statement to jump back to the "start" label after incrementing "x". The if statement checks whether "x" is less than 10, and if it is, the program jumps back to the "start" label. This process continues until "x" is equal to 10, at which point the program exits the loop and prints "x is 10" to the console output.

break Statement

The break Statement is one of the unconditional Branching Statements in C which is generally used in the loops for exiting the loop before the completion of the loop.

Syntax of break Statement in C

Here is the syntax for the "break" statement in C.

break;

When we use the above statement within any loop, it simply exits the loop regardless of the completion of loop.

Example of break Statement in C

Here is an example of the break statement in C within a while loop:

#include <stdio.h>


int main() {
    int x = 0;


    while (x < 10) {
        x++;
        if (x == 5) {
            break;
        }
    }


    printf("x is %d", x);


    return 0;
}

Output

x is 5

Explanation:
In the above example, the program uses a "while" loop to increment "x" until it is equal to 5. When "x" is equal to 5, the "break" statement is executed, causing the program to exit the loop prematurely. The final output is "x is 5".

continue Statement

The continue statement is used in C programming language to skip the current iteration of a loop and move to the next iteration. This statement is typically used in loops like for or while.

Syntax of continue Statement in C

continue Statement in C has following syntax.

continue;

When the continue statement is encountered inside a loop, the current iteration of the loop is terminated, and the control jumps back to the top of the loop to start the next iteration.

Example of continue Statement in C

This example shows the working of the continue Statement in C.

#include <stdio.h>


int main() {
    int i;
   
    for (i = 0; i < 10; i++) {
        if (i % 2 == 0) {
            continue; // skip even numbers
        }
       
        printf("%d ", i);
    }
   
    return 0;
}

Output

1 3 5 7 9

Explanation:
In the above example, the for loop runs from 0 to 9, and when the loop variable i is even, the continue statement is executed, and the control moves to the next iteration of the loop. Thus, the even numbers are skipped, and only odd numbers are printed by the printf statement.

Conclusion
Conditional and unconditional branching statements in C are powerful tools that allow programmers to change the flow of their programs based on specific conditions. The "if-else" statement allows programmers to execute code blocks based on logical conditions, while the "switch" statement allows programmers to select a code block based on a single value. Unconditional branching statements, such as the "goto" and "break" statements, allow programmers to jump to specific points in their code or exit loops and switch statements prematurely.

It is important to use branching statements carefully and judiciously to avoid creating code that is difficult to read or debug. In general, branching statements in C should be used sparingly and only when necessary to improve the clarity and organization of the code.

Branching Statements in C – FAQs

Here are some Frequently Asked Questions on Branching Statements in C.

Ques 1. Can the "switch" statement be nested in C?
Ans. No, we cannot nest the switch statement in C. This means that a switch statement in C cannot contain another switch statement inside it.

Ques 2. How many "case" statements can a "switch" statement have in C?
Ans. A "switch" statement in C can have any number of "case" statements.

Ques 3. What happens if none of the "case" statements match in a "switch" statement in C?
Ans. If none of the "case" statements match in a "switch" statement in C, the code within the "default" block is executed.

Ques 4. Can the "break" statement be used outside of loops and switch statements in C?
Ans. No, the "break" statement can only be used within loops and switch statements in C.

Ques 5. Can the "goto" statement be used to jump to a label defined in another function in C?
Ans. No, the "goto" statement can only jump to a label defined within the same function in C.

Leave a Reply

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