Book Cover

Introduction to Scientific Programming
Computational Problem Solving Using:
Maple and C
Mathematica and C

Author:
Joseph L. Zachary
Online Resources:
Maple/C Version
Mathematica/C Version

Counting Loops Tutorial

In this tutorial you will experiment with using C while statements to implement counting loops. You will be using some example programs in this tutorial. You can use your Web browser to view or download them as you prefer.

While Loops

The general form of a while loop is

  while (<condition>) {
    <statements>
  }

where <condition> stands for a Boolean expression that is either true or false, and <statements> stands for one or more statements.

When C encounters a while loop, it follows these steps.

  1. The <condition> is evaluated. If it is false, C skips ahead to the next statement following the while loop. Otherwise, it proceeds to step 2.

  2. The <statements> are evaluated in order. When they have been evaluated, C returns to step 1 above.

The body of a while loop can be evaluated zero or more times. If the condition is initially false, the body of the loop will not be evaluated at all.

Counting Loops

Although every while loop has the same general form, while loops can be used to serve a variety of different purposes. A common form of loop is the counting loop, in which the loop repeats a fixed number of times.

Download count1.c and read it into an Emacs buffer. Compile and run the program. The program will prompt you for an integer; try entering 10 the first time, 5 the second time, and -5 the third time. Notice that the program prints out all of the integers from 1 up to the one that you entered. If the integer that you enter is smaller than 1, no integers are printed out.

The counting loop contained in count1.c is

  printf("The integers from 1 up to %d are:\n", limit);
  i = 1;

  while (i <= limit) {
    printf("%d ", i);
    i = i + 1;
  }

  printf("\n");

A counting loop typically uses a variable to count from some initial value to some final value. This variable is often called the index variable. In this example, the index variable is i.

There are usually six components to a counting loop:

A counting loop with always have an index initialization statement, an index control expression, one or more body statements, and an index update statement. It will not always have setup statements or final statements.

Exercises

A Counting Loop: Factorial

Download count2.c and read it into an Emacs buffer. This program is divided into a factorial function, which takes an integer n as its parameter and returns n!, and a main function, which exercises factorial. We will focus on the factorial function.

The counting loop that appears in factorial is

  product = 1;
  i = 1;

  while (i <= n) {
    product = product * i;
    i = i + 1;
  }

Recall that tex2html_wrap_inline74 . This loop works by accumlating the product of the first n integers into the variable product, which begins with a value of 1. Each time through the loop, the current value of i is multiplied into product. During the course of the loop, i counts up from 1 to n.

In the previous section we identified the six components of a counting loop. See if you can classify the components of the loop above along those lines.

Click here for the answer

Exercises