C - IntroductionC - Hello World ProgramC - Exercise 1C - Basic structure of a C programC - TokensC - Data TypesC - Type ConversionC - Exercise 2C - Character Input Output OperationsC - Input Output operation using scanf and printf functions

Operators

C - Arithmetic OperatorsC - Relational OperatorsC - Logical OperatorsC - Assignment OperatorsC - Increment Decrement OperatorsC - Bitwise Operators

Precedence and Associativity

C - Precedence and AssociativityC - Exercise 3

Conditions

C - If Else decision making statementsC - Switch Case decision making statements

Loop

C - While LoopC - Do While LoopC - For LoopC - Exercise 4

Array

C - ArraysC - Two Dimensional ArraysC - Multi Dimensional ArraysC - Exercise 5

String

C - StringC - Exercise 6C - String Manipulation

Functions

C - FunctionsC - Functions CategoryC - Function Call - Flow of ControlC - RecursionC - Functions and ArraysC - Functions and Strings

Structures

C - StructuresC - Structures and ArraysC - Passing structure to functionC - Function returning structureC - Structure in Structure

Pointers

C - PointersC - Pointers and VariablesC - Pointers and Variables Memory RepresentationC - Pointers Chaining

Pointers and Arrays

C - Pointers and One Dimensional ArrayC - Pointers and Two Dimensional ArrayC - Array of Pointers

Pointers and Strings

C - Pointers and Strings

Pointers and Functions

C - Pointers and Functions - Call by Value and Call by ReferenceC - Function returning pointer

Pointers and Structures

C - Pointers and StructuresC - Pointers and Array of StructuresC - Passing structure pointer to function

Handling Files

C - File Handling - Getting StartedC - File Handling - Read and Write CharactersC - File Handling - Read and Write IntegersC - File Handling - Read and Write multiple dataC - File Handling - Randomly Access Files

Command Line Arguments

C - Command Line Arguments

Dynamic Memory Allocation

C - Dynamic Memory Allocation - Getting StartedC - Dynamic Memory Allocation - malloc functionC - Dynamic Memory Allocation - calloc functionC - Dynamic Memory Allocation - realloc function

C - Switch Case decision making statements

C Programming

In this tutorial we will learn about switch-case decision making statements in C programming language.

Switch case statements are somewhat similar to the if else statement.

Syntax of a switch-case statement.

switch (expression) {
  case value1:
    //block 1 code...
    break;

  case value2:
    //block 2 code...
    break;

  default:
    //default block code...
}

So, in the above syntax of a switch-case statement we have an expression which is matched with the case values value1, value2 and so on.

If lets say, the expression matches the case value2 then the block-2 code is executed and others are ignored.

The break keyword takes us out of the switch. It is recommended to have the break statement in the case block in order to jump out of the switch-case statement.

If no case value matches the expression then the default block code is executed.

Note! The default block is optional and can be skipped. And we don't have to add the break keyword in the default block.

Points to remember

  • expression is an integer or character
  • value1, value2, ... are integer constant and are called case label
  • block1, block2, ... contain zero or more statements
  • default is an optional case. If no case label matches the expression then the default-statement is executed
  • break statement signals the end of a particular case and causes an exit from the switch-block

In the following example we will take an integer value from the user as input and then match the value with the cases to print the suitable result.

#include <stdio.h>
int main(void)
{
  //declare variable
  int x;
  
  //take user input
  printf("Enter an integer number: ");
  scanf("%d", &x);
  
  //check condition
  switch (x) {
    case 1:
      printf("Entered number: 1\n");
      break;
    
    case 2:
      printf("Entered number: 2\n");
      break;
    
    case 3:
      printf("Entered number: 3\n");
      break;
    
    default:
      printf("Entered number is something else.\n");
  }
  
  printf("End of code\n");
  
  return 0;
}

Output

Enter an integer number: 1
Entered number: 1
End of code

In the above output we have entered 1 as input so, in the switch-case statement the value of x (expression) is matched with case 1. Hence, the case 1 block is executed. And because of the break keyword we jump out of the switch.

Output: When entered number is something else.

Enter an integer number: 10
Entered number is something else.
End of code

In the above output we have entered 10 as input value and no case value is a match. So, we execute the default block code.

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT