Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

Basic Elements of C++

Lecture 2

Dr. Ghada Afify


Basic Elements of C++
2
Basic C++ Data Types

You need to use various variables to store various


information.

Variables are reserved memory locations to store values.

Based on the data type of a variable, the operating


system allocates memory.

A byte is the minimum amount of memory that we can


manage in C++.
Basic C++ Data Types

C++
Basic C++ Data types
FYI
char Data Type

The smallest integral data type

Used for characters: letters, digits, and special


symbols

Each character is enclosed in single quotes


'A', 'a', ' ', '0', '*', '+', '$', '&'
string Type

Sequence of characters

Enclosed in double quotation marks

Length of a string is number of characters in it

Example: length of "William Jacob" is 13


Boolean Data Type

bool type
Two values such as: true and false , 0 and 1, yes
and no.

true and false are called logical values.


C++ Operators Types

An operator is a symbol that tells the compiler to


perform specific mathematical or logical
manipulations.

1. Arithmetic Operators
2. Comparison Operators
3. Assignment Operators
C++ Arithmetic Operators

Operator Description Example (a = 6, b = 3)


+ It will add two operands. a+b=9

- It will subtract two operands. a-b=3

* It will multiply two operands. a * b = 18

/ It divides two numbers and returns a a/b=2


floating-point result.
\ It divides two numbers and returns an a\b=2
integer result.
Mod It divides two numbers and returns only a Mod b = 0
the remainder.
^ It raises a number to the power of a ^ b = 216
another number.
Order of Precedence Arithmetic Operators

1. Any operation inside parentheses ( )


2. Power ( ^ )
3. Multiplication and division (* and / )
4. Integer division ( \ )
5. Modulus (%)
6. Addition and subtraction (+ and - )

When operators are on the same level compiler will Performed


them from left to right.
Ex:
(((3 * 7) – 6) + ((2 ^ 5) / 4 )) + 6
Comparison/ Relational Operators

Example (a =
Operator Description 10, b = 5)
< It will return true if right operand greater than left a < b = False
operand.
<= It will return true if right operand greater than or a <= b = False
equal to the left operand.
> It will return true if left operand greater than the a > b = True
right operand.
>= It will return true if left operand greater than or a >= b = True
equal to the right operand.
= It will return true if both operands are equal. a = b = False
!= It will return true if both operands are not equal. a != b = True
Assignment Operators

Operator Description Example


= It will assign a value to a variable or a = 10
property.
+= It will perform the addition of left and right a += 10 equals to
operands. a = a + 10
-= It will perform a subtraction of left and right a - = 10 equals to
operands. a = a - 10
*= It will perform a multiplication of left and a *= 10 equals to
right operands. a = a * 10
/= It will perform a division of left and right a /= 10 equals to
operands. a = a / 10
^= It will raise the value of a variable to the a ^= 10 equals to
power of expression. a = a ^ 10
Expressions

integral expression : If all operands are integers.


Example: 2 + 3 * 5

floating-point expression : If all operands are floating-


point.
Example: 12.8 * 17.5 - 34.50

Mixed expression: Contains integers and floating-point.


Example: 2 + 3.5
6 / 4 + 3.9
Input

Data must be loaded into main memory before it


can be manipulated

Storing data in memory is a two-step process:


Instruct computer to allocate memory
Include statements to put data into memory
Allocating Memory with Constants (cont.)

Constant (identifier): memory location whose


content can’t change during execution.
The syntax to declare a named constant is:
Allocating Memory with Variables

Variable (identifier): memory location whose content may


change during execution.
The syntax to declare a named variable is:
Naming Identifiers

1. Only Consist of letters, digits, and the underscore


character (_) (without any spaces).
2. Must begin with a letter or underscore.
3. Do not use any of a C++ Reserved keywords.
4. Avoid run-together words, Capitalize the beginning of
each new word- AnnualSale
5. C++ is case sensitive
− NUMBER is not the same as number
Naming Identifiers (cont.)

The C++ language is a "case sensitive" language.


That means that an identifier written in capital
letters is not equivalent to another one with the
same name but written in small letters.
For example:
RESULT variable is not the same as
result variable or
Result variable
These are three different variable identifiers.
Some C/C++ Reserved Keywords

FYI
21

Naming Identifiers (cont.)

The following are legal identifiers in C++:


• first
• e6788d
• PayRate
• Pay_Rate
Illegal Identifier
22

Illegal Reason Suggested Identifier


Identifier
my age Blanks are not myAge
allowed
2times Cannot begin times2 or
with a number twoTimes
four*five * is not allowed fourTimesFive

time&ahalf & is not timeAndAHalf


allowed
Exercise

Identifier Valid? Identifier Valid?

sumX2 valid Net-salary invalid

Hourly_rate valid 4grade invalid

Gross Pay invalid numStudents valid

“X” invalid while invalid

name@ invalid Employee’s invalid

_myVariable valid 20morrow invalid

@address invalid Final_grade_90 valid


Variables Declaration

Variable must be declared (defined) before they are used.


in C++ they can be declared in the middle of program, but must
be defined before using them.

Ex: int student_marks; // Variable declared


int i, j, k; // Multiple declaration
Variable Initialization

There are two ways to initialize a variable:


int std_marks;
By using the assignment statement
Std_marks= 35;
By using a read statement
cin >> Std_marks;

All variables must be initialized before they are used


in operations.
But not necessarily during declaration
Input (Read) Statement

cin >> is used to gather input.

cin >> student_name;

Using more than one variable in cin allows more


than one value to be read at a time
cin >> student_name >> Marks;
Output statement

The syntax of cout and << is:


Prompt Lines

Prompt lines: executable statements that inform the


user what to do
cout << "Please enter a number between 1 and 10 and "
<< "press the return key" << endl;
cin >> num;
Example: Write a Program to Add Two Integers.
Example: Write a Program to calculate area of cycle.
Example: Write a Program to calculate the following arithmetic
operation (y*y+2*y-y/3+23).
Escape Characters

\
Indicates “special” character output
Escape
Value
Sequence
\n newline
\t horizontal tab
\b backspace
\r carriage return
\' single quote (')
\" double quote (")
\? question mark (?)
\\ backslash (\)
Compound Assignment Operators

Compound assignment (+=, -=, *=, /=)


When we want to modify the value of a variable by
performing an operation on the value currently stored in
that variable; we can use compound assignment operators:
Increase/Decrease Operators

The increase operator (++) and the decrease


operator (--) increase or reduce by one the value
stored in a variable
x++; x--;
x=x+1; x=x-1;
x+=1; x-=1;
are all equivalent in functionality
also called increment/decrement operators
unary operators: works on one operand
Increase/Decrease Operators

In Example 1, B is increased before its value is


copied to A.
In Example 2, the value of B is copied to A and
then B is increased.
Increase/Decrease Operators
Increase/Decrease Operators
Thank You

You might also like