0% found this document useful (0 votes)
4 views

C.Module 02

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

C.Module 02

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Arithmetic operators are used to perform basic mathematical operations.

• Addition (+): Adds two operands.

int a = 5, b = 3;
int sum = a + b; // sum is 8

• Subtraction (-): Subtracts the second operand from the first.

int a = 5, b = 3;
int difference = a - b; // difference is 2

• Multiplication (*): Multiplies two operands.

int a = 5, b = 3;
int product = a * b; // product is 15

• Division (/): Divides the numerator by the denominator.

int a = 6, b = 3;
int quotient = a / b; // quotient is 2

• Modulus (%): Returns the remainder of a division.

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).

• Equal to (==): Checks if two operands are equal.

int a = 5, b = 3;
if (a == b) {
// This block will not execute because a is not equal to b.
}

• Not equal to (!=): Checks if two operands are not equal.

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)) {

Examples of Logical Operators in Use

• Logical AND (&&):

int x = 10, y = 20;


if (x > 5 && y < 25) {

• Logical OR (||):

int x = 10, y = 5;
if (x > 15 || y < 10) {

• Logical NOT (!):

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.

Syntax of the Conditional (Ternary) Operator


condition ? expression1 : expression2;

• condition: An expression that is evaluated. If the condition is true (non-zero), then


expression1 is evaluated and returned.
• If the condition is false (zero), then expression2 is evaluated and returned.

Examples

1. Checking if a Number is Even or Odd:

int num = 4;
char *result;

// Using the conditional operator to determine if the number is even or


odd
result = (num % 2 == 0) ? "Even" : "Odd";
printf("%d is %s.\n", num, result); // Output: 4 is Even.

2. Finding the Minimum of Two Numbers:

int x = 7, y = 3;
int min;

// Using the conditional operator to find the minimum of two numbers


min = (x < y) ? x : y;
printf("The minimum of %d and %d is %d.\n", x, y, min); // Output: The
minimum of 7 and 3 is 3.

Benefits of Using the Conditional Operator

• Conciseness: Reduces the amount of code needed compared to an if-else statement.


• Readability: For simple conditions, it can make the code more readable by keeping
everything in a single line.

When to Use the Conditional Operator

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:

int num = 10;


if (num > 0) {
printf("The number is positive.\n");
}

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:

int num = -5;


if (num > 0) {
printf("The number is positive.\n");
} else {
printf("The number is not positive.\n");
}
3. if-else if Ladder

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

• if: Executes a block of code if a condition is true.


• if-else: Provides an alternative block of code if the condition is false.
• if-else if Ladder: Allows checking multiple conditions in sequence.
• Nested if-else: Enables more complex decision-making by nesting if-else statements
within each other.

You might also like