Lab 2: C++ Expressions


Introduction

Today's exercise involves a series of experiments, each investigating a different aspect of a fundamental C++ concept: the expression. The term expression can be defined as follows:

An expression is a sequence of one or more operands, and zero or more operators, that when combined, produce a value.

To illustrate, the sequence

   12
fits the definition of an expression, since it consists of one operand (12) and zero operators that combine to produce a value (12). Perhaps more familiarly, the sequence
   2 + 3
fits the definition of an expression, since it consists of two operands (2 and 3) and one operator (+) that combine to produce a value (5). Operands need not be constants: the sequence
   2.5 * x - 1.0
fits the definition of an expression, since it consists of three operands (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 last two examples have been arithmetic expressions, that is, expressions whose operators are familiar arithmetic operators. As we shall see in today's exercise, C++ provides a rich set of arithmetic operators, as well as many non-arithmetic operators that allow C++ programmers to construct non-arithmetic expressions.

The Experimental Laboratory

Using what you have learned about Windows-95 and Visual C++, begin by creating a Visual C++ project in your labs folder named express.

We will use the file express.cpp as a software laboratory for most of the experiments in this exercise. Click on the preceding link, and use your browser's

   File -> Save As
menu choice to save this file in your express subdirectory within your labs directory. Then in your Developers Studio window, choose Project -> Add file... to add express.cpp to your project. You can then open the file and take a moment to study its contents.

The form of the C++ iostream interactive output statement can be given as follows:

   cout << Expression1 << Expression2 << ... << ExpressionN;
When execution reaches such a statement, Expression1 is evaluated and displayed, after which Expression2 is evaluated and displayed, after which ..., after which ExpressionN is evaluated and displayed. An output statement thus provides a simple "laboratory" in which we can "experiment" with expressions by building them and viewing the values they produce.


Experiments

Below is a list of the experiments available for this exercise. Your instructor will tell you which ones you can omit, if any.

For each experiment that you are to perform, click its link and print a hard copy of the resulting web page. Then record your experimental results on that hard copy.

Experiment 1: Simple Expressions

Experiment 2: Characters and Character Strings

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: Declarations

Experiment 10: Constant Declarations

Experiment 11: Assignment Statements

Experiment 12: Assignment Associativity

Experiment 13: Assignment Shortcuts

Experiment 14: Increment and Decrement Expressions

Experiment 15: Input and Output Expressions


Phrases you should now understand:

Expression, Simple Expression, Operand, Operator, Operator Precedence, Operator Associativity, Integer Division, Logical Operator, Boolean Operator, Library Function, Variable Declaration, Constant Declaration, Operator Chaining, Increment Expression, Decrement Expression, Output Operator, Input Operator.


Submit:

Turn in to your instructor the hard copies showing the results of each of your experiments, plus a hard copy of your final version of express.cpp.


Back to This Lab's Home Page

Back to the Prelab Questions

Forward to the Homework Projects


Copyright 1998 by Joel C. Adams. All rights reserved.