The exercise for this lab involves a series of experiments, each investigating a different aspect of C++ expressions.
Definition: An expression is a sequence of one or more operands and zero or more operators that, when combined, produce a value.
The operands are objects; the operators are actions. Here is a simple example:
12This is an expression that has one operand (the object 12), but there are no operators; and it produces a value (the object 12).
Here's a more familiar example:
2 + 3This too fits the definition of an expression, since it consists of two operands (the objects 2 and 3) and one operator (
+
) that
combine to produce a value (the object 5).
Operands need not be constants:
2.5 * x - 1.0This also fits the definition of an expression, since it consists of three operands (the objects 2.5,
x
, and 1.0) and two
operators (*
, -
) that combine to produce a
value (1 less than the product of 2.5 and x
).
These examples have been arithmetic expressions, expressions whose operators are familiar arithmetic operations applied to numbers. C++ provides a rich set of arithmetic operators, but since C++ has other types of data (e.g., characters and strings), we can also write expressions for other types of data.
There are other language features that determine which and when expressions are evaluated, but it basically the expressions that do all the real work. Thus, to extend our learning how to write programs, we begin with the basic building blocks — expressions.
Expressions are the building blocks that we use to write statements.
When we append a semicolon to an expression, it becomes a statement. As an
example, consider the expression 2 + 3
again. If we append a
semicolon,
2 + 3;it becomes a statement. But what does the compiler do with this statement? It is instructed to "add 2 and 3", but what is it supposed to do with the result? Print it? Add it to a database? Do further computations with it? There are hundreds of possiblilities. The compiler can not guess which one to do; we must tell it. If we want to output it
cout << 2 + 3;or store its value in another variable
int result = 2 + 3;we have to specify these actions explicitly: "Send the sum of 2 and 3 to the screen (i.e.,
cout
)." or " Save the sum
of 2 and 3 in result
so that it can be used later.".
These are complete instructions that produce concrete results.
Some expressions are complete in themselves. In fact, the output statement above is actually an output expression that contains an arithmetic expression. Output expressions are complete in themselves, so we can just tack on a semicolon to make it a statement. (The same is true of input expressions.)
experiment.cpp
is our
"playground" for the experiments in this lab exercise. Here is a listing
of it:
/* experiment.cpp is a program to conduct experiments with C++ expressions. --> YOUR OPENING DOC HERE --------------------------------------------------------------*/ #include <iostream> using namespace std; int main () { int i = 3, j = 5; cout << "i is " << i << "\n" << "j is " << j << "\n" << "i+j is " << i + j << "\n"; }
Download/copy this file into whatever directory/folder/project you are using for lab 3.
As indicated in the opening documentation, add the opening documentation that you are expected (by your instructor) to have there; for example,
Written by: John Doe for CPSC 104X on Dec. 5, 2012 Input: Various data items required in this lab exercise Output: Results produced by the various experiments.
You may find the variable declarations that you wrote for Lab #2 useful for some of the experiments in this lab.
Experiment #1: Output Expressions
Experiment #2: Input Expressions
Experiment #3: Arithmetic Expressions
Experiment #4: Relational Expressions
Experiment #5: Logical Expressions
Experiment #6: Operator Precedence
Experiment #7: Operator Associativity
Experiment #8: Expressions Containing Functions
Experiment #9: Assignment Statements
Experiment #10: Assignment Shortcuts
Submit your answers to the questions for the experiments that you completed. Your instructor may also ask you to submit a copy or multiple copies of your program.