answersLogoWhite

0

What is a switch case?

User Avatar

Anonymous

12y ago
Updated: 8/20/2019

C has two types of conditional statements: if and switch.

An if statement executes a block of code if an expression results in a nonzero value. The code can be followed by an else statement under the condition that the previous if expression resulted in false. For example:

if (n==10) printf("There are 10 somethings.\n");

else if (n==15) printf("There are 15 somethings.\n");

else printf("Something else has occurred.\n");

Switch statements can be used to compare a variable or value with multiple variables or values. It's a simplified version of if and looks like this:

switch (n) {

case 10:

printf("There are 10 somethings.\n");

break;

case 15:

printf("There are 15 somethings.\n");

break;

default:

printf("Something else has occurred.\n");

}

Each of the above case statements is a switch case.

However, switch statements will not replace the following if statement:

if (n==10) printf("There are 10 somethings.\n");

else if (cars=20) printf("There are 20 cars.\n");

switch can only deal with one variable or expression at a time, but the above if statement can handle two (or more).

See the related links below for more information on switch statements.

User Avatar

Wiki User

12y ago

What else can I help you with?

Related Questions

What is BNF for switch case?

BNF, or Backus-Naur Form, is a notation used to express the grammar of programming languages. A switch-case structure can be represented in BNF as follows: <switch-statement> ::= "switch" "(" <expression> ")" "{" <case-clause>* <default-clause>? "}" <case-clause> ::= "case" <constant> ":" <statement>* <default-clause> ::= "default" ":" <statement>* This defines a switch statement consisting of an expression, multiple case clauses, and an optional default clause.


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 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.


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

the switch is located in or on the transmission case


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

The switch / case statement.