×

C Tutorial

C Basics

C Data Types

C Input/Output

C Operators

C Conditional Statements

C Control Statements

C Strings

C Functions

C Arrays

C Structure and Unions

C Pointers

C Preprocessor Directives

C Command-line Arguments

C File Handlings

C Graphics

C Advance Topics

C Tips and Tricks

C Important Topics

C Practice

Switch Case Tutorial, Syntax, Examples and Rules in C language

In this article, we will learn about decision making statements like switch-case-default and their uses in C programming language.
Submitted by Sneha Dujaniya, on August 13, 2018

Decision making using switch-case-default

Many times in our daily lives, we face conditions where we are required to choose between a number of alternatives rather than just two or three. For example, which school to go to, which food to have in a restaurant, which game to play, etc. Similarly, in programming languages, we sometimes face problems where we have to make the program user-friendly by giving them more than two alternatives to choose from rather than just one or two.

In such cases, it becomes a convoluted problem if we use a series of if-else statements. Therefore, C provides us a discrete control statement which is "switch" to handle such issues effectively. Let us learn how to use a switch, case and default keywords?

Syntax

The general form of the three keywords is:

switch (integral_expression)
{
	case constant_1:
	code;
	[break;]
	case constant_2:
	code;
	[break;]
	
	.
	.
	.
	
	case constant_n:
	code;
	[break;]
	
	default:
	code;
}

Rules

  1. The integer expression after the switch keyword is any valid C statement that yields an integer value. Example, integer constants like 1, 2, 100 etc.
  2. The values of constant_1, constant_2 after the case keyword can be an integer or character. But all these constants must be different from each other.
  3. The code mentioned above is the code that we want to execute. It can be anything ranging from printf to another switch-case ladder.
  4. Now, when we run our program, what happens first is the integer expression gets evaluated.
  5. The control then goes inside the switch and the value received from the integer expression is compared with the case constants.
  6. If the match is found with any case constant, that particular case will be executed along with the following case and default statements.
  7. If the match is not found, only the statements after the default get executed.

Example 1

#include <stdio.h>

int main( )
{	
	int i = 2 ;

	switch ( i )
	{
		case 1:
		printf ( "1 \n" ) ;
		
		case 2: 
		printf ( "2 \n" ) ;
		
		case 3: 
		printf ( "3 \n" ) ;
		
		default:
		printf ( "No match \n" ) ;
	}
	
	return 0;
}

Output

    2 
    3 
    No match 

In the program above, most of us expected the output to be only 2 since the value of constant i is 2. But that does not happen since all the case statements and the default gets executed after the match is found, as mentioned earlier.

To prevent this from happening, we use another statement called the break statement to get the output from that particular case only. Note that break need not be written after the default statement since the control comes out of the switch loop anyway.

Example 2

#include <stdio.h>

int main( )
{	
	int i = 2 ;

	switch ( i )
	{
		case 1:
		printf ( "1 \n" ) ;
		break;
		
		case 2: 
		printf ( "2 \n" ) ;
		break;
		
		case 3: 
		printf ( "3 \n" ) ;
		break;
		
		default:
		printf ( "No match \n" ) ;
	}
	
	return 0;
}

Output

    2

More about switch (some useful points and some disadvantages)

1) The above program need not be made in an ascending order only. It can be made in other order.

Example:

#include<stdio.h>

int main( )
{
	int i = 2 ;

	switch ( i )
	{
		case 34 :
			printf ( "1 \n" ) ;
			break; 
			
		case 2 : 
			printf ( "2 \n" ) ;
			break;
			
		case 121 : 
			printf ( "3 \n" ) ;
			break; 
			
		default :
		printf ( "No match \n" ) ;
	}

	return 0;
}

Output

    2

2) We can also use "character values" in "case and switch".

Example:

#include<stdio.h>

int main()
{

	char ch = 's';
	
	switch (ch)
	{
		case 'a':
			printf("The letter is 'a'");
			break;

		case 'b':
			printf("The letter is 'b'");
			break;
			
		case 's':
			printf("The letter is 's'");
			break;
			
		default:
		printf("No match");
	}
	
	return 0;
}

Output

    The letter is 's'

The characters are in reality replaced by their ASCII values by the compiler to make them act like integer constants.

3) If we want to write multiple statements within a particular case, we need not enclose them within a pair of braces.

4) If there is a statement inside the switch statement but it does not belong to any of the cases then that particular statement will not be executed or in other words will be skipped by the compiler.

5) It is not compulsory to add the default statement at the end of the switch. Even if we don’t write it, the program would run just the same.

6) Nested switches exist in reality but rarely used. The switch statements are mostly used for writing menu driven programs.

7) Many times, we want to execute the same set of statements under different cases. This can be done as shown below.

Example:

#include <stdio.h>

int main()
{

	char ch;
	
	printf("Enter alphabets a, b or c:\n");
	scanf("%c",&ch);
	
	switch(ch)
	{
		case 'a':
		case 'A':
			printf("The letter is 'a'");
			break;
			
		case 'b':
		case 'B':
			printf("The letter is 'b'");
			break;
			
		case 'c':
		case 'C':
			printf("The letter is 's'");
			break;
			
		default:
		printf("No match");
	}

	return 0;
}

Output

    Enter alphabets a, b or c: b
    The letter is 'b'

Here, what happens is that the cases keep executing until a break statement is found. Therefore, if for example if alphabet a is entered the case 'a' is satisfied and because there are no statements after that, the control goes to the next case i.e. case 'A' and executes the statements underneath that.

8) The switch statement is often compared with the if statement. It is better to use the switch in many cases due to the advantages listed above but also, the disadvantage of the switch is that we can't have a case which contains conditionals like: case i > 10:




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.