Chapter 2
Chapter 2
• Includes
– short
– unsigned short
– Int
– unsigned int
– Long
– unsigned long
data types
Floating-Point Data
Types
• Are data type that allows fractional
values.
values
– If you are writing a program that works with
dollar amounts or precise measurements
you need floating point data types
• In C++ there are three data types that
can represent floating-point numbers.
• They are
– float
– double
– long double
Floating-Point Data
•
Types
Internally, floating-point numbers are stored in a
manner similar to scientific notation.
notation
– In scientific notation the number 47,281.97 is
4.728197x104.
• Computers typically use E notation to represent
floating-point values.
– In E notation, the number 47,281.97 would be
4.728197E4. The part of the number before the E is the
mantissa,
mantissa and the part after the E is the power of 10.
10
• The following table shows other numbers
represented in scientific and E notation.
Char Data Type
• 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(=)
• For example, if we take this code :
int a, b; // a:? b:?
a = 10; // a:10 b:?
b = 4; // a:10 b:4
a = b; // a:4 b:4
b = 7; // a:4 b:7
will give us the result that the value
contained in a is 4 and the one
contained in b is 7.
• The final modification of b has not
affected a, (right-to-left rule).
Arithmetic operators
( +, -, *, /, %)
• The following table shows the common
arithmetic operators in C++.
…. Arithmetic operators
( +, -, *, /, %)
• The addition operator returns the sum of its
two operands.
– Here, the variable amount will be assigned the value 12:
12
amount = 4 + 8;
• The subtraction operator returns the value of
its right operand subtracted from its left operand.
– This statement will assign the value 98 to temperature:
temperature = 112 - 14;
• The multiplication operator returns the
product of its two operands.
– In the following statement, markUp is assigned the value
3:
markUp = 12 * 0.25;
…. Arithmetic operators
( +, -, *, /, %)
• The division operator returns the
quotient of its left operand divided by its
right operand.
– In the next statement, points is assigned the
value 5:
points = 100 / 20;
• The modulus operator,
operator which only
works with integer operands,
operands returns the
remainder of an integer division.
– The following statement assigns 2 to leftOver:
leftOver = 17 % 3;
Compound assignment
•
operators (+=, -=,
The following table shows the combined
*=, /=,
assignment %=) also known as compound
operators,
operators
operators or arithmetic assignment operators.
operators
….. Compound assignment
operators (+=, -=,
• Programs may have assignment
*=, /=, %=)
statements of the following form:
number = number + 1;
– On the right-hand side of the assignment
operator, 1 is added to number.
– The result is then assigned to number,
number
replacing the value that was previously stored
there.
– Effectively, this statement adds 1 to number.
• In a similar fashion, the following
statement subtracts 5 from number.
number
number = number – 5;
….. Compound assignment
operators (+=, -=,
• Examples of statements: (Assume x = 6)
*=, /=, %=)
Relational operators
( ==, !=, >, <, >=,
<= ) allow you to compare numeric
• They
values and determine if one is
greater than,
than less than,
than equal to,
to or
not equal to another.
• For example,
– the greater-than operator (>)
determines if a value is greater than
another.
– The equality operator (==) determines if
two values are equal.
…. Relational operators
( ==, !=, >, <, >=,
• The following table lists all of C++’s
<= )
relational operators.
…. Relational operators
( ==, !=, >, <, >=,
• All the relational operators are binary
<=operators
)
– They use two operands.
operands
• Here is an example of an expression using
the greater-than operator:
x>y
– This expression is called a relational expression.
expression
– It is used to determine if x is greater than y.
y
• The following expression determines if x is
less than y:
y
x<y
…. Relational operators
( ==, !=, >, <, >=,
• Relational expressions are Boolean expressions
<=– which
) means their value can only be true or false.
false
• If x is greater than y,
y
– the expression x>y will be true,
true while
– the expression y==x will be false.
false
• The == operator determines if the operand on
its left is equal to the operand on its right.
right
– If both operands have the same value,
value the expression
is true.
true
– Assuming that a is 4,
4 the following expression is true:
true
a == 4
– But the following is false:
false
a == 2
…. Relational operators
•
( ==, !=, >, <, >=,
The >= operator determines if the operand on its
<=
left )
is greater
right.
right
than or equal to the operand on the
– Assuming that a is 4, b is 6, and c is 4,
4 both of the following
expressions are true:
true
b >= a
a >= c
– But the following is false:
false
a >= 5
• The <= operator determines if the operand on its
left is less than or equal to the operand on its right.
right
– Assuming that a is 4,
4 b is 6,
6 and c is 4,
4 both of the following
expressions are true:
true
a <= c
b <= 10
– But the following is false:
false
b <= a
…. Relational operators
( ==, !=, >, <, >=,
• The != operator is the not-equal operator.
operator
• It<= )
determines if the operand on its left is not
equal to the operand on its right.
right
• Is the opposite of the == operator.
• As before, assuming a is 4, b is 6,
6 and c is 4,
4
– both of the following expressions are true because
a is not equal to b and b is not equal to c:
c
a != b
b != c
– But the following expression is false because a is
equal to c:
a != c
…. Relational operators
•
( ==, !=, >, <, >=,
Examples of relational expressions and their true
<=false)values. (Assume x is 10 and y is 7.)
or
Logic operators (&&, ||,
• ! )connect two or more relational expressions
They
into one or reverse the logic of an expression.
• The following table lists C++’s logical operators.
… Logic operators (&&,
•
||, ! )
The && operator is known as the logical AND operator.
• It takes two expressions as operands and creates an
expression that is true only when both sub-expressions
are true.
true
…. Logic operators (&&,
•
||, ! )
The || operator is known as the logical OR operator.
• It takes two expressions as operands and creates an
expression that is true when either of the sub-
expressions are true.
true
…. Logic operators (&&,
||, ! )
• The ! operator performs a logical NOT operation.
• It takes an operand and reverses its truth or
falsehood.
falsehood
• In other words, if the expression is true,
true the !
operator returns false,
false and if the expression is false,
false
it returns true.
true
…. Logic operators (&&,
||, ! ) a b a && b a || b