If else in C


# Understanding Relational and Logical Operators in C

When programming in C, understanding relational and logical operators is crucial for controlling the flow of your code. These operators are used to compare values and combine multiple conditions, enabling you to make decisions based on complex criteria. In this blog post, we'll dive into these operators, explaining their usage with example code to illustrate how they work.

## Relational Operators

Relational operators in C are used to compare two values. The result of these comparisons is a boolean value: either true (non-zero) or false (zero). Here are the common relational operators in C:

1. **Equal to (`==`)**: Checks if two values are equal.
2. **Not equal to (`!=`)**: Checks if two values are not equal.
3. **Greater than (`>`)**: Checks if the left value is greater than the right value.
4. **Less than (`<`)**: Checks if the left value is less than the right value.
5. **Greater than or equal to (`>=`)**: Checks if the left value is greater than or equal to the right value.
6. **Less than or equal to (`<=`)**: Checks if the left value is less than or equal to the right value.

### Example Code

```c
#include <stdio.h>

int main() {
    int a = 5;
    int b = 10;

    // Equal to
    if (a == b) {
        printf("a is equal to b\n");
    } else {
        printf("a is not equal to b\n");
    }

    // Not equal to
    if (a != b) {
        printf("a is not equal to b\n");
    }

    // Greater than
    if (a > b) {
        printf("a is greater than b\n");
    } else {
        printf("a is not greater than b\n");
    }

    // Less than
    if (a < b) {
        printf("a is less than b\n");
    }

    // Greater than or equal to
    if (a >= b) {
        printf("a is greater than or equal to b\n");
    } else {
        printf("a is not greater than or equal to b\n");
    }

    // Less than or equal to
    if (a <= b) {
        printf("a is less than or equal to b\n");
    }

    return 0;
}
```

### Output

```
a is not equal to b
a is not equal to b
a is not greater than b
a is less than b
a is not greater than or equal to b
a is less than or equal to b
```

## Logical Operators

Logical operators are used to combine multiple conditions. These operators are essential in constructing complex decision-making expressions. Here are the common logical operators in C:

1. **Logical AND (`&&`)**: Returns true if both conditions are true.
2. **Logical OR (`||`)**: Returns true if at least one condition is true.
3. **Logical NOT (`!`)**: Returns true if the condition is false.

### Example Code

```c
#include <stdio.h>

int main() {
    int x = 5;
    int y = 10;
    int z = 15;

    // Logical AND
    if (x < y && y < z) {
        printf("x is less than y AND y is less than z\n");
    }

    // Logical OR
    if (x > y || y < z) {
        printf("x is greater than y OR y is less than z\n");
    }

    // Logical NOT
    if (!(x == y)) {
        printf("x is NOT equal to y\n");
    }

    return 0;
}
```

### Output

```
x is less than y AND y is less than z
x is greater than y OR y is less than z
x is NOT equal to y
```

## Combining Relational and Logical Operators

Often, you'll need to combine multiple relational and logical operators to create complex conditions. Here’s an example demonstrating this:

```c
#include <stdio.h>

int main() {
    int a = 5;
    int b = 10;
    int c = 15;

    // Combining relational and logical operators
    if ((a < b && b < c) || (a == 5)) {
        printf("Either a is less than b and b is less than c, OR a is equal to 5\n");
    }

    if (!(a > c) && (b <= c)) {
        printf("a is NOT greater than c AND b is less than or equal to c\n");
    }

    return 0;
}
```

### Output

```
Either a is less than b and b is less than c, OR a is equal to 5
a is NOT greater than c AND b is less than or equal to c
```

## Conclusion



 Here are five coding assignments that focus on using `if-else` statements without involving loops:

### Assignment 1: Number Sign Checker

**Description**: Write a program that determines if a given number is positive, negative, or zero.

**Requirements**:
1. The program should prompt the user to enter a number.
2. Use `if-else` statements to check whether the number is positive, negative, or zero.
3. Display the result accordingly.

### Assignment 2: Even or Odd Checker

**Description**: Write a program that checks if a given number is even or odd.

**Requirements**:
1. The program should prompt the user to enter a number.
2. Use `if-else` statements to determine if the number is even or odd.
3. Display the result.

### Assignment 3: Vowel or Consonant Checker

**Description**: Write a program that checks if a given character is a vowel or consonant.

**Requirements**:
1. The program should prompt the user to enter a single character.
2. Use `if-else` statements to determine if the character is a vowel (a, e, i, o, u) or a consonant.
3. Display the result.
4. Assume the input is a lowercase letter.

### Assignment 4: Number Comparison

**Description**: Write a program that compares two numbers and prints the larger one, or if they are equal.

**Requirements**:
1. The program should prompt the user to enter two numbers.
2. Use `if-else` statements to compare the numbers.
3. Display the larger number, or if they are equal, print that they are equal.

### Assignment 5: Letter Grade Evaluator

**Description**: Write a program that evaluates a letter grade based on a numeric score.

**Requirements**:
1. The program should prompt the user to enter a score between 0 and 100.
2. Use `if-else` statements to determine the letter grade based on the score:
    - A: 90-100
    - B: 80-89
    - C: 70-79
    - D: 60-69
    - F: 0-59
3. Display the letter grade.
 

Contact us for software training, education or development










 

Post a Comment

0 Comments

Me