Operator & If-Else Statement
1. Arithmetic Operators
a) Addition (+)
Adds two values.
int a = 5, b = 3;
int result = a + b; // Result: 8
printf("a + b = %d\n", result); // Output: a + b = 8
b) Subtraction (-)
Subtracts one value from another.
int a = 10, b = 4;
int result = a - b; // Result: 6
printf("a - b = %d\n", result); // Output: a - b = 6
c) Multiplication (*)
Multiplies two values.
int a = 7, b = 3;
int result = a * b; // Result: 21
printf("a * b = %d\n", result); // Output: a * b = 21
d) Division (/)
Divides two values.
int a = 15, b = 5;
int result = a / b; // Result: 3
printf("a / b = %d\n", result); // Output: a / b = 3
e) Modulus (%)
Returns the remainder of a division.
int a = 10, b = 3;
int result = a % b; // Result: 1 (since 10 ÷ 3 = 3 remainder 1)
printf("a %% b = %d\n", result); // Output: a % b = 1
f) Increment (++)
Increases the value of a variable by 1.
int a = 5;
a++; // Result: 6
printf("a = %d\n", a); // Output: a = 6
g) Decrement (--)
Decreases the value of a variable by 1.
int a = 8;
a--; // Result: 7
printf("a = %d\n", a); // Output: a = 7
2. Logical Operators
a) Logical AND (&&)
Returns true
if both conditions are true.
int a = 5, b = 10;
if (a > 3 && b < 15) {
printf("Both are true!\n"); // Output: Both are true!
}
b) Logical OR (||)
Returns true
if at least one condition is true.
int a = 7, b = 12;
if (a > 10 || b < 5) {
printf("At least one is true!\n"); // Will not execute since both conditions are false.
}
c) Logical NOT (!)
Reverses the condition’s result.
int a = 10;
if (!(a < 5)) {
printf("a is not less than 5!\n"); // Output: a is not less than 5!
}
3. Comparison Operators
Comparison operators are used to compare two values and return a boolean (true
or false
).
a) == (Equal To)
Returns true
if both operands are equal.
int a = 5, b = 5;
if (a == b) {
printf("a and b are equal\n");
}
b) != (Not Equal To)
Returns true
if the two operands are not equal.
int a = 5, b = 10;
if (a != b) {
printf("a and b are different\n");
}
c) > (Greater Than)
Returns true
if the left operand is greater than the right.
int a = 10, b = 5;
if (a > b) {
printf("a is greater than b\n");
}
d) < (Less Than)
Returns true
if the left operand is less than the right.
int a = 5, b = 10;
if (a < b) {
printf("a is smaller than b\n");
}
e) >= (Greater Than or Equal To)
Returns true
if the left operand is greater than or equal to the right.
int a = 10, b = 10;
if (a >= b) {
printf("a is greater than or equal to b\n");
}
f) <= (Less Than or Equal To)
Returns true
if the left operand is less than or equal to the right.
int a = 5, b = 10;
if (a <= b) {
printf("a is smaller than or equal to b\n");
}
4. Assignment Operators
a) Simple Assignment (=)
Assigns a value to a variable.
int a;
a = 10; // a now holds 10
printf("a = %d\n", a); // Output: a = 10
b) Compound Assignment (+=, -=, *=, /=, %=)
Combines arithmetic operations with assignment.
int a = 5;
a += 3; // Equivalent to a = a + 3 → Result: 8
a -= 2; // Equivalent to a = a - 2 → Result: 6
a *= 4; // Equivalent to a = a * 4 → Result: 24
a /= 6; // Equivalent to a = a / 6 → Result: 4
a %= 3; // Equivalent to a = a % 3 → Result: 1
printf("Final result of a = %d\n", a); // Output: Final result of a = 1
5. If-Else Statement Structure
a) If
Executes code if the condition is true.
int number = 10;
if (number > 5) {
printf("Number is greater than 5!\n"); // Output: Number is greater than 5!
}
b) If-Else
Executes an alternative code block if the condition is false.
int number = 3;
if (number > 5) {
printf("Number is greater than 5!\n");
} else {
printf("Number is not greater than 5!\n"); // Output: Number is not greater than 5!
}
c) Else-If
Adds additional conditions.
int number = 7;
if (number < 5) {
printf("Number is less than 5!\n");
} else if (number == 5) {
printf("Number is equal to 5!\n");
} else {
printf("Number is greater than 5!\n"); // Output: Number is greater than 5!
}