Conditional Constructs

3-minute read
Table of Contents

Sometimes we want to run code only if specific conditions are satisfied. Conditional statements allow us to do this.

If statements

With this we can run code if a condition is satisfied. If it is not satisfied then nothing happens. Mr. Jones, a restaurant owner only allows adults into his restaurant. He wants a piece of code that tells the user if they can enter the restaurant based on their age. He comes up with this:

#include <stdio.h>

int main(void){
    int age;
    printf("Please enter your age: ");
    scanf("%d",&age);

    if (age>=18){
        printf("You are an adult! You may enter.\n");
    }

    return 0;
}

check-if-adult.c
Copy

What happens when you input an age that is at least 18? What happens if you use an age less than 18?

If-else statements

With this, we can run code if a condition is satisfied and also run other code if the condition is not satisfied. Mr. Jones realizes that the code above does not do anything if a visitor inputs an age of less than 18. He modifies the code to the following:

#include <stdio.h>

int main(void){
    int age;
    printf("Please enter your age: ");
    scanf("%d",&age);

    if (age>=18){
        printf("You are an adult! You may enter.\n");
    }else{
        printf("Hey kid! What are you doing here?! Beat it!\n");
    }

    return 0;
}


check-if-adult-reject-child.c
Copy

What happens when you input an age that is at least 18? What happens if you input an age less than 18?

Execution of branches

The first branch whose condition is satisfied will be executed and the other branches will be ignored:

#include <stdio.h>

int main(void){
    int number = 9;

    if (number>7){
        printf("The number %d is greater than 7\n",number);
    }else if(number>5){
        printf("The number %d is greater than 5\n",number);
    }else{
        printf("The number %d is not greater than either\n",number);
    }

    return 0;
}

branches-if-else.c
Copy

Notice how 9 satisfies both of the conditions (>7 and >5) but only the first branch is executed. Try 6 and 3 as values for number and see what the result is in each case.

Switch-case statements

This can be used to execute code if a value matches any of a number of cases. Mrs. Pearson is a highschool teacher and she wants to have a way to remember the comment that she should place into a student’s report based on the grade they obtain. Her son creates the following program for her:

#include <stdio.h>

int main(void){
    char grade;
    printf("What grade did the student get? (A, B, C, D or F?)\n");
    scanf("%c",&grade);

    switch(grade){
        case 'A':
            printf("Excellent!\n");
            break;
        case 'B':
            printf("Very good!\n");
            break;
        case 'C':
            printf("Satisfactory work.\n");
            break;
        case 'D':
            printf("Can do much better.\n");
            break;
        case 'F':
            printf("What the sigma?\n");
            break;
        default:
            printf("That isn't a valid grade!\n");
    }
    return 0;
}
insert-comment-into-school-report.c
Copy

If the variable grade doesn’t match any of the specified cases, the code under the default case is executed.


Support us via BuyMeACoffee