C.Module 02
C.Module 02
int a = 5, b = 3;
int sum = a + b; // sum is 8
int a = 5, b = 3;
int difference = a - b; // difference is 2
int a = 5, b = 3;
int product = a * b; // product is 15
int a = 6, b = 3;
int quotient = a / b; // quotient is 2
int a = 7, b = 3;
int remainder = a % b; // remainder is 1
• Increment (++): Increases the value of an operand by one. It can be used in prefix (++a) or
postfix (a++) form.
int a = 5;
a++; // a is now 6
• Decrement (--): Decreases the value of an operand by one. It can be used in prefix (--a) or
postfix (a--) form.
int a = 5;
a--; // a is now 4
Relational operators are used to compare two values or expressions. The result of a relational
operation is either true (non-zero) or false (zero).
int a = 5, b = 3;
if (a == b) {
// This block will not execute because a is not equal to b.
}
int a = 5, b = 3;
if (a != b) {
// This block will execute because a is not equal to b.
}
• Greater than (>): Checks if the left operand is greater than the right operand.
int a = 5, b = 3;
if (a > b) {
// This block will execute because a is greater than b.
}
• Less than (<): Checks if the left operand is less than the right operand.
int a = 5, b = 3;
if (a < b) {
// This block will not execute because a is not less than b.
}
• Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right
operand.
int a = 5, b = 3;
if (a >= b) {
// This block will execute because a is greater than b.
}
• Less than or equal to (<=): Checks if the left operand is less than or equal to the right operand.
int a = 5, b = 3;
if (a <= b) {
// This block will not execute because a is not less than or equal to b.
}
Logical operators are used to combine multiple relational or conditional expressions to determine
the overall logic of the expressions. They return either true (non-zero) or false (zero) based on the
evaluation.
1. Logical AND (&&): Returns true if both operands are true; otherwise, it returns false.
int a = 5, b = 3, c = 7;
if (a > b && c > a) {
2. Logical OR (||): Returns true if at least one of the operands is true; otherwise, false.
int a = 5, b = 3, c = 7;
if (a > b || c < a) {
3. Logical NOT (!): Reverses the logical state of its operand. If a condition is true, it
becomes false, and vice versa.
int a = 5, b = 3;
if (!(a < b)) {
• Logical OR (||):
int x = 10, y = 5;
if (x > 15 || y < 10) {
int x = 10;
if (!(x == 20)) {
}
The conditional operator (also known as the ternary operator) is a shorthand way of writing
simple if-else statements. The conditional operator evaluates a condition and returns one of
two values depending on whether the condition is true or false.
Examples
int num = 4;
char *result;
int x = 7, y = 3;
int min;
Use the conditional operator for simple conditions where the code's clarity is not compromised.
For more complex conditions or when additional statements are needed, it is better to use if-
else statements.
Conditional statements are used to perform different actions based on different conditions. These
statements allow us to control the flow of a program.
1. if Statement
The if statement is the most basic conditional statement in C. It executes a block of code if a
specified condition is true.
Syntax:
if (condition) {
// Code to execute if the condition is true
}
Example:
2. if-else Statement
The if-else statement provides an alternative block of code that executes if the condition is
false.
Syntax:
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
Example:
The if-else if ladder is used when you have multiple conditions to check. It evaluates each
condition in sequence. As soon as a true condition is found, the corresponding block of code is
executed, and the rest of the ladder is skipped.
Syntax:
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else if (condition3) {
// Code to execute if condition3 is true
}
// Add more else-if as needed
else {
// Code to execute if all conditions are false
}
Example:
int num = 0;
if (num > 0) {
printf("The number is positive.\n");
} else if (num < 0) {
printf("The number is negative.\n");
} else {
printf("The number is zero.\n");
}
4. Nested if-else
A nested if-else is an if-else statement inside another if or else block. This allows for
checking multiple conditions in a hierarchical manner.
Syntax:
if (condition1) {
// Code to execute if condition1 is true
if (condition2) {
// Code to execute if both condition1 and condition2 are true
} else {
// Code to execute if condition1 is true and condition2 is false
}
} else {
// Code to execute if condition1 is false
}
Example:
int a = 5, b = 10;
if (a != b) {
if (a > b) {
printf("a is greater than b.\n");
} else {
printf("a is less than b.\n");
}
} else {
printf("a is equal to b.\n");
}
Summary