Вы находитесь на странице: 1из 32

EEC 124

Computer Engineering II

Lecture 3
Program 1

Computer Engineering II Dr. Mohamed A. Torad 2


Program 1

Computer Engineering II Dr. Mohamed A. Torad 3


Identifiers
Identifier: A C++ identifier consists of letters, digits, and the underscore
character (_ ) and must begin with a letter or underscore.

Computer Engineering II Dr. Mohamed A. Torad 4


Data Types

Computer Engineering II Dr. Mohamed A. Torad 5


Simple Data Types
There are three categories of simple data:
1. Integral, which is a data type that deals with integers, or numbers without a
decimal part
2. Floating-point, which is a data type that deals with decimal numbers
3. Enumeration, which is a user-defined data type

Integral data types are further classified into the following nine categories:
char, short, int, long, bool, unsigned char, unsigned short, unsigned int, and
unsigned long.

Computer Engineering II Dr. Mohamed A. Torad 6


Floating-Point Data Types

C++ provides three data types to manipulate decimal numbers: float, double,
and long double.

Computer Engineering II Dr. Mohamed A. Torad 7


Arithmetic Operators and Operator Precedence
Arithmetic Operators:
- + (addition)
- - (subtraction or negation)
- * (multiplication)
- / (division)
- % (mod, (modulus or remainder))

An arithmetic expression is constructed by using arithmetic operators and


numbers. The numbers appearing in the expression are called operands.

Computer Engineering II Dr. Mohamed A. Torad 8


Arithmetic Operators and Operator Precedence

Computer Engineering II Dr. Mohamed A. Torad 9


Order of Precedence
When more than one arithmetic operator is used in an expression, C++ uses the
operator precedence rules to evaluate the expression.

According to the order of precedence rules for arithmetic operators,


*, /, %

are at a higher level of precedence than:


+, -

Note that the operators *, /, and % have the same level of precedence.

Similarly, the operators + and - have the same level of precedence.

Computer Engineering II Dr. Mohamed A. Torad 10


Order of Precedence

Computer Engineering II Dr. Mohamed A. Torad 11


Expressions
If all operands (that is, numbers) in an expression are integers, the expression is
called an integral expression.
If all operands in an expression are floating-point numbers, the expression is
called a floating-point or decimal expression.

Computer Engineering II Dr. Mohamed A. Torad 12


Mixed Expressions
An expression that has operands of different data types is called a mixed
expression. A mixed expression contains both integers and floating-point
numbers. The following expressions are examples of mixed expressions:
2 + 3.5
6 / 4 + 3.9
5.4 * 2 - 13.6 + 18 / 2
Two rules apply when evaluating a mixed expression:
1. When evaluating an operator in a mixed expression:
a. If the operator has the same types of operands (that is, either both integers or
both floating-point numbers.
b. If the operator has both types of operands (that is, one is an integer and the
other is a floating-point number), then during calculation, The result is a
floating point number.
2. The entire expression is evaluated according to the precedence rules; the
multiplication, division, and modulus operators are evaluated before the
addition and subtraction operators.
Computer Engineering II Dr. Mohamed A. Torad 13
Mixed Expressions

Computer Engineering II Dr. Mohamed A. Torad 14


Mixed Expressions

Computer Engineering II Dr. Mohamed A. Torad 15


Type Conversion (Casting)
When a value of one data type is automatically changed to another data type, an
implicit type coercion is said to have occurred.
To avoid implicit type coercion, C++ provides for explicit type conversion or
type casting through the use of a cast operator. Which takes the following
form:
static_cast<dataTypeName>(expression)

Computer Engineering II Dr. Mohamed A. Torad 16


Computer Engineering II Dr. Mohamed A. Torad 17
string Type
A string is a sequence of zero or more characters. Strings in C++ are enclosed
in double quotation marks. A string containing no characters is called a null or
empty string.

Computer Engineering II Dr. Mohamed A. Torad 18


Input
Allocating Memory with Constants and Variables:

Computer Engineering II Dr. Mohamed A. Torad 19


Input

Computer Engineering II Dr. Mohamed A. Torad 20


Assignment Statement

Computer Engineering II Dr. Mohamed A. Torad 21


Assignment Statement

Computer Engineering II Dr. Mohamed A. Torad 22


Input (Read) Statement

Computer Engineering II Dr. Mohamed A. Torad 23


Input (Read) Statement

Computer Engineering II Dr. Mohamed A. Torad 24


Computer Engineering II Dr. Mohamed A. Torad 25
Increment and Decrement Operators

Computer Engineering II Dr. Mohamed A. Torad 26


Increment and Decrement Operators
Suppose that x and y are int variables. Consider the following statements:
x = 5;
y = ++x;
The first statement assigns the value 5 to x. To evaluate the second statement,
which uses the pre-increment operator, first the value of x is incremented to 6,
and then this value, 6, is assigned to y. After the second statement executes,
both x and y have the value 6.
Now, consider the following statements:
x = 5;
y = x++;
As before, the first statement assigns 5 to x. In the second statement, the post-
increment operator is applied to x. To execute the second statement, first the
value of x, which is 5, is used to evaluate the expression, and then the value of
x is incremented to 6. Finally, the value of the expression, which is 5, is stored
in y. After the second statement executes, the value of x is 6, and the value of y
is 5.Computer Engineering II Dr. Mohamed A. Torad 27
Increment and Decrement Operators

Computer Engineering II Dr. Mohamed A. Torad 28


Output

Computer Engineering II Dr. Mohamed A. Torad 29


Output

Computer Engineering II Dr. Mohamed A. Torad 30


Output

Computer Engineering II Dr. Mohamed A. Torad 31


Output

Computer Engineering II Dr. Mohamed A. Torad 32

Вам также может понравиться