CSSE2310 / CSSE7231
C Programming Tutorial 2
If Statements, Loops and Expressions

This tutorial introduces you to conditional statements (if statements) and loops. Along the way, you're also introduced to variable declarations, assignment statements, and expressions. This tutorial is a variant of the original CSSE1000 C Programming Tutorial 2.

Assumed Background

It is assumed that you have undertaken tutorial 1 and, ideally, have read one of the associated readings.

Associated Reading

The following textbook sections are relevant:

  • Kernigan & Ritchie (2nd ed.) - pages 8 to 14
  • Harbison & Steel (5th ed.) - Sections 5.1, 7.8, 8.6, 15.8 and 15.11

Other material within these texts is also relevant.

Environment

You may wish to create a new directory for this tutorial. You may type (cut and paste) each program in a separate C file or you may just use one C file and just overwrite your previous program each time.

Conditional statements

Most programs need to make decisions based on the data they're operating on. For example, consider the following program:

#include <stdio.h>

int main() 
{
    int num;
    
    /* Prompt for and read in number from user */
    printf("Enter number: ");
    scanf("%d", &num);
    if((num % 2) == 1) {
        printf("%d is odd\n", num);
    } else {
        printf("%d is even\n", num);
    }
    return 0;
}

You should type this program into an editor, save it, compile it and run it. There are several new concepts illustrated in this program:

int num;
This line declares a variable of type int (integer). In C, all variables must be declared before they are used - normally at the beginning of a function (main() is the function in this case). Variable declarations take the form:
type-name variable-name, variable-name, ...;
i.e. one or more (comma separated) variable names after a type name (followed by a semicolon).
Types include int (integer), float (single precision floating pointing number), double (double precision floating point number), char (character or byte) and many others.
scanf("%d", &num);
This line scans (i.e. reads in) a number from standard input (i.e. the user typing) and places it in the variable num. The first argument to scanf is a format string indicating the type(s) of input expected to be read - %d indicates that a decimal number (integer) is expected. The following argument(s) is/are the address(es) of variables which will hold the data read in (which must match the type specified). You don't need to understand "addresses of variables" yet - we will return to this in a later tutorial. &num means "address-of num"
if ...
This is a conditional statement that takes the general form:
if(expression1)
statement1;
else if (expression2)
statement2;
...
else
statementn;

If the first expression is true, then the first statement is executed. If the second expression is true then the second statement is executed, and so on. If no expression is true, the last statement is executed. Note that the else parts of the statement are optional. Note also that any of the statements can actually be multiple statements enclosed in braces. It is good programming style to always use the braces as in the example above. C interprets an expression of value 0 as false and any expression with non-zero value as true.
num % 2
The % or modulus operator returns the remainder of dividing the integer on the left by the integer on the right, i.e. x % y produces the remainder when x is divided by y (e.g. 11 % 3 is 2, 12 % 3 is 0). Other arithmetic operators include + (addition), - (subtraction), * (multiplication) and / (division). Note that / (division) applied to integers will produce the quotient - i.e. any remainder will be thrown away (e.g. 11/3 = 3). Use the % operator to obtain the remainder. The +, -, * and / operators can be applied to floating point numbers, however the % operator can not.
(num % 2) == 1
The == operator performs an equality comparison between the expression on the left (num % 2 in this case) and that on the right (1 in this case) and produces true (i.e. a non-zero value) if they're equal and false otherwise (i.e. 0).
printf("%d is odd\n", num);
We used printf() in tutorial 1 to just print "Hello World". We see in this example that printf() is capable of more general formatted printing. The printf() function takes a variable number of arguments. The first argument is a string of characters (formatting string) to be printed with each % indicating where the other arguments are to be substituted (and the character after the percent indicating the type of the argument). For example, %d indicates an integer. You must ensure that the type of the argument matches that specified in the formatting string.
Note that printf() is not part of the C language - it's just a function defined in a standard library of functions. Not all implementations will have a printf() function. We'll see further examples of printf() statements below.

Exercise

1. Modify the program above to print whether a number is divisible by 7 or not.

Loops

A "loop" is a programming construct that lets you repeat a number of actions a certain number of times (or until a certain condition is true or false). We're going to look at several ways of writing loops in C.

Consider the following flowchart which illustrates how to add the integers 1 through 10 and output the result:

The C equivalent of this program is:

#include <stdio.h>

int main() 
{
    int num, sum;

    num = 1;
    sum = 0;
    while(num <= 10) {
        sum = sum + num;
        num = num + 1;
    }
    printf("Sum = %d\n", sum);
    return 0;
}

You should type this program into an editor, save it, compile it and run it. There are several new concepts illustrated in this program:

int num, sum;
Note that multiple variables of the same type can be declared on the same line.
The declaration could have been written as two lines:
int num;
int sum;
num = 1;
This line is an example of an assignment statement. The variable on the left of the equals sign is assigned the value of the expression on the right. Assignment statements take the form:
variable-name = expression;
while(num <= 10) {
...
}
This is a while-loop. The program will execute the statements within the loop while the expression within the parentheses is true (i.e. in this case, while the value of the variable num is less than or equal to 10). While-loops take the form
while(expression) {
statement;
...
}

