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
ifstatement 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 theifstatement 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 theifmust 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. Anifstatement like this may also be followed by an optional number ofelsestatements. 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 manyelsestatements as you like by usingelse iffor 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. Theifblock 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
switchis a lot like a long sequence ofif/elsestatements together in one. The following is the general form of theswitchstatement:
switch (variable) {
casetest: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). Thedefaultstatement 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 thebreakstatements at the end of every line.breaktakes you out of whatever block you're currently in. In this case, thebreakstatements simply ensure that the program will exit theswitchafter it finds a matching case. Again, they are not necessary, but they makeswitcha much safer construction.
The While Loop
The
whilemechanism 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 thebreakstatement, 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
breakstatement kicks you out of the loop.
The For Loop
A
forloop is a special case of awhileloop. It is an abbreviation forvariable
=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 | ||