Title: cout (Print) Statement
Course: CPS 120: Introduction to Computer Science ~ Master (Aug/2015) OLD > Section: Learning Units

cout Statement:

- Used to display expressions, variables or values on the standard output
- Example: cout << "The square of 4is " << (4 * 4) << endl;

Visual C++ console output (cout) statement

- Writes data to the console.
- Has the form:

cout << [expr-1] << [expr-2] << … << [expr-n];

Statement Part | Description
cout | Console output command.
<< | Insertion operator; one precedes each expression to be sent to the console.   The << indicates the flow of data.
expr-n | Any valid expression (string, numeric, character, Boolean).
; | End of statement.

- In Visual C++, we build a string to output by connecting parts together with <<
- To use cout, the following statements are needed near the top of a C++ program:
#include <iostream>
using namespace std;
- Keyword endl is a special expression that starts a new console line.

cout Examples

Statement:  cout << "Hello World!";
Output:     Hello World!

Statement:  cout << "Hello World!" << endl;
Output:     Hello World!

Statement:  cout << "Hello" << endl << "World!" << endl;
Output:     Hello
World!

Statement:  cout << "There are " << 50 << " US states.";
Output:     There are 50 US states.

Statement:  cout << "2 + 4 = " << (2 + 4);
Output:     2 + 4 = 6