Making a statement!




Document: Software Engineering 1: Course Notes

next Branching and Looping
up Chapter 2 pp. 11-19
previous What's all this about ;;Boolean;; variables?

Making a statement!

Now that we have some logical operators available to us, how can we use their results to control the flow of execution in a program?

The answer is that there are special kinds of C statement where the exact effect of the statement can depend on a value generated at execution time (usually by a logical operator). But what is a "statement" anyway? Let's back up and review the concept a little.

We have seen examples of C "statements" already, in the introductory chapter; they were things like:

  R = Rpct / 100;

or:

  printf("\nEnter: Principal, Rate%, No. of yrs.\n");

These are all what we might term "simple" statements: they are the components of a program that are carried out, in sequence, when the program is executed. Technically, these particular statements are all examples of what are called expression statements: that is, as we shall see later, we can always think of the execution of these statements as involving, in effect, the evaluation of an "expression".

In any case, the "statement" notion is much broader than is accommodated by just expression statements. We shall see other more complex kinds of statement shortly. But first note that statements in general should be distinguished from the other major "kinds" of program component. As well as statements, other program components we have seen are comments, preprocessor directives, and declarations.

Comments are simple pieces of text delimited by the special tokens /* and */ which are ignored by the compiler, but serve to provide some additional information to a person reading the program:

/* WOTCOST; Computes the cost of a loan. */

Directives are the components of a program signalled by the # character. They are special instructions which control the way the rest of the program is translated or compiled; we say that they play a role only at "compile time". The only example we have seen so far is the #include directive - e.g. as follows:

  #include <stdio.h>

Its effect is to tell the compiler to also compile an extra separate file (in this case, one called stdio.h) as an integral part of the compilation of our own file.

Declarations, roughly speaking, "prepare the ground" for statements to be subsequently executed. So far, the only declarations we have seen have been concerned with creating variables with given names and types, e.g. as follows:

  float P, Rpct, R, M;

Recall that declarations and statements can act as sub-components: in particular, they may be combined to form a block, by enclosing them within braces; and a block, in turn, can form part of a function definition. This notion of hierarchical structure, with components within components within components, is a very important and pervasive one in the design of all complex engineering systems.

For now, note that technically a block is, itself, regarded as a special kind of statement. In fact, it is also alternatively referred to as a compound statement. The significance of this is that anywhere in a program where it is "grammatically" (or "syntactically") legal to place a "simple" statement then it will also be legal to put in a whole block, comprising an indefinitely large group of declarations and statements. It follows, logically, that a block may itself be a component of a larger more encompassing block and so on! Granted, there is no obvious reason why this might be useful yet, but we'll get there in due course!




Document: Software Engineering 1: Course Notes

next Branching and Looping
up Chapter 2 pp. 11-19
previous What's all this about ;;Boolean;; variables?



McMullin@ugmail.eeng.dcu.ie
Wed Apr 12 19:40:14 BST 1995