×

C Switch Statements

Switch-case statement comes under the Selection control statement; there are four types of Control statements

  1. Decision-making statements (if, if-else)
  2. Selection statements (Switch-case)
  3. Iteration statements (for, while, do-while)
  4. Jump statements (break, continue, goto)

If we want to write a program with one condition, then we use if-else statements. And if we want to write a program containing many conditions, then we will use an if-else ladder that supports many conditions simultaneously.

Instead of using the if-else ladder, we use the switch statement. In the switch statement, we build many blocks known as cases, and there must be a default case at the end of every switch statement.

  • In the switch statement, we pass a value that will check all the case’s values. If that value matches the case value, then that case will be executed. Otherwise, the default case will be executed.
  • In the if-else ladder, the conditions are evaluated continuously. But in the switch statement, the expression is evaluated only once.

When the expression value is matched with the case value, then that case statement will be executed until it reaches the break statement in the particular block.

  • After every case, we use a break statement to come out of the switch block and stop the execution. The program will not stop execution if we forget to use a break statement in our program.
  • If there is no case value matching the expression value, then the default statement will be executed. We will not give a break statement in the default case.
  • In the switch statement, all the cases are numbered like case 1, case 2, case 3,………, and case n.

Syntax for switch statement:

Switch ( expression )
{
Case 1: {
		Statement (s);
		break;
	         }
Case 2: {
		Statement (s);
		break;
	        }


Case 3: {
		Statement (s);
		break;
	       }
//we can have any number of case statements in the program
Default: {
		Statement (s);
	         }
}

Example 1:

//simple program for switch-case statement
#include <stdio.h>            //pre-processor standard input and output
#include <conio.h>	//pre-processor clear screen before scanning the value
Int main ()
{
int num;	//declaring integer variable
clrscr();	//clear screen command
printf(“enter the value of num:”);
scanf(“%d”, &num);	//scanning the variable data


Switch(num)	//passing the scanned value in the switch statement
{
Case 1:	//declaring case 1 with executable statements
	printf(“Hi all this is case-1”);
	Break;
Case 2:	//declaring case 2 with executable statements
	printf(“Hello, this is case-2”);
	Break;
Case 3:	//declaring case 3 with executable statements
	printf(“Hello, this is case-3”);
	Break;
Case 4:	//declaring case 4 with executable statements
	printf(“Hello, this is case-4”);
	Break;
Default:	//declaring default case with print statements
	printf(“Invalid choice”);
}
return 0;
}

Output:

Test case 1:

Enter the value of num: 4
Hello, this is case 4.

Explanation:

In the above test case, we have given the number 4, and in the program, we have given case 4 both the numbers are matched. So, case 4 will be executed.

 Test case 2:

Enter the value of num: 8
Invalid choice

Explanation:

In the above test case, we have given the number 8, and in the program, we have not declared any case 8. Here expression value and case value are not matched. So, the default case will be executed.

Example 2:

