In C programming, decision making allows you to execute certain blocks of code based on conditions. This is typically achieved using conditional statements like if, else, and switch. The condition used in these decision-making constructs is generally based on the relational and equality operators that compare values and return either true (non-zero) or false (zero).
Equality operators are used to compare two values for equality or inequality. They are commonly used in conditional statements to determine whether two expressions have the same value or not.
== (Equal to):
== operator checks if two values are equal.!= (Not equal to):
!= operator checks if two values are not equal.#include <stdio.h>
int main() {
int a = 5, b = 10;
if (a == b) {
printf("a and b are equal.\n");
} else {
printf("a and b are not equal.\n");
}
if (a != b) {
printf("a and b are not equal.\n");
} else {
printf("a and b are equal.\n");
}
return 0;
}
if statement checks whether a is equal to b. Since a is 5 and b is 10, the condition a == b evaluates to false, so the else block executes, printing "a and b are not equal.".if statement checks if a is not equal to b. Since a is 5 and b is 10, the condition a != b evaluates to true, and the program prints "a and b are not equal.".a and b are not equal.
a and b are not equal.
Relational operators are used to compare two values to determine their relationship (greater than, less than, greater than or equal to, or less than or equal to).
> (Greater than):
< (Less than):
>= (Greater than or equal to):
<= (Less than or equal to):
#include <stdio.h>
int main() {
int a = 5, b = 10;
if (a > b) {
printf("a is greater than b.\n");
} else {
printf("a is not greater than b.\n");
}
if (a < b) {
printf("a is less than b.\n");
} else {
printf("a is not less than b.\n");
}
if (a >= b) {
printf("a is greater than or equal to b.\n");
} else {
printf("a is less than b.\n");
}
if (a <= b) {
printf("a is less than or equal to b.\n");
} else {
printf("a is greater than b.\n");
}
return 0;
}
if statement checks if a is greater than b. Since a is 5 and b is 10, the condition a > b evaluates to false, so the program prints "a is not greater than b.".if statement checks if a is less than b. Since a is 5 and b is 10, the condition a < b evaluates to true, and the program prints "a is less than b.".if statements check whether a is greater than or equal to b, and whether a is less than or equal to b, respectively. Given a is 5 and b is 10, both of these conditions will evaluate to false for greater than or equal, and true for less than or equal.a is not greater than b.
a is less than b.
a is less than b.
a is less than or equal to b.
You can combine relational and equality operators to form more complex conditions in C. Often, you'll combine them with logical operators (&&, ||, !) to create compound conditions.
#include <stdio.h>
int main() {
int a = 5, b = 10, c = 15;
// Check if 'a' is less than 'b' and 'b' is less than 'c'
if (a < b && b < c) {
printf("a is less than b, and b is less than c.\n");
}
// Check if 'a' is equal to 'b' or 'b' is less than 'c'
if (a == b || b < c) {
printf("Either a is equal to b, or b is less than c.\n");
}
return 0;
}
if condition checks if a is less than b and b is less than c. Since a = 5, b = 10, and c = 15, this condition evaluates to true, and the program prints "a is less than b, and b is less than c.".if condition checks if a is equal to b or b is less than c. Since a != b but b < c, the condition evaluates to true, and the program prints "Either a is equal to b, or b is less than c.".a is less than b, and b is less than c.
Either a is equal to b, or b is less than c.
Equality Operators:
==: Checks if two values are equal.!=: Checks if two values are not equal.Relational Operators:
>: Greater than.<: Less than.>=: Greater than or equal to.<=: Less than or equal to.Decision Making: These operators are commonly used in conditional statements like if, else, and switch to decide which code block should be executed based on certain conditions.
By mastering equality and relational operators, you can create programs that make intelligent decisions based on conditions and data comparisons.
Open this section to load past papers