• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C » Switch Case in C
Next →
← Prev

Switch Case in C

By Dinesh Thakur

The switch case in C is a multi-way decision-making statement which selects one of the several alternatives based on a set of fixed values for a given expression. The switch case is mainly used to replace multiple if-else statements. The use of numerous if-else statements causes performance degradation as several conditions need to be evaluated before a particular condition is satisfied. The general form of the switch statement is

switch(expression) {
  case constant1 : statement(s);
                   [break;]
  case constant2 : statement{s);
                   [break;]
  default : statement(s) ;
}

The switch case in C starts with a keyword switch followed by the expression enclosed in the parentheses. The expression must evaluate to an integer value of type char, short, int or an enumeration is constant but not of type long, float or double. constant1, constant2, ….. are the constants or constant expression that evaluates to integral constant and are known as case labels. Some examples are 5, 'A', SHIRTSIZE (a named constant of type int). No two constants in the same switch can have identical values.

When the switch statement is executed then the expression in the parentheses following the keyword switch is compared with each of the constants in the order of their appearance in the switch statement. If a match occurs then the statement(s) corresponding to that case are executed. If no match occurs with any of the case then only the statement(s) following the default case are executed. The default part is optional in the switch statement. In that case, if none of the case values match, then no action takes place.

flowchart of switch case in cEach case can contain zero, one or more program statements. Unlike other control structures, the block of statements corresponding to a case is not required to be enclosed in curly braces.

A case is normally terminated with a break statement. When a break statement is encountered, the program comes out of the switch statement and continues with the next executable statement following the switch statement. If the break statement is not used then all the statements from the matched case in the sequence will be executed until another break statement is found or end of the switch statement is reached.

Now consider the example of switch-case statement.

#include<stdio.h>
#include<conio.h>
void main() {
  int day;
  printf ("Enter the day of the week (1 to 7) ");
  scanf ("%d", &day);
  switch(day) {
    case 1 : printf ("Today is SUNDAY");break;
    case 2 : printf ("Today is MONDAY");break;
    case 3 : printf("Today is TUESDAY");break;
    case 4 : printf("Today is WEDNESDAY");break;
    case 5 : printf("Today is THURSDAY");break;
    case 6 : printf ("Today is FRIDAY");break;
    case 7 : printf("Today is SATURDAY");break;
    default : printf("Enter a valid choice(l to 7 only)");
}
getch();
}


switch case in c program

Explanation: On execution, the user is prompted to enter the day of the week (say 4). When the program encounters the switch statement, the value of the variable named day is 4. IT starts comparing this value with the constants appearing in the case labels starting from the first one. Since the value 4 matches the integer constant 4 in the third case of the switch statement. Therefore, the associated print () statement is executed, which prints the message. Today is WEDNESDAY. A break statement is encountered next that causes the program to exit the switch statement. Finally, exit the program.

We’ll be covering the following topics in this tutorial:

  • switch v/s else-if ladder
  • Application of Switch Case

switch v/s else-if ladder

The switch case is similar to the else-if ladder as it checks several conditions. It checks the first condition, and if that condition is true, the statements under that condition are executed. The control is shifted to the statement after the switch statement, and if the condition is false, then the next condition is checked.

The switch case differs from the else-if ladder also. The expression used in else-if ladder returns (true (1) or false (0)) whereas the expression used in a switch statement can return an integer or a character constant where the values 1 for true and 0 for false are also included. Moreover, unlike the else-if ladder, the switch statement does not allow the use of && and || logical operators. In a switch statement, cases can be written in any order, but in the else-if ladder, the order is fixed.

To sum up, the else-if ladder is an effective approach when the number of conditions to be checked less. When a large number of conditions are involved, the switch is preferred.

Some key differences between switch and else-if ladder are as follows :

