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.
It is assumed that you have undertaken tutorial 1 and, ideally, have read one of the associated readings.
The following textbook sections are relevant:
Other material within these texts is also relevant.
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.
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:
1. Modify the program above to print whether a number is divisible by 7 or not.
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:
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?
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;
}
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.