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
- How to play Airship map in Among Us on Switchan upcoming airship map will be added to the game among us in early 2021, but if you are playing the version among us on the recently released nintendo switch already have access to this map. here's how to play the toppat airship map in among us on the nintendo switch.
- How to fix Joy-Con error on Nintendo Switchwe show you how to fix one of the most annoying problems to afflict the nintendo switch and switch lite -- even nintendo knows it's an issue.
- Distinguish between switches, routers and modemsthe switch, router and modem look very similar if you look only at the appearance. however, each of these types plays a role and performs different purposes within a network. so what is the main difference between a switch, router and modem?
- 6 reasons why many people regret buying Nintendo Switchwhen they first bought a nintendo switch, many people were excited about new releases like animal crossing and didn't own any other console. but unfortunately, a lot of switch consoles are currently sitting on shelves collecting dust.
- Official: Nintendo Switch can run Androida question that has been in the minds of many tech-savvy people for years: nintendo switch can run android?
- Check out the 7 best quality Red Switch mechanical keyboard models in 2023top 7 most beautiful and durable red switch mechanical keyboards today 1. red switch dareu ek1280s pink white mechanical keyboard 2. e-dra ek387l red switch mechanical keyboard...
- Google instructs iOS users to switch to Android 5.0not long after apple launched a guide for android users to transfer data to ios, it was google's turn to draw a path for ios users to switch to their latest platform.
- Which mechanical keyboard switch should I buy? How to choose a switch for each usereach type of switch on a mechanical keyboard gives us different experiences. depending on the needs, each person has their own choices. but for those who are new to mechanical keyboards, which type of switch should they buy?
- 9 tips and tricks for the Nintendo Switchshare screenshots, manage storage and everything else you need to know about your switch portable gaming device.
- The best Nintendo Switch games of 2021the best nintendo switch games. nintendo switch is a game console loved by many young people. check out the best nintendo switch games!