Lab 3: Operations and Expressions


Introduction

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. Let's consider a few simple examples:

12
is an expression. It's has one operand (the object 12), there are no operators, and it produces a value (the object 12).

Here's a more familiar example:

2 + 3
This 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.0
This 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 (like characters and strings), we can also write expressions for other types of data.

Ultimately, all computation in any programming language boils down to expressions. There are other language features to abstract and control which and when expressions are evaluated, but in the end, expressions do all the real work. So, to build our knowledge of writing programs, we start at the bottom with expressions.

Expression versus Statement

It is important to distinguish between an expression and a statement. Expressions are really an incomplete thought, like a sentence fragment. They are the building blocks for us to write our statements. A statement is complete thought; the compiler should have everything it needs to carry out an action. A statement always ends in a semicolon. (Actually, this is a bit of a lie as we'll see later.)

For example, consider the expression 2+3 again. We try to turn this into a statement by adding a semicolon:

2+3;
But what does this mean? Translated into English, we might say "add 2 and 3", but what are supposed to do with the result? Print it? Add it to a database? Do further computations with it? We could come up with hundreds of legitimate things we could do with the expression. The compiler will not guess; we must be specific.

If we want to print it

cout << 2 + 3;
or do further computations with it
int result = 2 + 3;
we have to specify these actions explicitly. Consider the English translations of these statements: "Send the addition of 2 and 3 to the screen (i.e., cout)." and "Save the addition of 2 and 3 in result so that I can use it later." Those are complete thoughts with 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 applies to input expressions.)

Files

Directory: lab3

Create the directory, and save the file there.

Add some lines to the comment at the top of the file similar to this:

* Modification history:
*    by John VanDoe in September 2002 for CPSC 185 at Calvin College
*       Modified to run the experiments for Lab #3.

The Experiments

You will find the variable declarations that you wrote for Lab #2 to be 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

Submit your answer 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.

Terminology

arithmetic expression, expression, operand, operator, statement, value (of an expression)
Lab Home Page | Prelab Questions | Homework Projects
© 2003 by Prentice Hall. All rights reserved.
Report all errors to Jeremy D. Frens.