Control Structures and Loops





Now, we're starting to get to the things that make C a real programming language. Control Structures and loops provide mechanisms for iteration of statements and for branching of your program depending on the status of some data.

The If Statement

The concept of the if statement is fairly straightforward. You use it to tell C to do something only if some condition is true, and optionally to do something else otherwise. The form of the ifstatement is below:

if (test) {
   do something
}

where test is the logical test for executing the statement. An example of a test is something like i <= 3. More about this in a moment. Note that the statements enclosed by the if must be enclosed in curly braces. This is called a block. You can, however, omit the braces if the enclosed block is only one line long, but you may wish to keep them there for clarity. An if statement like this may also be followed by an optional number of else statements. These are fairly intuitive. Essentially, it tells to do something if some case is true, or else do something else. You can string together as many else statements as you like by using else if for all but the last one.

A final word about logical tests. You may be tempted to write something like if ((i >= 3) == 1) because in English you would say "if it is true that i is greater than or equal to 3." In C, however, this is redundant. The if block will execute as long as the expression in parentheses evaluates to true, or any non-zero number. This is also true with boolean data: the following example is redundant:

bool b = true;
if (b == true) {
   /* do things */
}

You can instead simply say if (b). This form is much more C-like.

The Switch Statement

switch is a lot like a long sequence of if/else statements together in one. The following is the general form of the switch statement:

switch (variable) {
   case test: do something; break;
   more cases

   default: do something; break;
}

The tests are a little different here: instead of being a logical expression, they are values to compare the variable to. Each case is essentially a shortened form of if (variable == test). The default statement at the end tells the program what to do if the variable doesn't match any of the cases. It is not required, but it is safer to put it in just in case. Also, notice the break statements at the end of every line. break takes you out of whatever block you're currently in. In this case, the break statements simply ensure that the program will exit the switch after it finds a matching case. Again, they are not necessary, but they make switch a much safer construction.

The While Loop

The while mechanism is the first loop we've seen in C. The general form of a while loop is below:

while (condition) {
   do things
}

The condition here is exactly like the test of an ifstatement. The loop continues to run until the condition is not true; as long as it is true, the loop will continue to run. The loop does not have to execute, either. If the condition is not true to begin with, the loop will never run. Because of the fact that there is no set limit for the number of times the loop will run, you have to be careful that the loop does not run infinitely. In general, infinite loops are bad. For some applications, though, they are useful, such as for programs that need to run for as long as the system does. That's why infinite loops are a feature, not a bug =). And there is another reason why loops are sometimes useful. Occasionally, it can be very difficult to set things up so that the condition you want to test lies at the beginning of the loop. In that case, you can make a clever use of infinite loops and the break statement, as below:

while (1) {
statements
   if (condition) break;
more statements
}

The first line means that the loop will run forever, since 1 is never false. Then, once the condition you want becomes true at the place in the loop you want, the break statement kicks you out of the loop.

The For Loop

A for loop is a special case of a while loop. It is an abbreviation for

variable = initial condition;
while (variable < condition) {
   statements
   variable += step;
}

We can abbreviate this all with

for (variable = initial condition; variable < condition; variable += step) {
}

where step is the value you want to increment (or decrement) your variable by. A fairly simple loop is demonstrated below:

int i;
for (i = 0; i < 10; i++) {
   printf("%d\n", i);
}

All loops and control structures can be embedded inside one another; this is called nesting.



Back to Basic Input/Output   On to Functions
Back to the Outline