The C Switch Statement: A Basic Introduction

c switch statementConditional statements in any programming language are used to execute a statement or a group of statements (lines of code) thus changing the program’s control flow based on certain condition. In C language there are five conditional statements. The ‘if’, ‘if else’, ‘else if’, ‘switch’ and ‘goto’. These uses depend upon the readability, maintenance and complexity of the code. The ‘switch’ statement is mostly used when all the above factors are needed and decision regarding the flow has to be made between more than two options.

To learn more about C conditional statements, take a course at Udemy.com.

What is the C “switch” Statement?

Like other conditional statements, ‘switch’ is a type of selection control mechanism which allows the value of a variable or expression to be tested for equality against a list of constant expressions (each one is called a case), thus transferring the control flow to the matched case.

C switch Syntax

switch(variable/expression)

{

 

case constant-expression :

valid_C_statement(s);

break;

 

case constant-expression :

valid_C_statement(s);

break;

.

.

.

 

default:

valid_C_statement(s);

}

 

In the above syntax of C switch, the following are some of the important points that must be kept in mind.

  • In the C ‘switch,’ the constant-expression for a case must be of the same data type as the variable in the switch, and it only can be a constant or a literal (in this case literal is a value that can be turned into an integer, such as the value of a char).
  • The ‘break’ and ‘default’ statement are optional in C ‘switch’ syntax.
  • The ‘switch’ statement can have any number of cases, but within one ‘switch’ statement no two cases can have same constant-expression. So the value of case constant-expression must be unique.

How C switch Statement Works?

During execution, a ‘switch’ statement passes the control to the block of code whose case (constant-expression) matches with the value of switch (expression). Execution of code starts from the matching case statements and proceeds till the end of switch body or until a ‘break’ statement is encountered which leads execution out of the switch body. If no case matches the value of the switch expression, the control shifts to the ‘default’ statement.

char student_grade = 'D';

switch(student_grade)

{

case 'A' :

printf("Passed: Excellent!\n" );

break;

case 'B' :

printf("Passed: Good keep it up.\n" );

break;

case 'C' :

printf("Passed: Well done.\n" );

break;

case 'D' :

printf("Passed: Try harder next time.\n" );

break;

case 'F' :

printf("Try next time.\n" );

break;

default :

printf("Invalid grade\n" );

}

printf("You secure grade %c.\n", student_grade);

In the above code snippet, there is a character type variable named ‘student_grade’. This variable has a value ‘D’ (grade of a student). This is the expression passed to ‘switch’ statement which will match this value with cases constant-expression (grading criteria).

Code execution in ‘switch’ body starts by matching case ‘A’ constant-expression with ‘switch’ expression. When the constant-expression of the first case statement doesn’t match, execution shift to next case till it has a matched case ‘D’ and case statement is executed which outputs  output the string ‘Passed: Try harder next time.’. Next statement is ‘break’ which takes control outside the ‘switch’ body and a last line is execute output the string ‘You secure grade D.’

For more interesting C tutorials, look at Udemy.com courses.

Using ‘break’ Inside C switch Statement

The ‘break’ statement at the end of an each case is used to terminate the ‘switch’ statement preventing comparison with any subsequent cases.

In the above code snippet after execution of case ‘D’ statements, the next case ‘F’ was not compared for a match instead break statement terminated the ‘switch’.

As the ‘break’ statement is optional means it is not necessary to use in each case of a ‘switch’ statement. Like in a scenario when there have some students with ‘incomplete’ grade status, the ‘break’ statement can be skipped. Following code snippet demonstrates this concept.

char student_grade = 'I';

switch(student_grade)

{

case 'A' :

printf("Passed: Excellent!\n" );

break;

case 'B' :

printf("Passed: Good keep it up.\n" );

break;

case 'C' :

printf("Passed: Well done.\n" );

break;

case 'D' :

printf("Passed: Try harder next time.\n" );

break;

case 'I':

printf("Incomplete: ");

case 'F' :

printf("Try next time.\n" );

break;

}

printf("You secure grade %c.\n", student_grade);

 

In the above code snippet ‘break’ statement is missing in case ‘I’. In the code execution when case ‘I’ is matched with the ‘switch’ expression. The ‘I’ case statement executes with output ‘Incomplete: ’ on console but after that the control falls on subsequent case which is ‘F’ because no ‘break’ is encounter by the code execution. Following is the complete output.

Incomplete: Try next time.You secure grade I.

Using ‘default’ inside switch statement

In C ‘switch’ statement the ‘default’ only executes when there is no matching case constant-expression with the switch expression or variable. Only one ‘default’ statement is allow throughout the ‘switch’ body and need not to be at the end.

It is optional in C ‘switch’ statement but is a good practice to implement it. Following is a scenario in which it can be used.

char student_grade = 'G';

switch(student_grade)

{

case 'A' :

case 'B' :

case 'C' :

case 'D' :

printf("You Passed.\n" );

case 'F' :

printf("You Failed.\n" );

break;

default :

printf("Invalid grade\n" );

}

In the above code snippet when the expression of switch has values from ‘A’ to ‘D’ the output is ‘You Passed.’ and is ‘You Failed.’ in case of ‘F’. But when the value (‘G’) is none of grading criteria cases, the ‘default’ statement executes and displays a warning message ‘Invalid grade’ to notify program user.

It is convenient to use C Switch statements when there are multiple options to choose from for repeated if else statement require more space and are less readable as compared to the switch statement,  however too many cases can clutter the program therefore it is not advisable to use  C switch statements against too many cases.

Interested in learning more about C language? Take a course at Udemy.com.