Lab 3: Experiment 1


Output Expressions

We took a quick look at output in the previous lab; we'll expand on this some more in this experiment.

Interactive output is in C++ is done with the cout object. This is the name of the output screen or window. To actually send something to the screen, C++ provides the << operator. In general, an output statement has the following form:

   cout << value1 <<  value2 << ... <<  valuen;

Note that << operators separate each of the values.

As mentioned in the introduction to the lab, output statements are actually output expressions. Thus we must append a semicolon to make it a statement, which is what the compiler requires.

The endl Object

You may have noticed a strange object endl in the output statements of some example programs. (Note: That's an "ell" at the end of endl, not a "one.") One noticeable effect of endl is that it stops the current line of output and starts the next output on the next line. This suggests that endl is interchangeable with '\n'. But just how interchangeable?

Add this line of code in your program:

   cout << "Line #1." << '\n' << "Line #2" << '\n';
Don't remove anything from your program because you'll need it later. Recompile and run your program.

Question #3.1.1: What output does this statement produce? Be very precise with the line breaks.

Replace each '\n' with an endl. (Note that endl is not enclosed in quotes.) Recompile and execute your program.

Question #3.1.2: How has the output changed?

Unlike \n, which is simply one of several characters that produce a special effect when output, endl is the name of an object with a newline character in it (and perhaps other characters that we can't see.) Like cin and cout, endl comes from the iostream library.

You may be thinking why not just use '\n' all of the time? This would make output expressions like the one above much simpler:

   cout << "Line #1.\nLine #2\n";
Why bother with endl?

Actually, endl is more than just the newline character; it also indicates that you want the output to appear on the screen immediately instead of being stored in a section of memory called an output buffer that will eventually be "dumped" to the screen. (A buffer is used to improve the speed of execution of a program — it is faster to "write to memory" than to "write to the screen."). Usually, for these labs, you won't find this to be a big deal. It may never matter to you (depending on your program, compiler, and operating system) whether you use endl or '\n'.

Output Anything

We can output almost anything. Our program currently outputs strings, integer objects, and even the result of an expression. We can also output complex objects such as cin:

    cout << cin << endl;
Add this line to your program, recompile, and execute the program.

Question #3.1.3: What does this line output?

Outputting the value of cin isn't really useful; the point is that we can output it. For some objects we'll see later on, this can be very useful, especially in debugging our code.


Back to the Lab Exercise  |  Forward to the Next Experiment


Report errors to Larry Nyhoff (nyhl@cs.calvin.edu)