Saturday, January 11, 2025
HomeProgrammingSyntax of switch statement in C?

Syntax of switch statement in C?

In C, the switch statement allows branching based on the value of an expression. The syntax is as follows:

c
switch (expression) {
case value1:
// Code block for value1
break;
case value2:
// Code block for value2
break;
// More cases as needed
default:
// Code block if no case matches
}
  • expression is evaluated, and the control is transferred to the matching case.
  • The break statement exits the switch block after the case code runs.
  • The default case is optional and executes if no case matches.
See also  How to wrap text in CSS

Ensure break is used to avoid fall-through.

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