• admin@embedclogic.com

switch-case In C

Like an if-else method, switch(expression selection), case(constant expression label)and break(end processing of a particular case) use together to control the statement execution on the basis of expression selection.

expression output must be an integral value.

syntax

Points to remember:

  1. The expression inside switch must be character or integer, Real or float values are not allowed.
  2. The expression inside switch decides the execution of label.If the expression doesn’t match with any of constant expression then default label will execute.
  3. break statement to end processing of a particular case within the switchstatement and to branch to the end of the switch statement.
  4. Without break, the program continues to the next case, executing the statements until a break or the end of the statement is reached.

Try below example program without break statement and see the behavior.

  1. Use of break and default label may desirable sometimes.
  2. The expression may be any constant or arithmetic or bitwise operation which results in an integer or character constants.

Try below example program with real value or float input expression and see the behavior.

Example:

Program to display weekly wake-up time[Take the week number from keyboard].

Subscribe and stay updated with our latest articles.