Professional Documents
Culture Documents
• Includes
– short
– unsigned short
– Int
– unsigned int
– Long
– unsigned long
data types
Floating-Point Data Types
• For example:
– If your program has two integer variables named
students and classes,
classes you could compute how many
students you have,
have given number of classes,
classes if you
know there were 15 students per class:
students = classes * 15;
• In this example, 15 is a literal constant.
constant
• If you substituted a symbolic constant for this
value:
students = classes * studentsPerClass
IDENTIFIERS
IDENTIFIERS
• An identifier is a programmer-defined name that
represents some element of a program.
– Variable names are examples of identifiers.
• A valid identifier is a sequence of one or more letters,
letters
digits or underscore(
underscore _ ).
• Neither spaces nor special characters can be part of
an identifier.
• Variable identifiers should always begin with a letter
and underscore(
underscore _ ).
• They cannot begin with a digit.
digit
• They cannot match any key word of the C++ language
nor your compiler's specific ones.
Legal Identifiers
• Do not confuse the backslash (\) with the forward slash (/).
• Do not put a space between the backslash and the control character.
BASIC INPUT/OUTPUT
BASIC INPUT/OUTPUT
• In the iostream C++ library,
library standard input and
output operations are supported by two data streams:
– cin for input and
– cout for output.
output
• Therefore:
– cout (the standard output stream) is normally directed to
the Monitor
– and cin (the standard input stream) is normally assigned to
the Keyboard.
Keyboard
• By handling these two streams you can show
messages on the screen and receive input from the
keyboard.
Output (cout)
• The cout stream is used in conjunction with the overloaded
operator << (a pair of "less than" signs).
cout << "Output sentence"; // prints Output sentence on screen
cout << 120; // prints number 120 on screen
cout << x; // prints the content of variable x on screen
• When the << symbol is used this way, it is called the stream-
insertion operator,
operator
– since it inserts the data that follows it into the stream that precedes it.
• The information immediately to the right of the operator is
sent to cout and then displayed on the screen.
– In the examples above it inserted the constant string Output sentence,
sentence
the numerical constant 120 and the variable x into the output stream
cout.
cout
…. Output (cout)
• To use constant strings of characters we must enclose them
between double quotes (")
– so that they can be clearly distinguished from variables.
variables
– The output message is enclosed between double quotes (") because it
is a string of characters.
characters
• For example, these two sentences are very different:
cout << "Hello"; // prints Hello on screen
cout << Hello; // prints the content of Hello variable on
screen
• The insertion operator (<<) may be used more than once in a
same sentence:
cout << "Hello, " << "I am " << "a C++ sentence";
this would print the message Hello, I am a C++ sentence on
the screen.
Input (cin)
• Handling the standard input in C++ is done by
applying the overloaded operator of extraction (>>)
on the cin stream.
stream
• This must be followed by the variable that will store
the data that is going to be read.
read
• For example:
int age;
cin >> age;
• cin can only process the input from the keyboard
once the ENTER key has been pressed.
…. Input (cin)
• You can also use cin to request more than one
datum input from the user:
cin >> a >> b;
is equivalent to:
cin >> a;
cin >> b;
• In both cases the user must give two data,
data one
for variable a and another for variable b that may
be separated by any valid blank separator:
separator
– a space,
space
– a tab character or
– a newline.
newline
Example on I/O
// i/o example
#include <iostream.h>
int main ()
{
int i;
cout << "Please enter an integer value: ";
cin >> i;
cout << “\nThe value you entered is " << i;
cout << " and its double is " << i*2 << ".\n";
return 0;
}
OUTPUT
Please enter an integer value: 702
The value you entered is 702 and its double is 1404.
OPERATORS
Operators
• An operand is usually a piece of data, like a number.
• There are three types of operators: unary, binary, and
ternary.
• Unary operators only require a single operand.
operand
– For example, consider the following expression: -5
– The minus sign, when used this way, is called the negation operator.
• Binary operators work with two operands.
operands
– The assignment operator is in this category.
– E.g. a=1
• Ternary operators require three operands.
operands
– C++ only has one ternary operator (? – conditional operator).
– E.g. a>b ? a : b
Operators
• C++ provides operators to operate on variables
and constants
• Operators are a set of keywords and signs..
signs.
• Includes
– Assignment operator(=)
– Arithmetic operators ( +, -, *, /, % )
– Compound assignment operators (+=, -=, *=, /=, %=)
– Relational operators ( ==, !=, >, <, >=, <= )
– Logic operators ( !, &&, || )
– Conditional operator ( ? )
– The Increment and Decrement Operators(++, --)
Assignment operator(=)
• It serves to assign a value to a variable.
a = 5;
assigns the integer value 5 to variable a.
• lvalue must always be a variable whereas
• rvalue can be either a constant,
constant a variable,
variable the result of
an operation or any combination of them.
• For e.g.
a = b;
– assigns to variable a (lvalue) the value that contains variable b
(rvalue) independently of the value that was stored in a at that
moment.
…. Assignment operator(=)