🌟 Join our Telegram group for exclusive updates! Join Now Get Involved

Switch Case Statements

Switch Case Statements

Switch Case Statements

The control statement that allows us to make a decision effectively from the number of choices is called a switch, or a switch case-default since these three keywords go together to make up the control statement. The expression in switch returns an integral value, which is then compared with different cases. Switch executes that block of code, which matches the case value. If the value does not match with any of the cases, then the default block is executed.

Following is the syntax of switch case-default statements:

        
switch (integer expression) {
    case {value 1}:
        do this;
        break;

    case {value 2}:
        do this;
        break;

    case {value 3}:
        do this;
        break;

    default:
        do this;
}
        
    

Understanding the Syntax:

  • The expression following the switch can be an integer expression or a character expression.
  • The case value 1 and 2 are case labels that are used to identify each case individually. Case labels should be unique for each of the cases.
  • At the end of the case labels, a colon (:) is used.
  • Each case is associated with a block containing multiple statements.
  • Whenever the switch is executed, the value of the test-expression is compared with all the cases present. When a case is found, the block of statements associated with that case will execute.
  • The break keyword indicates the end of a particular case. If break is not used in each case, C's switch will continue to execute all the cases until the end is reached.
  • The default case is optional. It will be executed when the expression's value does not match any of the cases inside the switch.

One example where we could use the switch case statement is:

        
#include <stdio.h>

int main() {
    int i = 2;

    switch (i) {
        case 1:
            printf("Statement 1");
            break;

        case 2:
            printf("Statement 2");
            break;

        case 3:
            printf("Statement 3");
            break;

        default:
            printf("No valid i to switch to.");
            break;
    }
    return 0;
}
        
    

Output:

        
Statement 2
        
    

Different from if-else. How?

There is one problem with the if statement: the program's complexity increases whenever the number of if statements increases. If we use multiple if-else statements, the code might become difficult to read and comprehend. Using the switch statement is the solution to this problem.

Furthermore:

  • Switch statements cannot evaluate float conditions; the test expression can only be an integer or a character, whereas if statements can evaluate float conditions.
  • Switch statements cannot evaluate relational operators; they are not allowed in switch statements, whereas if statements can evaluate relational operators.
  • Cases in the switch can never have variable expressions; for example, we cannot write case a + 3:

Rules for Switch Statements:

  • The test expression of switch must necessarily be an int or char.
  • The value of the case should be an integer or character.
  • Cases should only be inside the switch statement.
  • Using the break keyword in the switch statement is not necessary.
  • The case label values inside the switch should be unique.

It is not necessary to use the break keyword after every case. Break should only be used when we want to terminate our case at that time; otherwise, we won’t. We can also use nested switch statements, i.e., switch inside another switch. Also, the case constants of the inner and outer switch may have common values without any conflicts.

Cookies Consent

This website uses cookies to ensure you get the best experience on our website.

Cookies Policy

We employ the use of cookies. By accessing BYTEFOXD9, you agreed to use cookies in agreement with the BYTEFOXD9's Privacy Policy.

Most interactive websites use cookies to let us retrieve the user’s details for each visit. Cookies are used by our website to enable the functionality of certain areas to make it easier for people visiting our website. Some of our affiliate/advertising partners may also use cookies.