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.
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:
- The expression
switch
is evaluated once. - The value of the expression is compared with the value of each case.
- If there is a match, the relevant block of code will be executed.
- The command
break
exits the switch block and stops execution. - The command
default
is 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 default
specify 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 default
must be used as the last command in switch
and 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.
You should read it
- How to use the SWITCH function in Excel 2016
- How to add a passcode to Nintendo Switch
- How to capture screen and record video on Nintendo Switch game console
- Top 5 Best Ethernet Switch 2021
- Review of PoE 8 Port TP-Link Switch TL-SG1008P: Indispensable for home security
- How to update games on Nintendo Switch
- How to turn an old router into a switch
- How to play Airship map in Among Us on Switch
May be interested
- Boolean in Cboolean, 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 Cwhat 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)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 Sharpthe 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 Csometimes 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 Studiomany 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...