Single-line comments (p. 46) begin with //
. You insert comments to document your programs and improve their readability.
Comments do not cause the computer to perform any action (p. 47) when the program is run— they’re ignored by the compiler.
A preprocessing directive (p. 46) begins with #
and is a message to the C++ preprocessor. Preprocessing directives are processed before the program is compiled.
The line #include
<iostream>
(p. 46) tells the C++ preprocessor to include the contents of the input/output stream header, which contains information necessary to compile programs that output data to the screen or input data from the keyboard.
White space (i.e., blank lines, space characters and tab characters; p. 46) makes programs easier to read. White-space characters outside of string literals are ignored by the compiler.
C++ programs begin executing at main
(p. 47), even if main
does not appear first in the program.
The keyword int
to the left of main
indicates that main
“returns” an integer value.
The body (p. 47) of every function must be contained in braces ({
and }
).
A string (p. 47) in double quotes is sometimes referred to as a character string, message or string literal. White-space characters in strings are not ignored by the compiler.
Most C++ statements (p. 47) end with a semicolon, also known as the statement terminator (we’ll see some exceptions to this soon).
Output and input in C++ are accomplished with streams (p. 47) of data.
The output stream object std::cout
(p. 47)—normally connected to the screen—is used to output data. Multiple data items can be output by concatenating stream insertion (<<
; p. 48) operators.
The input stream object std::cin
—normally connected to the keyboard—is used to input data. Multiple data items can be input by concatenating stream extraction (>>
) operators.
The notation std::cout
specifies that we are using cout
from “namespace” std
.
When a backslash (i.e., an escape character) is encountered in a string of characters, the next character is combined with the backslash to form an escape sequence (p. 48).
The newline escape sequence
(p. 48) moves the cursor to the beginning of the next line on the screen.
C++ keyword return
(p. 49) is one of several means to exit a function.
All variables (p. 51) in a C++ program must be declared before they can be used.
Variables of type int
(p. 51) hold integer values, i.e., whole numbers such as 7, –11, 0, 31914.
A variable can be initialized in its declaration using list initialization (p. 51; introduced in C++11)—the variable’s initial value is placed in braces ({
and }
) immediately following the variable’s name.
A variable name is any valid identifier (p. 51) that is not a keyword. An identifier is a series of characters consisting of letters, digits and underscores ( _
). Identifiers cannot start with a digit. Identifiers can be any length, but some systems or C++ implementations may impose length restrictions.
A message that directs the user to take a specific action is known as a prompt (p. 52).
C++ is case sensitive (p. 52).
A program reads the user’s input with the std::cin
(p. 53) object and the stream extraction (>>
; p. 53) operator.
Most calculations are performed in assignment statements (p. 53).
A variable is a location in memory (p. 54) where a value can be stored for use by a program.
Every variable stored in the computer’s memory has a name, a value, a type and a size.
Whenever a new value is placed in a memory location, the process is destructive (p. 54); i.e., the new value replaces the previous value in that location. The previous value is lost.
When a value is read from memory, the process is nondestructive (p. 55); i.e., a copy of the value is read, leaving the original value undisturbed in the memory location.
The std::endl
stream manipulator (p. 54) outputs a newline, then “flushes the output buffer.”
C++ evaluates arithmetic expressions (p. 55) in a precise sequence determined by the rules of operator precedence (p. 56) and associativity (p. 56).
Parentheses may be used to group expressions.
Integer division (p. 56) yields an integer quotient. Any fractional part in integer division is truncated.
The remainder operator, %
(p. 56), yields the remainder after integer division.
The if
statement (p. 59) allows a program to take alternative action based on whether a condition is met. The format for an if
statement is
if (condition) {
statement;
}
If the condition is true, the statement in the body of the if
is executed. If the condition is not met, i.e., the condition is false, the body statement is skipped.
Conditions in if
statements are commonly formed by using equality and relational operators (p. 59). The result of using these operators is always the value true or false.
The using
declaration (p. 61)
using std::cout;
informs the compiler where to find cout
(namespace std
) and eliminates the need to repeat the std::
prefix. The using
directive (p. 61)
using namespace std;
enables the program to use all the names in any included C++ standard library header.