Else Statement โ”#

What if you wanted to also check if the condition wasnโ€™t satisfied in an if statement? You may assume we would have to write out two if statements, like:

int condition = 0;

if (condition) {

}

if (!condition) {

}

However you can actual use the else statement instead:

int condition = 0;

if (condition) {

} else {

}

You can also chain together else if to check for multiple conditions in one if statement chain.

int condition = 0;
int other_condition = 1;
int another_condition = 0;

if (condition) {

} else if (other_condition) {

} else if (another_condition) {

}

Note that you can only add the else at the very end of an if statement chain.

int condition = 0;
int other_condition = 1;

if (condition) {

} else if (other_condition) {

} else {

}

Tasks ๐ŸŽฏ#

Write a program that asks a person for their favorite number.

  • If the number is greater than 0, print โ€œYour number is positive!โ€.

  • If the number is less than 0, print โ€œYour number is negative!โ€.

  • If the number is equal to zero, print โ€œYour number is neither positive nor negative, so it must be zero then!โ€.

    Solution โœ…
    #include <stdio.h>
    
    int main() {
        int favorite_number = 0;
        printf("What is your favorite number?\n");
        scanf("%d", &favorite_number);
        if (favorite_number > 0) {
            printf("Your number is positive!\n");
        } else if (favorite_number < 0) {
            printf("Your number is negative!\n");
        } else {
            printf("Your number is neither positive nor negative, so it must be zero then!\n");
        }
        return 0;
    }