Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

Switch case statement in C language

Posted on August 26, 2016January 29, 2020

Table of Contents

  • Switch case statement in C language
      • Switch case in c language
      • How to work switch statements

Switch case statement in C language

In this tutorial, we will discuss the concept of the Switch case statement in C language

Switch case in c language

A Switch statement helps a variable for compare against a list of values. Each value called a case. Value of every case statements can be checked for equality value of switch statement

In this tutorial, We can understand the switch case statement in C language. we can have any number of case statements within a switch and  each case statement ending a colon with a value able to compare

Every case statements inside the switch statement must be the same data type of the variable and case statement must be unique.

when checking all case statement, if there are false, default statement executed and output is displayed

 

syntax

 switch (x)
​{
case statement1:
// code to be executed if n is equal to statement(s) 1;
break;

case statement2:
// code to be executed if n is equal to statement(s) 2;
break;

case statement2:
// code to be executed if n is equal to statement(s) 3;
break;     .
.
.
default:
// code to be executed if n doesn’t match any constant
}

Flow diagram

How to work switch statements

  • The switch statement is evaluated once
  • The value of the switch expression is compared with the value of each case.
  • When there is a match of the particular case, the associated block of statements is executed.
  • When there is not match any cases, the default statement is executed.
Program 1
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int marks=30; //local variable diclaration
    switch(marks){
    case 90:          //if marks is 90
    printf(“Excellentn”);  //display this statement
    break;
    case 80:              //if marks is 80
    printf(“very welln”);  //display this statement
    break;
    case 70:               //if marks is 70
    printf(“you are a clever studentn”);  //display this statement
    break;
    case 60:               //if marks is 60
    printf(“you passedn”);  //display this statement
    break;
    case 50:                  //if marks is 50
    printf(“better try againn”);  //display this statement
    break;
    case 40:                    //if marks is 40
    printf(“not enough try againn”);  //display this statement
    break;
    case 30:                    //if marks is 30
    printf(“sorry you are failn”);  //display this statement
    break;
    default:
    printf(“not a marksn”); //every cases are false
    break;                   //display default statement
    }
    printf(“your marks is:%dn”,marks);
    return 0;
}
The above code is executed, it produces following reault
at the above program, value of  every case compare the value of switch statement
      case 60: //if marks is 60
    printf(“you passed”);  //display this statement
    break;
value is 60; this case be equal to switch, so this statement is displayed
When the above code is executed, it produced the following result
you passed
your marks is :60
 

Suggetsed for you

While Loop in Java      While Loop in C++     While Loop In C

Do-while Loop in Java    Do-while Loop C++      Do-while loop in C 

For Loop in C++       For Loop in C         For loop in java

 

Switch statement in Java        Switch statement in C++

 

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes