Expressions and Statements

In programming, an expression is any legal combination of symbols that represents a value. Each programming language and application has its own rules for what is legal and illegal. For example, in the C language x+5 is an expression, as is the character string "MONKEYS."
Every expression consists of at least one operand and can have one or more operators. Operands are values, whereas operators are symbols that represent particular actions. In the expression
x + 5
x and 5 are operands, and + is an operator.
Expressions are used in programming languages, database systems, and spreadsheet applications. For example, in database systems, you use expressions to specify which information you want to see. These types of expressions are called queries.
Expressions are often classified by the type of value that they represent. For example:
Boolean expressions : Evaluate to either TRUE or FALSE
integer expressions: Evaluate to whole numbers, like 3 or 100
Floating-point expressions: Evaluate to real numbers, like 3.141 or -0.005
String expressions: Evaluate to character strings
An expression represents a single data item--usually a number. The expression may consist of a single entity, such as a constant or variable, or it may consist of some combination of such entities, interconnected by one or more operators. Expressions can also represent logical conditions which are either true or false. However, in C, the conditions true and false are represented by the integer values 1 and 0, respectively. Several simple expressions are given below:
a + b
x = y
t = u + v
x <= y
++j
j = j + 1
a = 6;
c = a + b;
++j;
{
pi = 3.141593;
circumference = 2. * pi * radius;
area = pi * radius * radius;
}
#define NAME text
#define PI 3.141593
area = PI * radius * radius;
area = 3.141593 * radius * radius;
The first expression, which employs the addition operator (+), represents the sum of the values assigned to variables a and b. The second expression involves the assignment operator (=), and causes the value represented by y to be assigned to x. In the third expression, the value of the expression (u + v) is assigned to t. The fourth expression takes the value 1 (true) if the value of x is less than or equal to the value ofy. Otherwise, the expression takes the value 0 (false). Here, <= is a relational operator that compares the values of x and y. The final example causes the value of j to be increased by 1. Thus, the expression is equivalent to
The increment (by unity) operator ++ is called a unary operator, because it only possesses one operand.
statement causes the computer to carry out some definite action. There are three different classes of statements in C: expression statements,compound statements, and control statements.
An expression statement consists of an expression followed by a semicolon. The execution of such a statement causes the associated expression to be evaluated. For example:
The first two expression statements both cause the value of the expression on the right of the equal sign to be assigned to the variable on the left. The third expression statement causes the value of j to be incremented by 1. Again, there is no restriction on the length of an expression statement: such a statement can even be split over many lines, so long as its end is signaled by a semicolon.
A compound statement consists of several individual statements enclosed within a pair of braces { }. The individual statements may themselves be expression statements, compound statements, or control statements. Unlike expression statements, compound statements do notend with semicolons. A typical compound statement is shown below:
This particular compound statement consists of three expression statements, but acts like a single entity in the program in which it appears.
symbolic constant is a name that substitutes for a sequence of characters. The characters may represent either a number or a string. When a program is compiled, each occurrence of a symbolic constant is replaced by its corresponding character sequence. Symbolic constants are usually defined at the beginning of a program, by writing
where NAME represents a symbolic name, typically written in upper-case letters, and text represents the sequence of characters that is associated with that name. Note that text does not end with a semicolon, since a symbolic constant definition is not a true C statement. In fact, during compilation, the resolution of symbolic names is performed (by the C preprocessor) before the start of true compilation. For instance, suppose that a C program contains the following symbolic constant definition:
Suppose, further, that the program contains the statement
During the compilation process, the preprocessor replaces each occurrence of the symbolic constant PI by its corresponding text. Hence, the above statement becomes
Symbolic constants are particularly useful in scientific programs for representing constants of nature, such as the mass of an electron, the speed of light, etc. Since these quantities are fixed, there is little point in assigning variables in which to store them.


Previous Page                                                                                                    Next Page

Spread the word! By sharing this Post!

Pageviews This Week