answersLogoWhite

0

What is switch case?

Updated: 8/11/2023
User Avatar

Wiki User

11y ago

Best Answer

switch (expression)

{

case constant-value1:

statements

break [optional];

case constant-value2:

statements

break [optional];

.

.

.

default:

statements;

}

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

The switch statement is a form of flow control, often used to replace repetitive if-else blocks. It takes a single integral value and performs a series of comparisons against programmer-supplied values. As soon as one of the values matches, execution of supplied code begins.

As an example, let's look at a common use for switch blocks: testing a char (an integral value) to see what type of value it contains. We'll test for consonants, vowels, and mathematical operators.

char ch;

// if-else solution

if(ch '*') {

printf("operator\n");

}else if(ch 'u') {

printf("vowel\n");

}else {

printf("consonant\n");

}

// switch solution

switch(ch) {

// Note the syntax of the case lines - case value colon

case '+':

case '-':

case '/':

case '*':

printf("operator\n");

break

// Also note the break statement. Without it, any operator matches

// would "fall through" to the next printf statement.

case 'a':

case 'e':

case 'i':

case 'o':

case 'u:

printf("vowel\n");

break;

// The default keyword specifies what action should be taken if no

// matches have been made.

default:

printf("consonant\n");

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Switch case is a condition statement used for performing specific tasks based on specific value of the expression. The general syntax of switch is:

switch(expression){

case value1: statements; break;

case value2: statements; break;

.

.

case valueN: statements; break;

default: statements;

}


The break statements are essential after every statement, otherwise all the other cases starting from the matched case will be executed till break is encountered. The default statement is executed when none of the cases match.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

we use case statement in c for a particulAR reson case is also work as a switch ststement

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Keywords.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is switch case?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering
Related questions

What are the similarities between if else and switch case?

If else and switch case both are used to control the flow of program.


Where is the transfer case switch on a 1997 dodge 4x4?

The vacuum switch is on the top of the transfer case. The 4wd indicator switch is on the front axle shift actuator.


What is the use of the default case in a switch case?

The default case in a switch statement will be activated if none of the other case values match. It is used exactly for this purpose - if nothing else matches in the switch then this one will always match.Without a default case value, if nothing matched in the switch then nothing will be done. Sometimes it is necessary to know that nothing matched.


What are the release dates for Switch - 1975 The Case of the Purloined Case 1-21?

Switch - 1975 The Case of the Purloined Case 1-21 was released on: USA: 2 March 1976


Where is the vacuum switch on the front axle located?

On a Dodge truck, the vacuum switch is on the transfer case.On a Dodge truck, the vacuum switch is on the transfer case.


Where in the circuit do you insert the switch?

i assume main switch in that case at the mains input.


What is case select in c language?

It is switch-case. Example: switch (opsign) { case '+': . result = a + b; . break; case '-': . result = a - b; . break; ... default: . printf ("opsign=%c unknown\n", opsign); }


How do you write coding php using switch case?

The case structure in PHP uses the following syntax: switch($foo){ case 'bar': doSomething(); break; case 'blah': doSomethingElse(); break; default: handleOtherCases(); }


What is the function of visual c plus plus switch condition?

The switch / case statement.


Where is the neutral switch located on a 1994 ford escort?

the switch is located in or on the transmission case


What should you do in case your wall switch gets warm?

increase the amperage of the switch. if it is a 15 amp switch, change it to a 20 amp switch. you also can reduce the load on the switch, which will relieve the heating. in either case, do it NOW, that switch can start a fire at any time. be prepared to pay 5 to7.00 for a 20 amp switch.


Which comma is used for characters in switch case statements?

There is only one comma, but it is not used in switch-case. Character literals are between apostrophes: 'x'