Can the case after the switch statement in C language be a relational expression?

hzc
Release: 2020-07-03 16:29:45
Original
6644 people have browsed it

The case of the switch statement in C language cannot be followed by a relational expression. The case is followed by a constant expression. The true and false value judgment of the CASE expression will be terminated, and the remaining WHEN clause will be neglect.

Can the case after the switch statement in C language be a relational expression?

#No, the case is followed by a constant expression.

Pay attention to whether the data types returned by each branch in the CASE expression are consistent. It is incorrect to write a branch that returns a character type and another branch that returns a numeric type.

When using CASE expressions, the most common grammatical error is forgetting to write END. Although if you forget to write, the program will return an error message that is easier to understand, and it is not a fatal error. Most of the errors during execution are caused by this reason.

Can the case after the switch statement in C language be a relational expression?

The execution result of the CASE expression is NULL . However, not writing it may cause the trouble of "the syntax is correct, but the result is wrong", which is not easy to trace the cause, so write an ELSE clause (even when the result can be NULL).

Extended information

1. The true and false value judgment of the CASE expression will be terminated, and the remaining WHEN clauses will be ignored. In order to avoid unnecessary confusion, pay attention to the exclusivity of the condition when using the WHEN clause.

2. When the value of the expression after the case is equal to the value of the switch expression, the statement after the corresponding case will be executed, and then no judgment will be made, and all subsequent statements after the case will continue to be executed. If the value of the expression is different from the constant expressions after all cases, the statement after default will be executed.

Recommended tutorial: "c Language Tutorial"

The above is the detailed content of Can the case after the switch statement in C language be a relational expression?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template