In C language, conditional judgment statements such as <span>if</span>, <span>else if</span>, <span>else</span>, and <span>switch-case</span> allow different code paths to be executed based on various conditions. This is the foundation for writing logical control programs.
Before diving into the main content, let’s first review the definition and structure of functions in C language.
1. Function Definition in C Language
Basic Syntax:
ReturnType FunctionName(ParameterList) {
// Function body
return ReturnValue;
}
Example:
int add(int a, int b) {
return a + b;
}
A function is a set of statements that can be called repeatedly, taking inputs through parameters and returning results via <span>return</span>.
2. if, else Statements (Basic Branch Structure)
2.1 Basic Syntax
if (condition) {
// Execute when condition is true
} else {
// Execute when condition is false
}
2.2 Example
int x = 10;
if (x > 0) {
printf("x is positive\n");
} else {
printf("x is non-positive\n");
}
3. if, else if Multiple Branch Structure
3.1 Basic Syntax
if (condition1) {
// Condition1 is true
} else if (condition2) {
// Condition2 is true
} else {
// Other cases
}
- • It will evaluate from top to bottom, executing the corresponding code block as soon as a condition is met;
- •
<span>else</span>can be omitted, and<span>else if</span>can have multiple instances.
3.2 Example
int score = 85;
if (score >= 90) {
printf("A\n");
} else if (score >= 80) {
printf("B\n");
} else if (score >= 70) {
printf("C\n");
} else {
printf("Fail\n");
}
4. switch-case Multi-value Matching Structure
4.1 Basic Syntax
switch (expression) {
case value1:
// Execute when value1 matches
break;
case value2:
// Execute when value2 matches
break;
...
default:
// Execute when no case matches
break;
}
- •
<span>switch</span>is suitable for discrete value judgments (such as integers, characters); - • Each
<span>case</span>must be followed by<span>break</span>, otherwise it will fall through; - •
<span>default</span>is optional.
4.2 Example
int day = 3;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
default:
printf("Unknown day\n");
break;
}
5. Differences and Recommendations between if and switch
| Comparison Point | if/else if/else | switch/case |
| Applicable Scenarios | Range judgment (e.g., x > 0 && x < 100) | Discrete constant matching (e.g., 1, 2, 3…) |
| Expression Type | Any type with relational operations | Only integers/characters (C99 supports enum) |
| Readability | Difficult to read when conditions are complex | Concise and clear, suitable for menu-like selection structures |
| Performance Differences | Compiler optimizes automatically (no significant difference) | Some compilers optimize switch better |
6. Nested Usage Example (Comprehensive)
void checkValue(int x) {
if (x > 0) {
if (x % 2 == 0) {
printf("Positive Even\n");
} else {
printf("Positive Odd\n");
}
} else if (x == 0) {
printf("Zero\n");
} else {
printf("Negative\n");
}
}
7. Advanced Usage of switch: Merging Multiple Cases
char grade = 'B';
switch (grade) {
case 'A':
case 'B':
case 'C':
printf("Pass\n");
break;
case 'D':
case 'F':
printf("Fail\n");
break;
default:
printf("Invalid grade\n");
}
8. Traps and Precautions
| Issue | Example | Correct Approach |
Forgot <span>break</span> |
<span>case 1: ... case 2: ...</span> |
Add <span>break</span> after each <span>case</span> |
| Multiple if conditions are true simultaneously | <span>if (x > 0) else if (x > -1)</span> |
Order logically or use an <span>else if</span> chain |
<span>switch</span> does not support range judgment |
<span>case x > 10:</span> is illegal |
Use <span>if (x > 10)</span> instead |
Incorrect brackets <span>{}</span> |
<span>if (x > 0) printf(); else ...</span> |
It is recommended to develop good indentation + bracket habits |
9. Summary Comparison Table
| Syntax Structure | Usage Description | Supports Multiple Branches | Supports Range Judgment |
<span>if ... else</span> |
General syntax for conditional judgment | Yes | Yes |
<span>else if</span> |
Multi-condition branch judgment | Yes | Yes |
<span>switch-case</span> |
Discrete constant branch judgment | Yes | No |
10. Conclusion
- •
<span>if/else</span>is the most basic and flexible form of conditional control; - •
<span>switch</span>is clearer and more efficient in multi-value branch scenarios (such as menus, state handlers); - • Choosing the right control structure can make the code logical and maintainable;
- • When writing conditional statements, good indentation, adding brackets, and clear logical order are key programming habits.