Experiment 1: Simple Expressions


Study the source program express.cpp, particularly the lines

   cout << '\n' << i << '+' << j << " = " << i + j
        << endl << x << '+' << y << " = " << x + y
        << endl;
In the space below, write what you think will be appear on the screen when this program is executed (don't worry, wrong guesses are not penalized.)





Translate express.cpp into machine language, and run (execute) the resulting binary program. What is displayed?





If what was displayed was what you anticipated, give a detailed explanation of why you said what you did. If what was displayed differs from what you anticipated, explain what was wrong with your thinking.





From this, we can see that the effect of the iostream interactive output statement is to display on the screen the value of each Expressioni in the statement. Perhaps less obviously, the word endl used as an ouput expression (declared in iostream.h) produces a similar effect to the newline character ('\n').

Simple Expressions

An expression that has a single operand and no operators is called a simple expression. Two of the expressions in the output statement in express.cpp are not simple expressions. Which of the expressions are not simple expressions?





This example illustrates several different kind of simple expressions:

Output Expressions

The final thing to see in this exercise is that the output statement itself consists of a not-so-simple expression. That is, in the lines below

   cout << '\n' << i << '+' << j << " = " << i + j
        << endl << x << '+' << y << " = " << x + y
        << endl;
cout is a stream of characters running from your program to your screen, and each << symbol is a special operator, called the insertion operator. The effect of an expression
   cout << Expression
is to insert the value of Expression into the stream named cout. Since that stream "flows" to your screen, the characters appear there! So << is itself an operator, just like +, but its left operand must be an output stream like cout, and its behavior is to insert the value of its right operand into its left operand.


Back to the Experiment List

Forward to the Next Experiment


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