Programming in C (Part 4 - Control Structures) — 17 July, 2015

IF conditions

Do you remember the program about the apples we did in Part 2?

We will now define how much apples can fit in the basket, if the number of apples is bigger than the maximum number of apples the basket can take we will send a warning. For that we will use a control structure, if (expression) statement, the statement is executed only if the expression is true.

#include <stdio.h>

#define APPLES 15
#define MAXAPPLES 10
int main() {
    printf("You have %d apples\nThe basket can take %d\n", APPLES, MAXAPPLES);

    if (APPLES > MAXAPPLES){
        printf("You have no space for all this apples in the basket, you need \
to remove %d apples\n", APPLES - MAXAPPLES); 
    }

    return 0;
}


Now we will say how many more apples the basket can take, we will use an if (expression) statement-1 else statement-2. The statement-1 will be executed if the expression is true, the statement-2 will be executed if the expression is false.

#include <stdio.h>

#define APPLES 6
#define MAXAPPLES 10
int main() {
    printf("You have %d apples\nThe basket can take %d\n", APPLES, MAXAPPLES);

    if (APPLES > MAXAPPLES){
        printf("You have no space for all this apples in the basket, you need \
to remove %d apples\n", APPLES - MAXAPPLES); 
    }
    else {
        printf("You can still put %d more apples in the basket\n", MAXAPPLES - APPLES);
    }

    return 0;
}


We did some progress but what will happen if APPLES is equal to MAXAPPLES?

#include <stdio.h>

#define APPLES 10 
#define MAXAPPLES 10
int main() {
    printf("You have %d apples\nThe basket can take %d\n", APPLES, MAXAPPLES);

    if (APPLES > MAXAPPLES){
        printf("You have no space for all this apples in the basket, you need \
to remove %d apples\n", APPLES - MAXAPPLES); 
    }
    else if (APPLES < MAXAPPLES) {
        printf("You can still put %d more apples in the basket\n", MAXAPPLES - APPLES);
    }
    else {
        printf("The basket can't take more apples\n");
    }

    return 0;
}

As you can see we used an else if, we can use how many else if as we want, the else will be executed if none of the above conditions is true.


Switch

The switch is very similar to an if ... else if ... else condition.

#include <stdio.h>

int main() {
    int grade = 3;

    switch (grade) {
        case 1:
            printf("How can you get such a bad grade!?\n");
            break;
        case 2:
            printf("That's bad...\n");
            break;
        case 3:
            printf("Not bad\n");
            break;
        case 4:
            printf("Good Job\n");
            break;
        case 5:
            printf("Excelent!!\n");
            break;
        default:
            printf("What??\n");
            break;
    }

    return 0;
}

That should be very easy to understand, if none of the above conditions is true then the default condition is executed.

When you use the break you are getting out of the switch block, if you don't use the break all the above statements will be executed.

Try this code with and without breaks to see what happens.