switch…case in C (Switch Statement in C) with Examples
The value of a variable is examined by the switch statement in C, and the results are compared with a number of other scenarios. After the case match has been located, the block of statements that is related with that particular case will be carried out.
The term “identifier” refers to the unique name or number that is assigned to each case that is contained within a block of a switch. The value that was supplied by the user is compared with each of the possible outcomes contained within the switch block until a suitable match is discovered.
In the event that a case match is not discovered, the default statement will be carried out, and the control will then exit the switch block.
Switch Case Syntax
Syntax With a Switch Case
The following is an example of a general syntax for how switch-case is implemented in a programme written in C:
substitute ( expression )
{ case value-1: Block-1; Break; case value-2: Block-2; Break; case value-n: Block-n; Break; default: Block-1; Break; }
Statement-x;
Either a character expression or an integer expression can be used for this expression.
The values 1, 2, and n serve as case labels and are utilised in the process of individually identifying each case. Keep in mind that case labels should not be the same because doing so could cause an issue with the execution of a programme. Let’s say we have two different situations, but they both have the label “1.” The case that appears first during the execution of the programme will be carried out, even if you wish the programme to carry out a different case from the one that shows first. This causes issues within the programme and does not produce the outcomes that were intended.
Case identifiers are always terminated with a colon ( : ). There is a block connected to each of these individual cases.
A block is composed of many statements that are grouped together for the purpose of a specific scenario.
The value of the test expression is compared with each of the cases that we have defined within the switch each time the switch is executed, and the results are displayed. Suppose the test phrase includes value 4. This value is compared with each of the cases in the programme, up until the point where the case with the label four is discovered. Control is transferred away from the switch once a case has been located and the block of statements that corresponds to that case has been carried out.
The case that has been reached its conclusion when the break keyword is used is indicated. If we do not include the break statement in each instance, then the switch in C will continue to run through all of the cases until it reaches the end of the list, even if one of the cases has already been executed. Because we do not want this to occur, the break keyword must be included in each and every one of the cases. Once it is put into action, Break will cause the case to be terminated, and the control will be removed from the switch.
Switch Statement Flowchart
There is no requirement to use the default case. In situations in which the value of test-expression does not correspond to any of the instances included within the switch, the default action will be carried out. If that is not the case, then writing default within the switch is not required.
The programme will continue to be run after the switch instruction has been carried out, at which point control will be passed on to statement-x.
Statement of a Switch Flowchart
The switch case selection process is depicted in the following diagram:
A Flowchart for Switch Statements
The Operation of a Switch
An Example of a Switch Case in C
The following software serves as an example of the switch’s functionality:
Switch Case Example in C
#include “stdio.h” in your code.
in the main () if the value of int num is eight, then switch (num) case 7: printf(“Value is 7”); break; case 8: printf(“Value is 8”); break; case 9: printf(“Value is 9”); break; case default: printf(“Out of range”); break; case default: return 0; case default: return 0; case default: return 0; case default: return 0; case default: return 0; case default: return 0; case default: return 0; case default
Output:
Value is 8
A Flowchart for Switch Statements
In the programme that was provided to us, we have explained how to give the variable num the value 8.
A switch construct is used to compare the value that is stored in the variable num, and the block of statements that is connected with the case that is matched is then executed.
Because the value that is now being held in the variable num is 8, a switch in this programme will go to the case whose case-label value is 8. Following the completion of the case, the control will be released from the switch, and the programme will end with a successful result by writing the value onto the output screen.
You may see what happens to the output by experimenting with different values for the variable num.
As an illustration, let’s have a look at the following application and its defaults:
#include “stdio.h” in your code.
in the main () ; int language equals 10; toggle (language) case 1: printf(“C#n”); break; case 2: printf(“Cn”); break; case 3: printf(“C++n”); break; default: printf(“Other programming languagen”); case 1: printf(“C#n”); break; case 2: printf(“Cn”); break; case 3: printf(“C++n”); break; case
Output:
Different programming language also available
In C, working with switch cases requires you to combine many cases together and identify them individually. If you want to branch off at the end of a switch statement, you have to include a break statement in each individual case.
When there are no other cases that match, the default case, which is optional, is executed.
The following switch statement is what we look at:
#include “stdio.h” in your code.
in the main () if (int number = 5) then switch (number) case 1: case 2: case 3: printf(“One, Two, or Three.n”); break; case 4: case 5: case 6: printf(“Four, Five, or Six.n”); break; default: printf(“Greater than Six.n”); Output: case 1: case 2: case 3: printf(“Four, Five, or Six.n”); break; case 4: case 5: case 6: printf(“Four, Five, or
Four, five, or six is an option.
Nested Switch in the Language C
In the programming language C, it is possible to have a switch within a switch. Additionally, it is possible for the case constants of the inner switch and the outer switch to have the same values and not have any conflicts as a result.
We will consider the following programme in which the user will be asked to type his own ID, and if the ID is valid, the programme will ask the user to enter his password. If the password is correct, the programme will print the name of the user; if the password is incorrect, the programme will print Incorrect Password, and if the ID does not exist, the programme will print Incorrect ID.
Nested Switch in C
#include “stdio.h” in your code.
in the main () printf(“Please Enter Your ID:n”); scanf(” percent d”, & ID); switch int ID = 500; int password = 000; (ID) if case 500: printf(“Enter your password:n “); scanf(” percent d”, & password); switch (password) case 000: printf(“Welcome Dear Programmern”); break; default: printf(“incorrect password”); break; break; default: printf(“incorrect ID”); break; OUTPUT: case 000: printf(“Welcome Dear Programmern”); break; default: printf(“incorrect password”); break; case 000: break; default: printf
Plese Enter Your ID: 500
Please enter your password, which is 000.
Welcome Dear Programmer
Nested Switch in the Language C
Within the scope of the presented programme, we have analysed and discussed how to initialise two variables: Username and passphrase
The value that is inserted into variable ID is compared with the help of an outer switch construct. When a case is matched, such as when ID==500, it executes the block of statements associated with that case.
An inner switch is used to check the values entered in the variable password and execute the statements linked with the matched case (when password==000). This happens if the block statement is executed with the matched case.
In any other circumstance, the switch case will cause the default case to be triggered, which will result in the proper text regarding the programme outline being printed.
Why do we need a Switch case?
There is a possibility of an issue with the if-else statement, and that possibility is that the complexity of the programme will increase whenever there is an increase in the number of alternate paths. It is possible that the programme will become more challenging to read and understand if several if-then constructs are used throughout the code. Sometimes it can be so confusing that it even throws off the programmer who built the programme.
The switch statement is the answer to this conundrum that we have here.
Rules for switch statement
Norms applicable to the switch statement
Any time an expression is carried out, it must produce a result.
Case labels must be constants and unique.
It is required that case labels conclude with a colon ( : ).
Every single scenario requires the presence of a break keyword.
There can only be one label set as the default.
We are able to nest numerous switch statements within one another.
Leave a Reply