Menu
Coddy logo textTech

Else-If

Part of the Fundamentals section of Coddy's C journey — lesson 27 of 63.

The else-if statement allows you to check multiple conditions in sequence. When the first if condition fails, it moves to check the next else-if condition, and so on.

Start with a basic if statement:

if (grade >= 90) {
    printf("A grade\n");
}

Add an else-if to check another condition:

if (grade >= 90) {
    printf("A grade\n");
} else if (grade >= 80) {
    printf("B grade\n");
} else if (grade >= 70) {
    printf("C grade\n");
}

Finally, you can add an else statement at the end to handle all other cases:

if (grade >= 90) {
    printf("A grade\n");
} else if (grade >= 80) {
    printf("B grade\n");
} else if (grade >= 70) {
    printf("C grade\n");
} else {
    printf("Failed\n");
}
challenge icon

Challenge

Easy

Write a program that takes a temperature value and a character ('C' or 'F') indicating the scale. Based on the Celsius temperature, print one of the following messages:

  • If temperature < 0°C: "Freezing"
  • If temperature is between 0°C and 20°C: "Cold"
  • If temperature is between 21°C and 30°C: "Pleasant"
  • If temperature > 30°C: "Hot"

If the input is in Fahrenheit, convert it to Celsius first using the formula: C = (F - 32) * 5/9.

Print the message exactly as shown above based on the temperature.

Cheat sheet

The else-if statement allows checking multiple conditions in sequence. When the first if condition fails, it moves to check the next else-if condition.

Basic else-if structure:

if (condition1) {
    // code for condition1
} else if (condition2) {
    // code for condition2
} else if (condition3) {
    // code for condition3
} else {
    // code for all other cases
}

Example with grade checking:

if (grade >= 90) {
    printf("A grade\n");
} else if (grade >= 80) {
    printf("B grade\n");
} else if (grade >= 70) {
    printf("C grade\n");
} else {
    printf("Failed\n");
}

Try it yourself

#include <stdio.h>

int main() {
    int temperature;
    char scale;
    scanf("%d", &temperature);
    scanf(" %c", &scale);
    // Don't change above this line
    
    // Write your code here
    
    return 0;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals