โ Comprehensive Analysis of Control Flow in C Language: The if Statement
๐ 1. Single Branch if
๐ Executes if the condition is true, otherwise skips.
#include <stdio.h>
int main() {
int age;
printf("Please enter your age:");
scanf("%d", &age);
if (age >= 18) {
printf("Adult, can act independently!\n");
}
return 0;
}
๐ 2. if…else (Two-way Structure)
#include <stdio.h>
int main() {
int num;
printf("Please enter an integer:");
scanf("%d", &num);
if (num % 2 == 0) {
printf("Even\n");
} else {
printf("Odd\n");
}
return 0;
}
๐ 3. if…else if…else (Multi-branch Structure)
#include <stdio.h>
int main() {
float score;
printf("Please enter your score:");
scanf("%f", &score);
if (score >= 90) {
printf("Excellent (A)\n");
} else if (score >= 80) {
printf("Good (B)\n");
} else if (score >= 70) {
printf("Average (C)\n");
} else if (score >= 60) {
printf("Pass (D)\n");
} else {
printf("Fail (F)\n");
}
return 0;
}
๐ 4. Nested if (Condition within a Condition)
#include <stdio.h>
int main() {
int year;
printf("Please enter a year:");
scanf("%d", &year);
if (year % 4 == 0) {
if (year % 100 != 0) {
printf("Leap year\n");
} else {
if (year % 400 == 0)
printf("Leap year\n");
else
printf("Common year\n");
}
} else {
printf("Common year\n");
}
return 0;
}
โ ๏ธ Recommended Writing (More Concise):
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
โ 5. Comprehensive Example: Multi-condition Judgment
๐ Input height and weight to determine if the weight is standard
- Standard weight โ height in cm – 110
- Allowable error ยฑ5kg
#include <stdio.h>
int main() {
int height, weight, standard;
printf("Please enter height (cm) and weight (kg):");
scanf("%d %d", &height, &weight);
standard = height - 110;
if (weight > standard + 5) {
printf("Overweight\n");
} else if (weight < standard - 5) {
printf("Underweight\n");
} else {
printf("Standard weight\n");
}
return 0;
}
๐ง 6. Common Errors Summary
| Error Writing | Reason |
|---|---|
<span>if(x = 5)</span> |
<span>=</span> is assignment, should use <span>==</span> |
<span>if x > 0</span> |
Condition must be enclosed in parentheses <span>()</span> |
Multiple statements without <span>{}</span> |
May cause logical errors |
| Confused indentation | Poor readability, prone to errors |
โ
Recommendation: Always write <span>{}</span> to cultivate standardized thinking
๐งช 7. Classroom Exercises
๐ Exercise 1: Determine if Positive
#include <stdio.h>
int main() {
int n;
printf("Please enter an integer:");
scanf("%d", &n);
if (n > 0)
printf("Positive\n");
else
printf("Non-positive\n");
return 0;
}
๐ Exercise 2: Highest Score Judgment (Comparison of Two)
#include <stdio.h>
int main() {
float a, b;
printf("Please enter two scores:");
scanf("%f %f", &a, &b);
if (a > b)
printf("First is higher: %.2f\n", a);
else
printf("Second is higher: %.2f\n", b);
return 0;
}
๐ Exercise 3: Triangle Validity Judgment
The sum of any two sides must be greater than the third side ๐ to form a triangle
#include <stdio.h>
int main() {
int a, b, c;
printf("Please enter the lengths of three sides:");
scanf("%d %d %d", &a, &b, &c);
if (a + b > c && a + c > b && b + c > a)
printf("Can form a triangle\n");
else
printf("Cannot form a triangle\n");
return 0;
}
๐ Next Lesson Preview
C Language Multi-branch Structure โโ Comprehensive Analysis of switch Statement is particularly useful for menu selection and classification judgments โ