Tuesday, September 21, 2010

C Is a Structured Language

In your previous programming experience, you may have heard the term blockstructured
applied to a computer language. Although the term block-structured
language does not strictly apply to C, C is commonly referred to simply as a
structured language. It has many similarities to other structured languages, such
as ALGOL, Pascal, and Modula-2.
The reason that C (and C++) is not, technically, a block-structured language is that
block-structured languages permit procedures or functions to be declared inside
other procedures or functions. Since C does not allow the creation of functions
within functions, it cannot formally be called block-structured.
The distinguishing feature of a structured language is compartmentalization of code
and data. This is the ability of a language to section off and hide from the rest of the
program all information and instructions necessary to perform a specific task. One
way that you achieve compartmentalization is by using subroutines that employ local
(temporary) variables. By using local variables, you can write subroutines so that the
events that occur within them cause no side effects in other parts of the program. This
capability makes it very easy for programs to share sections of code. If you develop
compartmentalized functions, you only need to know what a function does, not how it
does it. Remember, excessive use of global variables (variables known throughout the
entire program) may allow bugs to creep into a program by allowing unwanted side
effects. (Anyone who has programmed in standard BASIC is well aware of this
problem.)
The concept of compartmentalization is greatly expanded by C++. Specifically, in
C++, one part of your program may tightly control which other parts of your
program are allowed access.
A structured language allows you a variety of programming possibilities. It
directly supports several loop constructs, such as while, do-while, and for. In a
structured language, the use of goto is either prohibited or discouraged and is not the
common form of program control (as is the case in standard BASIC and traditionalFORTRAN, for example). A structured language allows you to place statements
anywhere on a line and does not require a strict field concept (as some older
FORTRANs do).
Here are some examples of structured and nonstructured languages:
Nonstructured           Structured
FORTRAN               Pascal
BASIC                      Ada
COBOL                     Java
C++
C
Modula-2
Structured languages tend to be modern. In fact, a mark of an old computer
language is that it is nonstructured. Today, few programmers would consider using
a nonstructured language for serious, new programs.
New versions of many older languages have attempted to add structured elements.
BASIC is an example. However, the shortcomings of these languages can never be
fully mitigated because they were not designed with structured features from the
beginning.
C's main structural component is the function—C's stand-alone subroutine. In
C, functions are the building blocks in which all program activity occurs. They let
you define and code separately the separate tasks in a program, thus allowing your
programs to be modular. After you have created a function, you can rely on it to
work properly in various situations without creating side effects in other parts of
the program. Being able to create stand-alone functions is extremely critical in larger
projects where one programmer's code must not accidentally affect another's.
Another way to structure and compartmentalize code in C is through the use of
code blocks. A code block is a logically connected group of program statements that is
treated as a unit. In C, you create a code block by placing a sequence of statements
between opening and closing curly braces. In this example,
if (x < 10) {
printf("Too low, try again.\n");
scanf("%d", &x);
}
the two statements after the if and between the curly braces are both executed if x is
less than 10. These two statements together with the braces represent a code block.
They are a logical unit: One of the statements cannot execute without the other
executing also. Code blocks allow many algorithms to be implemented with clarity,
elegance, and efficiency. Moreover, they help the programmer better conceptualize
the true nature of the algorithm being implemented.

No comments:

Post a Comment