“From today on, study hard and make progress every day.”
Repetition is the best method for memory; spend one minute each day to remember the basics of C language.
“C Language Beginner’s Essential Knowledge Notes Series of 100 Articles”“
25. if Statement: The Most Common Conditional Judgment
1. Basic Structure of if Statement
The if statement is the most fundamental conditional control structure in C language, with the following syntax:
if (condition_expression) {
// Code block executed when condition is true
}
Execution Flow
- 1. Calculate the value of the condition expression
- 2. If the result is true (non-zero), execute the code block
- 3. If the result is false (zero), skip the code block
2. Simple Examples
1. Basic Usage
int age = 18;
if (age >= 18) {
printf("Adult\n");
}
2. Single Statement Short Form
if (x > 0) printf("Positive\n"); // Can omit {} when there is only one statement
3. if-else Structure
1. Complete Syntax
if (condition) {
// Execute if condition is true
} else {
// Execute if condition is false
}
2. Practical Application
int score = 75;
if (score >= 60) {
printf("Pass\n");
} else {
printf("Fail\n");
}
4. Multiple Conditional Judgments
1. else-if Ladder
if (condition1) {
// Condition1 is true
} else if (condition2) {
// Condition2 is true
} else {
// All conditions are false
}
2. Grade Level 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("D\n");
}
5. Detailed Explanation of Condition Expressions
1. Common Condition Forms
if (a == b) // Equality check
if (a != 0) // Inequality check
if (ptr) // Non-null pointer check
if (flag) // Boolean value check
2. Compound Conditions
if (age >= 18 && age <= 60) // AND
if (score < 60 || score > 90) // OR
if (!is_holiday) // NOT
6. Common Errors
1. Confusion Between Assignment and Comparison
if (x = 5) { // Error! Assigning 5 to x
// ...
}
Correct approach:
if (x == 5) { // Correct comparison
// ...
}
2. Floating Point Comparison
if (f == 0.3) { // Dangerous! Floating point precision issue
// ...
}
Correct:
#include <math.h>
if (fabs(f - 0.3) < 1e-6) { // Allowable error
// ...
}</math.h>
7. Advanced Usage Techniques
1. Simplifying Multiple Conditions
if (10 <= x && x <= 20) // Mathematical range check
2. Function Return Value Check
#include <string.h>
if (strcmp(s1, s2) == 0) // String equality</string.h>
3. Initialization Check
FILE *fp;
if ((fp = fopen("file.txt", "r")) == NULL) {
// Error handling
}
4. Common Condition Precedence
// High-frequency conditions placed first
if (likely_case) {
// ...
} else {
// ...
}
Some students contacted me, wanting to have a study exchange group. Previously, I hesitated to create one due to concerns about advertisements, but I think having a group would indeed be convenient, so I will create one this time to try it out.
If you need it, hurry up and join; the validity period is 7 days.

———- End ———-
[Special Statement: All articles in this public account are original or authorized by the author, please feel free to use them; the views are for learning reference only~~]


“If you like C, please like it”
Click the bottom right corner to view
“