Skip to the content
onlineexamguide
  • Home
  • Courses
  • Engg. Interview
    • Placement Papers
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • NTA UGC NET Exam
    • SSC Examination quiz
    • TET Examination Quiz
    • Banking Exam
    • Aptitude Tests
    • Computer Knowledge Tests
    • Logical Reasoning Tests
    • English Language Tests
    • Staff Nurse Exams
    • General Knowledge Tests
    • Networking Tests
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login
  • Home
  • Courses
  • Engg. Interview
    • Placement Papers
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • NTA UGC NET Exam
    • SSC Examination quiz
    • TET Examination Quiz
    • Banking Exam
    • Aptitude Tests
    • Computer Knowledge Tests
    • Logical Reasoning Tests
    • English Language Tests
    • Staff Nurse Exams
    • General Knowledge Tests
    • Networking Tests
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login

Switch Case in C Programming Tutorial

Switch Case in C

Learn Switch Case in C Tutorial and C MCQ Questions and Answers on Switch Case Syntax to attend job placement exams, interview questions, college viva and Lab Tests.

Switch Case in C

C language provides IF, ELSE IF and ELSE conditional statements to suit various programming needs. Using so many if-else statements look clumsy some times. So, C language an alternative to if-else i.e SWITCH CASE.

Keywords used to implement a SWITCH CASE are switch, case, break and default.

Syntax:

switch(integer constant or expression)
{
  case constant1: statement1;
  case constant2: statement2;

  default: statement4;
}

Usual way of implementing a SWITCH

switch(integer constant)
{
  case constant1: statements1; break;
  case constant2: statements2; break;
  case constant3: statements3; break;

  default: statement4;
}

In the above switch control statement, an integer constant is accepted as a constant. You can use any int data type variable or integer expression as INPUT to a switch. Order of switch implementation is explained below.

  1. Step 1: The input CONSTANT is compared against first case constant i.e constant1. If it matches, corresponding statements are executed.

2. Step 2: If there is no break; after statements, statements in next case constant section are also executed. There also if there is no break, control goes to the next case constant statements and so on. This way of executing all the statements down the ladder are called SWITCH CASE FALL THROUGH.

3. Step 3: Finally, if no case constant is matched with input CONSTANT, default case statements are executed. Usually we keep default case at the end of SWITCH. Placement or order of DEFAULT can be anywhere.

4. Step 4: Switch CASE CONSTANTS can not be Variables.

5. Step 5: There is no effect of CONTINUE statement in a Switch Statement.

6. Step 6: Proper use of break statement at the end of statements ensures no side effects in SWITCH implementation.

Switch Case in C Example

1:

void display();
int main()
{
  int b=8;
  display(b);
  display(10);

  return 9;
}
void display(int d)
{
  switch(d)
  {
    case 6: printf("SIX");break;
    case 8: printf("EIGHT"); break;
    case 9: printf("NINE"); break;

    default: printf("UNKNOWN");break;
  }
  printf("\n");
}

//output
//EIGHT
//UNKNOWN

Above program prints a number in text format using switch case.

Example 2:

int main()
{
  switch('c')
  {
    case 'a': printf("A");break;
    case 'b': printf("B"); break;
    case 'c': printf("C"); break;

    default: printf("UNKNOWN");break;
  }
}
//output
//C

You can character constants inside CASE constants. ASCII values of character constants are nothing but integer numbers.

C Programming Switch Case Tutorial online test

[WpProQuiz 30]

Write a comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • Atal Bihari Vajpayee | श्री अटल बिहारी वाजपेयी जीवन परिचय
  • सम्राट अशोक का जीवन परिचय (Emperor Ashoka)
  • Prithviraj Chauhan
  • बाल गंगाधर तिलक
  • स्वामी दयानंद सरस्वती
  • UPPSC Mines Inspector Recruitment 2022 Notification Out
  • AIIMS Delhi JR Vacancy 2022 [194 Post] Notification and Apply Online
  • भीमराव अम्बेडकर
  • डॉक्टर राजेंद्र प्रसाद का जीवन परिचय
  • श्रीनिवास रामानुजन का जीवन परिचय
  • Amnesty International day
  • World Economic Forum
  • UPSSSC VDO Syllabus and Exam Pattern 2022
  • RBI Officer Grade B Recruitment 2022
  • UKMSSB Assistant Professor Recruitment 2022 Apply Now 339 Post

onlineexamguide

onlineexamguide.com is the ultimate guide that will keep you updated about almost every Exam & Interviews . We aim to provide our readers with an informative details that have been occurring in Examination . Here at onlineexamguide.com , we focus on delivering our readers with the latest exam Pattern Mock test

We Provide Free online test to practice for Competitive exams , Online Exam, Entrance and Interview. Learn and Practice online test for Free and Prepare for your exam online with us

Quick links

  • About us
  • Privacy Policy
  • Instructor Registration
  • Student Registration
  • Java Programming Tests
  • C programming Tests
  • C++ programming Tests
  • Aptitude Tests

Follow us

Free Online Mock Test

  • UPTET PRIMARY Online Test Series
  • Super TET Mock Test in Hindi 2022
  • CTET Mock Test 2022 Paper 1
  • SSC CHSL Online Mock Test
  • SSC MTS Mock Test 2022
  • SSC CGL Mock Test
  • SSC GD Mock Test
  • ccc online test

Search

Learn and Earn

Register as Instructor - Create and sell online courses and coaching services with the best online platform onlineexamguide.com . Build a course, build a brand, earn money

Contact us

For any queries

Email us on - admin@onlineexamguide.com

We will response fast as much as we can
Copyright © 2022 onlineexamguide.com - All Rights Reserved.
error: Content is protected !!

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.