Today, we will explore an important concept that plays the role of a judge in programming—relational expressions.
1. Explanation of Knowledge Points
1. What is a Relational Expression?
In C language, a relational expression is an expression used to compare the relationship between two values. It consists of two operands (which can be variables, constants, or the results of other expressions) and a relational operator. The result of evaluating a relational expression is a logical value, which is typically represented as an integer in C language:
- True: a non-zero value, usually represented by
<span>1</span>. - False: a zero value, usually represented by
<span>0</span>.
You can think of it as a question, such as “Is A greater than B?” or “Is X equal to Y?” It will give you a clear “yes” or “no” answer.
C language provides the following six relational operators:
| Operator | Meaning | Example | Result (assuming a=5, b=3) |
|---|---|---|---|
<span><</span> |
Less than | <span>a < b</span> |
0 (False) |
<span><=</span> |
Less than or equal to | <span>a <= b</span> |
0 (False) |
<span>></span> |
Greater than | <span>a > b</span> |
1 (True) |
<span>>=</span> |
Greater than or equal to | <span>a >= b</span> |
1 (True) |
<span>==</span> |
Equal to | <span>a == b</span> |
0 (False) |
<span>!=</span> |
Not equal to | <span>a != b</span> |
1 (True) |
Note: Never confuse the assignment operator <span>=</span> with the relational operator <span>==</span>! <span>=</span> is used to assign a value to a variable, while <span>==</span> is used to check if two values are equal. This is a very common mistake among beginners, so remember it!
2. When to Use Relational Expressions?
Relational expressions are mainly used for conditional judgments in programs. When we need to decide the execution path of the program based on a certain condition, relational expressions come into play. They play a core role in control flow statements, such as:
<span>if</span>statement: Executes a specific block of code if the condition is true.if (score >= 60) { printf("Congratulations, you passed!\n"); }<span>while</span>loop: Repeats the block of code as long as the condition is true.while (count < 10) { // Perform some operations count++; }<span>for</span>loop: Relational expressions are often used in loop conditions.for (int i = 0; i < 5; i++) { // ... }
Why use them?
Because the real world is full of choices and changes. For a program to simulate reality, it must be able to respond differently based on different situations. Relational expressions are the foundation for achieving this “intelligent decision-making.” Without them, our programs can only execute in a fixed manner, unable to cope with complex and variable demands.
Connections and Differences with Related Knowledge Points:
- Arithmetic Expressions: Arithmetic expressions perform numerical calculations (e.g.,
<span>a + b</span>), resulting in a numerical value. Relational expressions perform comparisons, resulting in logical values (0 or 1). - Logical Expressions: Logical expressions are used to combine multiple relational expressions or other logical values, forming more complex conditions through logical operators (
<span>&&</span>,<span>||</span>,<span>!</span>). Relational expressions are the basic components of logical expressions. - Assignment Expressions: Assignment expressions are used to assign values to variables (e.g.,
<span>a = 10</span>), resulting in the assigned value. Relational expressions are used for comparisons, resulting in 0 or 1.
| Feature/Expression Type | Arithmetic Expression | Relational Expression | Logical Expression | Assignment Expression |
|---|---|---|---|---|
| Purpose | Numerical calculation | Comparison judgment | Combining conditions | Variable assignment |
| Operators | <span>+</span>, <span>-</span>, <span>*</span>, <span>/</span>, <span>%</span> |
<span><</span>, <span><=</span>, <span>></span>, <span>>=</span>, <span>==</span>, <span>!=</span> |
<span>&&</span>, <span>||</span>, <span>!</span> |
<span>=</span> |
| Result Type | Numerical | Logical value (0 or 1) | Logical value (0 or 1) | Assigned value |
| Typical Application | Calculating total score | Judging whether to pass | Judging whether multiple conditions are met | Updating variable values |
3. How to Use Relational Expressions?
Let’s look at a few simple code examples to see how relational expressions work in practice.
Scenario 1: Judging whether a number is positive
#include <stdio.h>
int main() {
int num;
printf("Please enter an integer:");
scanf("%d", &num);
// Use relational expression to judge if num is greater than 0
if (num > 0) {
printf("%d is a positive number.\n", num);
} else if (num == 0) { // Use relational expression to judge if num is equal to 0
printf("%d is zero.\n", num);
} else { // num < 0
printf("%d is a negative number.\n", num);
}
return 0;
}
In this example, <span>num > 0</span> and <span>num == 0</span> are relational expressions. They determine which statement the program will output based on the value of <span>num</span>.
Scenario 2: Checking if the user input password is correct
#include <stdio.h>
#include <string.h> // Use string comparison function strcmp
int main() {
char correctPassword[] = "123456";
char inputPassword[20];
printf("Please enter the password:");
scanf("%s", inputPassword);
// For string comparison, cannot use == directly, need to use strcmp function
// strcmp returns 0 if the two strings are equal
if (strcmp(inputPassword, correctPassword) == 0) {
printf("Password correct, welcome!\n");
} else {
printf("Password incorrect, please try again.\n");
}
return 0;
}
Here, <span>strcmp(inputPassword, correctPassword) == 0</span> is also a relational expression, but its left operand is the result of a function call. It checks whether the user input password matches the preset password.
Scenario 3: Loop until a certain condition is met
#include <stdio.h>
int main() {
int guess;
int target = 7; // Assume the number we want to guess is 7
printf("Guess a number between 1 and 10:\n");
// As long as the guessed number is not equal to the target number, keep looping
while (guess != target) { // Relational expression as loop condition
printf("Your guess is:");
scanf("%d", &guess);
if (guess < target) {
printf("Too small, guess bigger!\n");
} else if (guess > target) {
printf("Too big, guess smaller!\n");
}
}
printf("Congratulations, you guessed it! It is %d!\n", target);
return 0;
}
In this “guess the number” game, <span>guess != target</span> is the condition for the <span>while</span> loop. As long as this relational expression is true (i.e., the guessed number is not equal to the target number), the loop will continue.
2. Typical Exercises
1. Multiple Choice Question
Which of the following C language operators is used to determine if two values are equal?
- A.
<span>=</span> - B.
<span>==</span> - C.
<span>!=</span> - D.
<span>===</span>
2. True or False Question
In C language, the result of a relational expression can only be true (1) or false (0).
3. Fill in the Blanks
The result of the expression <span>5 > 3</span> is <span>____</span>. The result of the expression <span>10 <= 10</span> is <span>____</span>. The result of the expression <span>7 != 7</span> is <span>____</span>.
4. Programming Question (Beginner)
Write a C program that receives two integers <span>a</span> and <span>b</span> from the user, then judges and outputs whether <span>a</span> is greater than <span>b</span>.
5. Programming Question (Intermediate)
Write a C program that simulates a simple “age verification” system. The program requires the user to input their age, and if the age is between 18 and 65 (inclusive), it outputs “You are of working age.”; otherwise, it outputs “Your age does not meet the requirements.”.
3. Exercise Explanation
1. Multiple Choice Question
Which of the following C language operators is used to determine if two values are equal?
- A.
<span>=</span> - B.
<span>==</span> - C.
<span>!=</span> - D.
<span>===</span>
Answer: B
Explanation:
- A.
<span>=</span>is the assignment operator, used to assign the value on the right to the variable on the left. - B.
<span>==</span>is the relational operator, used to determine if two values are equal. - C.
<span>!=</span>is the relational operator, used to determine if two values are not equal. - D.
<span>===</span>does not exist in C language; it is the strict equality operator in other languages like JavaScript.
2. True or False Question
In C language, the result of a relational expression can only be true (1) or false (0).
Answer: False
Explanation:Although the result of a relational expression is usually 1 (true) or 0 (false), strictly speaking, any non-zero value in C language is considered “true,” while zero is considered “false.” Therefore, the result of a relational expression is non-zero value (true) or zero value (false). Typically, the “true” result of a relational expression is 1, but theoretically, it can also be other non-zero integers. For example, <span>int x = (5 > 3);</span> will result in <span>x</span> being <span>1</span>.
3. Fill in the Blanks
The result of the expression <span>5 > 3</span> is <span>1</span>. The result of the expression <span>10 <= 10</span> is <span>1</span>. The result of the expression <span>7 != 7</span> is <span>0</span>.
Explanation:
<span>5 > 3</span>: 5 is indeed greater than 3, so the result is true, which is<span>1</span>.<span>10 <= 10</span>: 10 is less than or equal to 10 (satisfying the equality condition), so the result is true, which is<span>1</span>.<span>7 != 7</span>: 7 is not equal to 7 is false, so the result is false, which is<span>0</span>.
4. Programming Question (Beginner)
Write a C program that receives two integers <span>a</span> and <span>b</span> from the user, then judges and outputs whether <span>a</span> is greater than <span>b</span>.
#include <stdio.h>
int main() {
int a, b;
printf("Please enter the first integer a:");
scanf("%d", &a);
printf("Please enter the second integer b:");
scanf("%d", &b);
// Use relational expression a > b to judge
if (a > b) {
printf("a (%d) is greater than b (%d).\n", a, b);
} else if (a < b) { // You can also add a less than judgment
printf("a (%d) is less than b (%d).\n", a, b);
} else { // Finally, a == b
printf("a (%d) is equal to b (%d).\n", a, b);
}
return 0;
}
Explanation:This question tests the basic <span>></span> relational operator and the <span>if-else if-else</span> structure. By using <span>scanf</span> to get user input, then directly using the relational expression <span>a > b</span> in the condition of the <span>if</span> statement. Depending on its truth value, different <span>printf</span> statements are executed. For the completeness of the program, <span>a < b</span> and <span>a == b</span> judgments are also added.
5. Programming Question (Intermediate)
Write a C program that simulates a simple “age verification” system. The program requires the user to input their age, and if the age is between 18 and 65 (inclusive), it outputs “You are of working age.”; otherwise, it outputs “Your age does not meet the requirements.”.
#include <stdio.h>
int main() {
int age;
printf("Please enter your age:");
scanf("%d", &age);
// Use two relational expressions combined with the logical AND operator &&
// age >= 18 checks if age is greater than or equal to 18
// age <= 65 checks if age is less than or equal to 65
// Only when both conditions are true, the entire expression is true
if (age >= 18 && age <= 65) {
printf("You are of working age.\n");
} else {
printf("Your age does not meet the requirements.\n");
}
return 0;
}
Explanation:This question tests not only relational expressions but also introduces the logical operator <span>&&</span> (logical AND). “Being between 18 and 65 years old” means that both conditions must be satisfied: age greater than or equal to 18 and age less than or equal to 65. Therefore, we use the two relational expressions <span>age >= 18</span> and <span>age <= 65</span> and connect them with <span>&&</span>. Only when <span>age >= 18</span> is true and <span>age <= 65</span> is also true, the condition of the entire <span>if</span> statement will be true, and the program will output “You are of working age.” This is a very practical example of combined condition judgment.
In this world, everything is a combination of “yes” and “no”. Those seemingly simple relational expressions are teaching us how to discern clearly, how to make decisive choices, and then, continue moving forward.