Computer Science, asked by snehaparamanik450, 5 months ago

when more than one case executes in switch case due to missing of break statement​

Answers

Answered by krishrocker7
1

Answer:

hope it helps took  a lot time too  write

Explanation:

The expression can be integer expression or a character expression.

Value-1, 2, n are case labels which are used to identify each case individually. Remember that case labels should not be same as it may create a problem while executing a program. Suppose we have two cases with the same label as '1'. Then while executing the program, the case that appears first will be executed even though you want the program to execute a second case. This creates problems in the program and does not provide the desired output.

Case labels always end with a colon ( : ). Each of these cases is associated with a block.

A block is nothing but multiple statements which are grouped for a particular case.

Whenever the switch is executed, the value of test-expression is compared with all the cases which we have defined inside the switch. Suppose the test expression contains value 4. This value is compared with all the cases until case whose label four is found in the program. As soon as a case is found the block of statements associated with that particular case is executed and control goes out of the switch.

The break keyword in each case indicates the end of a particular case. If we do not put the break in each case then even though the specific case is executed, the switch in C will continue to execute all the cases until the end is reached. This should not happen; hence we always have to put break keyword in each case. Break will terminate the case once it is executed and the control will fall out of the switch.

The default case is an optional one. Whenever the value of test-expression is not matched with any of the cases inside the switch, then the default will be executed. Otherwise, it is not necessary to write default in the switch.

Once the switch is executed the control will go to the statement-x, and the execution of a program will continue.

Similar questions