Thursday, January 16, 2025
HomeProgrammingSyntax of switch statement in C?

Syntax of switch statement in C?

The switch statement in C is used to execute one of many code blocks based on the value of an expression. The basic syntax is:

c
switch (expression) {
case value1:
// Code to be executed if expression == value1
break;
case value2:
// Code to be executed if expression == value2
break;
default:
// Code to be executed if expression doesn't match any case
}
  • expression: The value to be evaluated.
  • case value: Each possible value the expression might take.
  • break: Exits the switch statement (optional, but often used).
  • default: The default case, executed if no case matches.
See also  How can I set a default value for an HTML element?

The switch statement helps simplify multiple if-else conditions.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x