Structured Programming
Structured programming is a programming paradigm advocated by computer
scientists since the late sixties. There are three basic control structures
in structured programming from which any program can be written:
- The sequence structure. Actions can take place in sequence,
one after another.
- The selection structure. Actions can take place conditionally,
depending on whether some condition is met.
- The iteration structure. Actions can continue to take place
while some condition is true, and then proceed to the next action when the
condition is no longer true.
Some others programming models are object oriented programming,
functional programming, and logic programming. C is
best suited for structured programming.
The Selection Structure
In C, the selection structure is implemented with the if statement.
the format of an if statement is:
if ( expression )
statement1;
else
statement2;
where expression is some expression yielding a truth value
that is interpreted as being either true or false. If the value is true,
statement1 is executed. If it is false, statement2
is executed. The part beginning with else is optional; you need
not have an else clause in an if statement.
Compound Statements
Often, we want to do more than one statement within an if statement.
In this case, we can make a compound statement composed of many
statements enclosed in curly braces. If we do this, the compound statement
is treated as one big statement by the if statement:
if ( expression ) {
statement1;
statement2;
statement3;
} else {
statement4;
statement5;
statement6;
}
A compound statement works just about anywhere a regular statement works.
You can even declare new local variables at the beginning of a compound
statement (although it's usually best to declare them at the beginning
of a the function).
Logic and Truth Values
The expression inside the if statement can be any arithmetic
or logical expression. In C, if the value of the expression is 0,
then the truth value is false. Otherwise, the truth value is true.
So something like this:
if ( 6 )
printf ("Hello!\n");
would print Hello!, since 6 isn't 0 and thus is true when interpreted
as a truth value. Normally, we treat numbers and truth values differently
and distinguish between them in our variable names and the context in which
they are used.
C provides many logical and relational operators whose purpose is to return
truth values.
Relational Operators
A relational operator accepts two numeric operands and
returns a truth value giving information about the relationship between
the operands. For example, the < operators returns true
if and only if its left operand is less than its right operand. Here is
a list of some C relational operators:
Operator Returns true if and only if
< first is less than second
> first is greater than second
<= first is at most second
>= first is at least second
== first is equal to second
!= first is not equal to second
Logical Operators
A logical operator accepts truth values as operands and return a truth
value as the result. For example, the && (read and)
operator returns true if and only if both operands are true. Here is a
list of logical operators:
Operator Returns true if and only if Called
&& both operands are true AND
|| at least one operand is true OR
! the operand is false (unary operator) NOT
Another way to express the way these logical operators function is
a truth table showing exactly when they return true and false.
The following table looks at two variables a and b
containing truth values, and the values of AND, OR, and NOT:
a b a && b a || b !b
------------------------------------
F F F F T
F T F T F
T F F T
T T T T
The canonical value for true returned by relational and logical operators
is the integer 1, e.g. in C the numeric value of the expression
2 < 3 would be 1.
Sample Programs
Let's look at a sample C program illustrating the use of if,
relational, and logical operators. What is the output of this program?
#include <stdio.h>
int main () {
int a, b, c;
a = 2;
b = 3;
c = 5;
if ( a + b == c )
printf ("T");
else
printf ("F");
if ( a < b || c < a )
printf ("T");
else
printf ("F");
if ( ! (a < b) && !(b != c))
printf ("T");
else
printf ("F");
if ( a != a || a <= a )
printf ("T");
else
printf ("F");
return 0;
}
Here is another sample program using if. It asks the user to
type in his or her annual income, then computes the income tax owed
(we are assuming the user lives in a country with a very simple tax
system with tax brackets beginning at $9,000, $20,000, and $40,000
per year).
#include <stdio.h>
int main () {
float income, /* user's income */
tax; /* tax owed */
/* prompt for and read income */
printf ("What was your income last year? ");
scanf ("%f", &income);
/* compute tax based on income and tax bracket */
if (income < 9000.00) {
/* inform user he or she may get EIC */
printf ("You may qualify for earned income credit.\n");
tax = 0.15 * income;
} else if (income < 20000.00)
tax = 0.20 * income;
else if (income < 40000.00)
tax = 0.30 * income;
else {
/* it can't hurt to ask... */
printf ("May I borrow some money?\n");
tax = 0.40 * income;
}
/* give user income tax figure with 2 digits after decimal */
printf ("Your income tax is %0.2f\n", tax);
return 0;
}