Switch in C

Instead of writing many if.else statements , you can use switch statements in C. The Switch statement in C selects one of many blocks of code to be executed.

Switch in C Picture 1

Basic syntax of Switch in C

switch(expression) { case x: // code block break; case y: // code block break; default: // code block }

Here's how it works:

  1. The expression switchis evaluated once.
  2. The value of the expression is compared with the value of each case.
  3. If there is a match, the relevant block of code will be executed.
  4. The command breakexits the switch block and stops execution.
  5. The command defaultis optional, specifying some code to run if there is no matching case.

The example below uses the number of days of the week to calculate the day of the week:

For example:

#include int main() { int day = 4; switch (day) { case 1: printf("Monday"); break; case 2: printf("Tuesday"); break; case 3: printf("Wednesday"); break; case 4: printf("Thursday"); break; case 5: printf("Friday"); break; case 6: printf("Saturday"); break; case 7: printf("Sunday"); break; } return 0; }

Break keyword

When C reaches the keyword break, it will exit the switch block. This action will stop further code deployment and test cases inside the block. When a suitable case is found, it means the job is complete. Now is the time to rest, no need for further testing.

A break can save a lot of implementation time because it skips execution of all the remaining code in the switch block.

Default keyword

Keywords defaultspecify some code to run if there is no matching case. For example:

#include int main() { int day = 4; switch (day) { case 6: printf("Today is Saturday"); break; case 7: printf("Today is Sunday"); break; default: printf("Looking forward to the Weekend"); } return 0; }

Note: The keyword defaultmust be used as the last command in switchand it does not require a break.

Above are the things you need to know about Switch in C. Hope the article is useful to you.

4 ★ | 2 Vote

May be interested

  • Boolean in CPhoto of Boolean in C
    boolean, also known as bool in c, is an element that you must know when using this programming language. here's everything you need to know about boolean in c.
  • If ... Else in CPhoto of If ... Else in C
    what is the if … else condition in c? what is the meaning and usage of if else statement in c? let's find out with tipsmake.com.com!
  • How to Compile C Programs with GNU (GCC)Photo of How to Compile C Programs with GNU (GCC)
    tipsmake today will teach you how to compile a c program from source code using gnu (full name is gnu compiler collection, abbreviated gcc) - compiler for linux and minimalist gnu (mingw) on windows.
  • How to Design and Implement a Field Using C SharpPhoto of How to Design and Implement a Field Using C Sharp
    the first step in designing a class usually is to define its fields. fields are the backbone that classes rely upon, and so, the process of designing and implementing fields is crucial to the overall process of designing classes. this...
  • How to Make Your Windows Forms Application Run at Windows Startup Using CPhoto of How to Make Your Windows Forms Application Run at Windows Startup Using C
    sometimes you need to run your windows forms application when the user log in to windows. many applications that use this behavior are windows live messenger and yahoo messenger. in this article you will learn how to make your application...
  • How to Set Up an OpenGL SDL GLEW Template Project in Visual StudioPhoto of How to Set Up an OpenGL SDL GLEW Template Project in Visual Studio
    many programmers prefer opengl for graphics. if you are one of them, you are strongly advised by its producer, to use a window toolkit (such as sdl) and an opengl loading libraries (such as glew). this guide will help you get over the...