• switch is better because it is a better way of writing programs as compared to else-if and leads to more structured programming. Thus switch is more simple and manageable.
• switch does not allow the use of logical AND (&&) and logical OR(||) operators.
• The block of statements corresponding to a case is not required to be enclosed in curly braces. But in else-if ladder use of compound statements require braces.
• In a switch statement, a case is normally terminated with a break statement. It is not there in else if ladder.
The switch has an optional default case. It is not there in the else-if ladder.
The expression in the switch statement can result in constant of type char, int but not of type float and double. It is not in case of the else-if ladder.

Application of Switch Case

The switch statement is convenient to be used if there are a large number of alternative paths. It is particularly used in menu-driven programs. It is efficient and faster than if statement. But it can only be used in case of equality (= =) operator but not with other relational operators and it cannot be used with multiple variables.

The GOTO Statement

The goto statement is an unconditional control transfer statement that causes the control to jump to a different location in the program without checking any condition as specified by the goto. It is normally used to alter the normal sequence of program execution by transferring control to some other part of the program Like switch, break and continue statements, the goto statement also causes the control to jump over the statements. So it is also called a Control Transfer statement.

It is generally written as :

goto label ;

Here, the label is a valid identifier used to label the target statement to which control will be transferred. Control may be transferred to any other statement within the current function. You cannot jump between functions. The target statement must be followed by a colon (:). It is written as,

label: statement

The label can be set anywhere in the program above or below the goto statement, so the goto statement causes the control to be shifted either in a forward direction or in a backward direction which is controlled by the position of the label which is called a jump.

There are two types of jumps

(a) Forward Jump (b) Backward Jump

Sometimes it may be necessary to skip a set of statements in a program. To accomplish this, the process we use a goto statement. When the label is defined after the goto statement, the process of skipping is called forward jump. Forward jump using goto statement can also be used for an early exit from a loop.

The backward jump means to shift the control to a statement which is executed before. It is accomplished by a goto statement in which the label used is defined before the goto statement. It causes a certain set of statements to be repeated again and again. If this statement is used without any condition, it causes repetition of part of program infinitely, which forms an infinite loop.

transfer of control in goto statementThe goto statement when combined with a decision making the statement, i.e. if statement works similar to looping statements.

Consider the following example :

#include<stdio.h>
#include<conio.h>
main() {
 int i = 1;
 up : printf ( "\nHello" ) ;
 i++;
 if(i<=5)
   goto up;
   getch();
}

goto statement in c

This statement causes Hello to be printed 5 times which works similar to a loop. Thus, if statement used with goto statement forms a loop.

goto loop statementIn the previous example, we can see that if the expression (i==3&&j==2) is satisfied, then the control shifts to the label (out) where the statement is written is executed.

The break statement is preferred over the goto statement. As we know that C is a structured language and the use of goto tends to encourage logic skips all over the program which is against the structured feature which allows the statements to be executed in an orderly and sequential manner. Therefore the use of the goto statement is generally avoided in the case of C. But in case of a break, we can exit from the present loop only thus it avoids multiple jumps.

Occasionally goto statements can also be useful in conditions where there are situations in which it is necessary to jump out of double nested loops. If a certain condition is detected, then two, if break statements, would be needed which would be awkward. It can be made clear by seeing the above example where the goto statement makes a jump from two for loops.

#include<stdio.h>
#include<conio.h>
void main() {
  int m,n;
  printf ("Enter two numbers : ") ;
  scanf( "%d%d" ,&amp;m,&amp;n);
  if (m<n)
    goto output1;
  else
    goto output2;
    output1 :
     printf("Out of %d and %d, %d is smaller",m,n,m);
   goto stop;
   output2 :
    printf ("Out of %d and %d, %d is smaller" ,m,n,n);
   stop :
   getch();
}

goto program

You’ll also like:

  1. Switch ….Case Statement
  2. Switch Case in Python
  3. is a default case necessary in a switch statement
  4. Write a C++ Program to detect whether the entered number is even or odd. Use nested switch () case statement.
  5. Write a java program changing the case of a string(Lower case)
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C Programming

