C Conditionals

All of the C programs that we've examined until now have been very similar to each other. Sure, the different programs solve different problems, and they are divided into different functions, and so on, but all of the programs follow the same general behavior outline:

  1. Start with some data values, either ``built-in'' or input from a person.
  2. Perform some specific calculations with those values.
  3. Print out the results of those calculations.
  4. Stop.

Each of our example programs goes straight through that outline every time it is run. The program's input and output values might be different from one run to the next, but the sequence of statements executed inside the program is always the same. The program carries out the same instructions, in the same order, every time you run it.

In a way, all of our programs have been fancy, pre-programmed, special-purpose calculators. You put one set of numbers in, and another set of numbers comes out. Our programs have never had to make decisions or do different things for different situations.

In this lesson you'll begin learning how to put decision-making control structures in your C programs. A control structure is a statement (or sequence of statements) that ``makes decisions'' and directs the computer's path through a program. In particular, you'll be learning about if statements.

Click on the button below to copy today's example files into your own examples directory (i.e., the directory named examples inside your home directory).

EXECUTE Click to copy the example files.


Joseph L. Zachary
Hamlet Project
Department of Computer Science
University of Utah