# if–else, else if, Logical Operators & User Input

> <div data-node-type="callout">
> <div data-node-type="callout-emoji">💡</div>
> <div data-node-type="callout-text">I recently started teaching a course on C programming and as part of my preparation for class, I usually write out lesson notes that I will be teaching from. I have therefore decided to make these into blog posts so anyone else learning C can also benefit from them. Therefore, this lesson is part of the series I am calling: <strong>C Programming for Absolute Beginners. </strong>I hope you enjoy it. Ask any questions or leave comments if you need further clarification or want to make a suggestion.</div>
> </div>

In the [last lesson](https://blog.ehoneahobed.com/control-flow-and-the-if-statement-how-programs-make-decisions), your program learned how to make a simple decision using `if`.

But what if you want the program to do something **when the condition is false**?

Or what if you want it to check **multiple conditions**?

That’s what we’ll learn today.

By the end of this lesson, you’ll know how to:

*   Use `if–else`
    
*   Use `else if` for multiple decisions
    
*   Combine conditions using logical operators
    
*   Take user input using `scanf`
    

Let’s build on what you already know.

## 1\. The `if–else` Statement

An `if` statement only runs code when the condition is true.

But sometimes you want two possible outcomes:

*   One if true
    
*   One if false
    

That’s where `else` comes in.

### Syntax

```c
if (condition) {
    // runs if true
} else {
    // runs if false
}
```

## 2\. Example: Pass or Fail

```c
#include <stdio.h>

int main() {
    int score = 45;

    if (score >= 50) {
        printf("You passed!\n");
    } else {
        printf("You failed.\n");
    }

    return 0;
}
```

Explanation:

*   If score is 50 or more → “You passed!”
    
*   Otherwise → “You failed.”
    

Now the program always prints something.

## 3\. The `else if` Statement

What if you want more than two outcomes?

For example, grading:

*   80 and above → Grade A
    
*   60–79 → Grade B
    
*   50–59 → Grade C
    
*   Below 50 → Fail
    

You can use `else if`.

### Syntax

```c
if (condition1) {
    // runs if condition1 is true
} else if (condition2) {
    // runs if condition2 is true
} else {
    // runs if none are true
}
```

## 4\. Example: Grading System

```c
#include <stdio.h>

int main() {
    int score = 75;

    if (score >= 80) {
        printf("Grade A\n");
    } else if (score >= 60) {
        printf("Grade B\n");
    } else if (score >= 50) {
        printf("Grade C\n");
    } else {
        printf("Fail\n");
    }

    return 0;
}
```

**Important**:

C checks conditions from top to bottom.

As soon as one condition is true:

*   It runs that block
    
*   It skips the rest
    

So **order** matters.

## 5\. Logical Operators

Sometimes one condition is not enough.

You may want:

*   Age between 18 and 30
    
*   Score greater than 50 OR equal to 0
    

Logical operators help combine conditions.

### AND – `&&`

Both conditions must be true.

Example:

```c
if (age >= 18 && age <= 30) {
    printf("Young adult\n");
}
```

This only runs if age is between 18 and 30.

### OR – `||`

Only one condition must be true.

Example:

```c
if (score >= 90 || score == 0) {
    printf("Special case\n");
}
```

If either part is true, the block runs.

### NOT – `!`

Reverses a condition.

Example:

```c
if (!(age >= 18)) {
    printf("Not an adult\n");
}
```

This means: if age is NOT 18 or more.

## 6\. Taking User Input with `scanf`

So far, we have hardcoded values.

But real programs ask users for input.

We use `scanf` to read input.

### Basic Example

```c
#include <stdio.h>

int main() {
    int age;

    printf("Enter your age: ");
    scanf("%d", &age);

    if (age >= 18) {
        printf("You are an adult.\n");
    } else {
        printf("You are not an adult.\n");
    }

    return 0;
}
```

Important:

*   `%d` is for integers.
    
*   `&age` gives the memory address.
    
*   C needs the address to store the input.
    

Do not forget the `&`.

## 7\. Mini Project: Age Category Program

Let’s combine everything.

```c
#include <stdio.h>

int main() {
    int age;

    printf("Enter your age: ");
    scanf("%d", &age);

    if (age < 13) {
        printf("Child\n");
    } else if (age <= 19) {
        printf("Teen\n");
    } else {
        printf("Adult\n");
    }

    return 0;
}
```

This program:

*   Takes input
    
*   Checks multiple conditions
    
*   Prints the correct category
    

You are now writing real logic.

## 8\. Common Beginner Mistakes

1.  Putting conditions in the wrong order
    
2.  Forgetting curly braces
    
3.  Forgetting `&` in `scanf`
    
4.  Writing overlapping conditions
    

Example of wrong order:

```c
if (score >= 50)
```

placed before

```c
else if (score >= 80)
```

This would prevent Grade A from ever happening.

Always go from highest condition to lowest.

## 9\. Practice Exercises

1.  Write a program that:
    
    *   Asks for temperature
        
    *   Prints “Cold” if below 15
        
    *   Prints “Warm” if 15–30
        
    *   Prints “Hot” if above 30
        
2.  Write a program that:
    
    *   Asks for a number
        
    *   Prints “Even” if divisible by 2
        
    *   Prints “Odd” otherwise
        
3.  Write a grading program using user input.
    

## Final Thoughts

You now know how to:

*   Use `if`
    
*   Use `if–else`
    
*   Use `else if`
    
*   Combine conditions
    
*   Take user input
    

At this point, your programs can:

*   Store data
    
*   Make decisions
    
*   Respond to users
    

That’s a big step for a beginner.

Next, we’ll move into loops and that’s when your programs start repeating actions automatically.