and result in the execution of the statements (called the body of the loop) until the expression becomes true.
The braces may be omitted if there is only one statement within the loop:
while(expression)
statement;

but it is usually safer to include them.
Note that it is conventional to indent the body of a loop to indicate the logical structure of the program, however, the C compiler doesn't care about indentation.
sum = sum + num;
num = num + 1;
These are further examples of assignment statements. The variable on the left is assigned the value of the expression on the right. Because it is quite common to add some value to a number, C has a special way of writing this. These statements could be rewritten as:
sum += num;
num += 1;

Further, because adding one to a number is quite common in C, it is possible to rewrite num += 1 as
num++;
or
++num;
The ++ means increment by one and can be used as a postfix operator (after the variable name) or prefix operator (before the variable name). These can be used within expressions and have different values, e.g.
sum += (num++);
means
sum = sum + num;
num = num + 1;

whereas
sum += (++num);
means
num = num + 1;
sum = sum + num;

A similar operator (--) exists for decrementing (i.e. subtracting one).

Exercises

2. Modify the program above to calculate the factorial of 7 - i.e. multiply all integers from 1 to 7. Note that the *= operator can also be used in a similar way to the += operator.
3. Modify the program further so that it reads a number from the user and calculates the factorial of that number. Try running this program multiple times and determine the largest number for which you get a "reasonable" answer for the factorial. Why do you stop getting "reasonable" answers at some point?

For Loops

Another way of writing loops is with a for-loop statement. Consider the following program (try compiling and running it):

#include <stdio.h>

int main() 
{
    int num, sum;

    sum = 0;
    for(num = 0; num <= 10; num++) {
        sum += num;
    }
    printf("Sum = %d\n", sum);
    return 0;
}

This program does exactly the same thing as the while loop example above. For loops take the form:
for(initialisation; test-expression; increment)
statement
where statement can be replaced by any number of statements within braces. There are three parts within the parentheses - separated by semicolons. The first part is the initialisation - performed once before the loop is entered. The second part is the test expression - the body of the loop is executed when this expression is true. If the body of the loop is executed, the increment part is then executed and the test expression is re-evaluated. (Note that the increment step does not have to increment by 1 - any expression can be used here.

To add the odd numbers from 1 to 99, the for loop above could be modified to:
for(num=1; num <= 99; num = num+2) {
sum += num;
}

Note that a for loop can always be written as a while loop:
initialisation;
while(test-expression) {
statement(s);
increment;
}

The choice between using a while loop and a for loop is somewhat arbitrary. Often a for loop is used when a fixed sequence is being counted through.

Loops can be nested. Try running the following:

#include <stdio.h>

int main() 
{
    int a, b;
    int size;
        
    size=10;
        
    for(a=1; a <= size; a++) {
        for(b=1; b<= size; b++) {
            /* Print out the sum of a and b - using a field width of 3 */
            printf("%3d ", a+b);
        }
        printf("\n");
    }
    return 0;
}

The only new concept here is the use of a field width in the printf function. %3d means print out an integer using a minimum field width of 3 characters - i.e. if the number will take fewer than 3 characters when printed then pad it out with spaces so that it takes 3 characters.

This program produces an addition table for the first 10 integers:

  2   3   4   5   6   7   8   9  10  11 
  3   4   5   6   7   8   9  10  11  12 
  4   5   6   7   8   9  10  11  12  13 
  5   6   7   8   9  10  11  12  13  14 
  6   7   8   9  10  11  12  13  14  15 
  7   8   9  10  11  12  13  14  15  16 
  8   9  10  11  12  13  14  15  16  17 
  9  10  11  12  13  14  15  16  17  18 
 10  11  12  13  14  15  16  17  18  19 
 11  12  13  14  15  16  17  18  19  20 

We can extend the program to add some headings. Work through this program to make sure you understand how it works.

#include <stdio.h>

int main() 
{
    int a, b, i, size;

    size=10;

    /* Write out heading line */
    printf("a+b| ");
    for(b=1; b <= size; b++) {
        printf("%3d ", b);
    }
    printf("\n");

    /* Write out separator line (dashes) */
    printf("---+-");
    for(i=1; i <= size*4; i++) {
        printf("-");
    }
    printf("\n");
    for(a=1; a <= size; a++) {
        printf("%2d | ", a);
        for(b=1; b<= size; b++) {
            printf("%3d ", a+b);
        }
        printf("\n");
    }
    return 0;
}

Exercises

4. Modify the program above to produce a multiplication table.
5. Try modifying the field width specifications so that the table is slightly more spread out. What other lines have to change?
6. Modify the program to produce an addition table for numbers from -5 to 5 inclusive (rather than 1 to 10)
7. Modify the program to use while loops instead of for loops.