( 1 – حال201( حاسب آلـى: المقـرر
Lecture # 3
Basics of C++
KEYWORDS & OPERATORS
(Keywords, Data Types and Operators)
1
( 1 – حال201( حاسب آلـى: المقـرر
INTRODUCTION TO KEYWORDS & OPERATORS
1. What are keywords?
In C++ reserves a set of 63 words for its own use. These words are called keywords, and
each of these keywords has a special meaning within the C++ language.
asm const else friend new short this
unsigned
auto const_cast enum goto operator signed throw
using
bool continue explicit if private sizeof true
virtual
break default export inline protected static try
void
case delete extern int public static_cast typedef
volatile
catch do false long register struct typeid
wchar_t
char double float mutable reinterpret_cast switch typename
while
class dynamic_cast for namespace return template union
2. Fundamental data types
Most programming languages require the programmer to declare the data type of every
data to specify the type of each data field. When programming, we store the variables in our
computer's memory, but the computer has to know what kind of data we want to store in them.
a. Integer
An integer type variable is a variable that can only hold whole numbers (eg. -2, -1, 0, 1, 2).
C++ actually has four different integer variables available for use: char, short, int, and long.
The only difference between these different integer types is that they have varying sizes.
In the following tutorials, we will typically assume:
Name Description Size
char Character 1byte
short Short Integer 2 byte
int Integer 4 byte
2
( 1 – حال201( حاسب آلـى: المقـرر
long int Long Integer 4 byte
b. Floating point numbers
Integers are great for counting whole numbers, but sometimes we need to store very large
numbers, or numbers with a fractional component. A floating point type variable is a variable
that can hold a real number, such as 4.0, 2.5, 3.33, or 0.1226. There are three different floating
point data types.
Name Size
Float 4 byte
Long Float 8 byte
Double Long 8 byte
3. Arithmetic operator
Operator Symbol Form Operation
Addition + x+y x plus y
Subtraction - x–y x minus y
Multiplication * x*y x multiplied by y
Division / x/y x divided by y
Modulus The remainder of x divided by
% x%y
(Remainder) y
3
( 1 – حال201( حاسب آلـى: المقـرر
4. Increment & Decrement Operators
Incrementing (adding 1 to) and decrementing (subtracting 1 from) a variable are so
common that they have their own operators in C. There are actually two versions of each
operator — a prefix version and a postfix version.
Operator Symbol Form Operation
Prefix increment ++ ++x Increment x, then evaluate x
Prefix decrement –– – –x Decrement x, then evaluate x
Postfix increment ++ x++ Evaluate x, then increment x
Postfix decrement –– x– – Evaluate x, then decrement x
5. Relational Operators
Operator Symbol Form Operation
Greater than > x>y true if x is greater than y, false otherwise
Less than < x<y true if x is less than y, false otherwise
Greater than or true if x is greater than or equal to y,
>= x >= y
equals false otherwise
Less than or true if x is less than or equal to y, false
<= x <= y
equals otherwise
Equality == x == y true if x equals y, false otherwise
true if x does not equal y, false
Inequality != x != y
otherwise
4
( 1 – حال201( حاسب آلـى: المقـرر
6. Logical Operators
While relational operators can be used to test whether a particular condition is true or false,
they can only test one condition at a time.
C++ provides us with 3 logical operators.
Operator Symbol Form Operation
Logical NOT ! !x true if x is false, or false if x is true
Logical AND && x && y true if both x and y are true, false otherwise
Logical OR || x || y true if either x or y are true, false otherwise
a. Logical NOT
Logical NOT (operator !)
Right operand Result
0 1
1 0
b. Logical OR
The logical OR operator is used to test whether either of two conditions is true. If the left
operand evaluates to true, or the right operand evaluates to true, the logical OR operator returns
true. If both operands are true, then logical OR will return true as well.
5
( 1 – حال201( حاسب آلـى: المقـرر
Logical OR (operator ||)
Left Right
Result
operand operand
0 0 0
0 1 1
1 0 1
1 1 1
c. Logical AND
The logical AND operator is used to test whether both conditions are true. If both
conditions are true, logical AND returns true. Otherwise, it returns false.
Logical AND (operator &&)
Left operand Right operand Result
0 0 0
0 1 0
1 0 0
1 1 1