Programming Fundamentals

Part 1: While Loops (15 mins)

In this short section, we will look at some while loops, and understand the four main components of a while loop.

You will use these eight programs during the tutorial:

A
void a(void) {
    int i = 5;
    while (i > 0) {
        printf("%d\n", i);
        i--;
    }
}
B
void b(void) {
    int i = 1;
    while (i < 32) {
        printf("%d\n", i);
        i = i + i;
    }
}
C
void c(void) {
    int i = 0;
    while (i < 32) {
        printf("%d\n", i);
        i = i + 2;
    }
}
D
void d(void) {
    int i = 5;
    while (i >= 0) {
        printf("%d\n", i);
        i--;
    }
}
E
void e(void) {
    int i = 0;
    int keep_going = 1;
    while (keep_going == 1) {
        if (i > 3) {
            keep_going = 0;
        }
        i++;
    }
    printf("%d\n", i);
}
F
void f(void) {
    int i;
    while (i > 0) {
        printf("%d\n", i);
        i--;
    }

}
G
void g(void) {
    int i = 0;
    int max = 32;
    while (i < max) {
        printf("%d\n", i);
        max = max + 2;
    }

}
H
void h(void) {
    int i = 0;
    int keep_going = 0;
    while (keep_going == 1) {
        if (i > 3) {
            keep_going = 0;
        }
        i++;
    }
    printf("%d\n", i);
}

Part 2: 2D While Loops (10 mins)

In this short section, we will expand to looking at 2D while loops.

Assume '#define SIZE 4' in all examples

You will use these four programs during the tutorial:

void a(void) {
    int row = 0;
    while (row < SIZE) {
        int col = 0;
        while (col < SIZE) {
            if (row == col) {
                printf("O");
            } else {
                printf("X");

            }
            col++;
        }
        row++;
        printf("\n");
    }
}
void b(void) {
    int row = 0;
    while (row < SIZE) {
        int col = 0;
        while (col < SIZE) {
            if (col % 2 == 0) {
                printf("O");
            } else {
                printf("X");

            }
            col++;
        }
        row++;
        printf("\n");
    }
}
void c(void) {
    int row = 0;
    while (row < SIZE) {
        int col = 0;
        while (col < SIZE) {
            if (col != 1 && row != 1) {
                printf("O");
            } else {
                printf("X");

            }
            col++;
        }
        row++;
        printf("\n");
    }
}
void d(void) {
    int row = 0;
    while (row < SIZE) {
        printf("X");
        int col = 1;
        while (col < 3) {
            if (row == 0 || row == 3) {
                printf("X");
            } else {
                printf("O");
            }
            col++;
        }
        printf("X");
        row++;
        printf("\n");
    }
}

Part 3: Enums and Structs (5 mins)

In this section we will create and use our own struct, and our own enum.

Structs

Below shows an example of a struct which we will explore further.
#include <stdio.h>

struct person {
    int shoe_size;
    double height;
    char first_name_initial;
};

Enums

Below shows an example of an enum which we will explore further.
#include <stdio.h>

enum opal_card_type {
    ADULT,
    STUDENT,
    CONCESSION
};

Part 4: Variable Names

In this section we will learn more about variable names.

You should remember the following rules about Legal variable names in C:

You should also remember these rules about variable names which are Good Style in C:

Variable names can still be a poor name, even if they follow the style guide. Variables should be named descriptively, and in a way which is relevant to the program.