//program for switch-case statements
#include <stdio.h>            //pre-processor standard input and output
#include <conio.h>	//pre-processor clear screen before scanning the value
Int main ()
{
int n;	//declaring integer variable
clrscr();	//clear screen command
printf(“enter the value of n:”);
scanf(“%d”, &n);	//scanning the variable data


Switch(n)	//passing the scanned value in the switch statement
{
Case 10:	//declaring case 1 with executable statements
{
      Int a;	//declaring integer variable a
      Printf(“enter the a value:”);
      Scanf(“%d”, &a);              //scanning the variable data(a)
      printf(“The division of two numbers is %d”,(n/a));           //printing the result
	Break;
}
Case 20:	//declaring case 2 with executable statements
{	
       Int b;                       //declaring integer variable b
	Printf(“enter the b value:”);
      Scanf(“%d”, &b);                 //scanning the variable data(b)
      printf(“The multiplication of two numbers is %d”,(n*b));    //printing the result
	Break;
}
Case 30:	//declaring case 3 with executable statements
{
	Int a;                    //declaring integer variable a
	Printf(“enter the a value:”);
      Scanf(“%d”, &a);      //scanning the variable data(a)
printf(“The addition of two numbers is %d”,(a+n));           //printing the result
	Break;
}
Case 40:	//declaring case 4 with executable statements
{
	Int b;                                //declaring integer variable b
	Printf(“enter the b value:”);
       Scanf(“%d”, &b);       //scanning the variable data(b)
printf(“The subtraction of two numbers is %d”, (n-b));        //printing the result
	Break;
Default:	//declaring default case with print statements
	printf(“Invalid choice”);
}
return 0;
}

Output:

Test case 1:

Enter the value of n: 40
Enter the b value: 25
The subtraction of two numbers is 15.

Explanation:

We have given the expression value 40, so in the program, there is a case value 40. Both are the same, so case 40 will be executed. And the output will be (40-25) = 15.

Test case 2:

Enter the value of n: 20
Enter the b value: 5
The multiplication of two numbers is 100.

Explanation:

We have given the expression value 20, so the program has a case value matching the expression value. Both are the same, so case 20 will be executed. And the output will be (20*5) = 100.

Test case 3:

Enter the value of n: 10
Enter the a value: 2
The division of the two numbers is 5.

Explanation:

We have given the expression value 10, so the program has a case value matching the expression value. Both are the same, so case 10 will be executed. And the output will be (10/2) = 5.

Test case 4:

Enter the value of n: 30
Enter the a value: 10
The addition of the two numbers is 40.

Explanation:

We have given the expression value 30, so the program has a case value matching the expression value. Both are the same, so case 30 will be executed. And the output will be (30+10) = 40.

Test case 5:

Enter the value of n: 70
Invalid choice.

Explanation:

We gave the expression value 70, so the program doesn’t have a case value matching the expression value. So the out will be “Invalid choice”.


Related Topics

C Program to Find the Largest Number using Ternary Operator

In this tutorial, we will write some program to determine which of the provided integers is larger using the ternary operator, also known as the conditional operator in C. Ternary Operator: The ternary...

3 minutes read.

Type Conversion in the C

The process of changing one data type to another in the C programming language is known as "type conversion." The type conversion method is only executed on data types that...

4 minutes read.

C Program to find the size of a File

Before knowing about the Program, you need to understand what the size of a file is. After that, you should know how it is going to work with the given...

4 minutes read.

Multiplication table program in C using For loop

Before we move on the program of multiplication table of any natural number, first we have to know about the For loop statement. The syntax of ‘for’ loop in C programming...

3 minutes read.

Builtin functions of GCC compiler

Certain built-in functions are present in the GCC compiler. These features are as follows: Function_ built in_popcount (x) The number of 1s in data of the integer type can be counted using...

3 minutes read.

Multi-dimensional Arrays in C

Multi-dimensional Arrays in C The C programming allows the concept of multi-dimensional arrays. The data within the multidimensional arrays are stored in the tabular form, that is, in a row significant...

3 minutes read.

For loop in C Programming

In the C programming language, the for loop is used to repeatedly iterate over a set of instructions or a section of code. It is frequently used to traverse array-based...

3 minutes read.

Data Structures And Algorithms in C

C language is one of the most flexible and simple languages. So, it is always better to learn any concept in C language compared to other languages. Let us look...

13 minutes read.

Assignment Operator Program in C

An assignment operator is a symbol used to assign a value to a variable in a programming language. The assignment operator is often the equal symbol (=) in programming languages....

12 minutes read.

Positioning of file in C

Positioning() function in c The fseek() function is utilized to change the document position of the stream . The fully extent of value from the source should be one of the...

3 minutes read.

Consumer billing system in C

Introduction : Billing has always been taught to do perfectly, we know today's world is full of products, and We all are using multiple products. We are buying and selling various...

9 minutes read.

Data Types in C

Data type is a very important concept in C programming language. Simply, “A data type is the classification of data values that a data item can have.” We need to...

11 minutes read.

C program to Store Information of Students Using Structure

What is the Structure in C? User-defined data types include structures. Structures aid your ability to combine things of various categories into a single group. Like arrays, it operates similarly. A...

3 minutes read.

Limitations of Inline Function in C

The compiler is unable to conduct inlining in two instances. It just accepts the inline definitions and produces space for the function in the same way it does for a...

3 minutes read.

Flow Chart of For loop in C

This is a flowchart that represents the process of executing the for loop in the C programming language. Generally, as we know there are three main components of for loop:1. The...

3 minutes read.

strtok() and strtok_r() functions in C with examples

strtok() and strtok_r() functions in C with examples C offer various strtok() and strtok r() methods for a string to be separated by a certain delimiter. Dividing a string is a...

2 minutes read.

Expressions in C

Expressions in C: In the C programming language, an expression defines a formula in which the operands are linked to each other by using operators to compute the value. The operand...

4 minutes read.

#define in C

#define in C In the C programming language, the preprocessor directive acts an important role within which the #define directive is present that is used to define the constant or the...

3 minutes read.

While Loop Syntax in C

What is Loop? The statements in the sequence are repeatedly executed via looping statements in C until the condition is met. The body of a loop and a control statement make...

4 minutes read.

Bar3d() function in C Graphics

The bar3d function is used to create a 2-dimensional filled-in rectangular bar. We can also create three-dimensional shapes in C using helpful function. The first step in creating this 3D...

3 minutes read.