C Programming Tutorials

  • C - History
  • C - Anatomy
  • C - Constants
  • C - Identifiers
  • C - Data Types
  • C - Libraries File
  • C - Header Files
  • C - Basic Language
  • C - Data Types Sizes
  • C - Header Files Importance
  • C - Escape Sequences
  • C - Main() Purpose
  • C - Program Procedure
  • C - Control Statements
  • C - Enumeration Constant
  • C - Add numbers
  • C - Return Statement
  • C - Avoid Goto
  • C - Command Line Arguments
  • C - Switch Case
  • C - Switch Case Limitations
  • C - getchar() and putchar()
  • C - Iteration Statements
  • C - Pass by Value and Reference
  • C - Structures and Unions
  • C - Structure
  • C - Dynamic Memory
  • C - Fgets and Fputs Functions
  • C - Gets() and Puts() Functions
  • C - Armstrong Number
  • C - Storage Classes
  • C - Fibonacci Series
  • C - Precision Setting
  • C - const Parameters

C - Variable & It's Type

  • C - Variables
  • C - Variable Lifetime
  • C - Static Variable
  • C - Register Variable
  • C - Global Variables
  • C - Auto Variables
  • C - Local Variables

C - Operator & Expressions

  • C - Operator
  • C - Boolean Operators
  • C - Bitwise Operator
  • C - Arithmetic Operators
  • C - Modulus Operator
  • C - Ternary Operator
  • C - Expressions
  • C - Arithmetic Expressions

C - Array

  • C - Arrays
  • C - Array Types
  • C - Array Characteristics
  • C - Static Arrays
  • C - Global Arrays
  • C - 3D Arrays
  • C - Dynamic Arrays
  • C - Pointer to 3D Arrays
  • C - Array Elements Hold
  • C - Arrays as Function Parameters
  • C - Accessing Matrix Elements
  • C - File Handling
  • C - Matrix Multiplication
  • C - Dynamic Memory Allocation

C - Searching & Sorting

  • C - Data Structures
  • C - Linear Search
  • C - Bubble Sort
  • C - Merge Sort
  • C - Linked List
  • C - Insertion Sort
  • C - Binary Search
  • C - Selection Sort
  • C - Quick Sort

C - Functions

  • C - Functions
  • C - Functions Advantages
  • C - Void Functions
  • C - Function Call
  • C - Default Return Value
  • C - String functions

C - Pointer

  • C - Pointers
  • C - Type Casting Of Pointers
  • C - Pointer Advantages
  • C - Pointers Initialization
  • C - Vectors and Pointers

C - Differences

  • C - C Vs C++
  • C - Formal Args. Vs Actual Args.
  • C - Keywords Vs Identifiers
  • C - Strings Vs Character Arrays
  • C - Address Vs Dereference Operator
  • C - Goto Vs longjmp
  • C - Declaring Vs Defining Variable
  • C - String Vs Array
  • C - Call by Value Vs Reference
  • C - Structure Vs Union
  • C - For Vs While loops
  • C - Compiler Vs Interpreter

C - Programs

  • C Program Standard Deviation
  • C Program Calculate Tax
  • C Program Sum Series
  • C Program Merge Arrays
  • C Program Euclid’s Algorithm
  • C Program Print Weekdays
  • C Program Sum of Digits
  • C Program Print a list
  • C Program Print Pythagorean
  • C Program Quiz program
  • C Program Display Table
  • C Program Print Comma-Separated
  • C Program Prints Prime Numbers
  • C Program for Print Integer
  • C Program Count Number
  • C Program Print Color Name
  • C Program Print Odd Numbers
  • C Program Calculate area
  • C Program for a Menu
  • C Program Add Two Vectors
  • C Program Array Addresses
  • C Program Division by Zero Error
  • C Program Compare two Dates
  • C Program Tower of Hanoi
  • C Program return 3 Numbers
  • C Program for Prime Numbers
  • C Program for Factorial
  • C Program for Palindrome

Other Links

  • C Programming - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW