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

Escape Sequences

11 Aryaf Aladwan
Variables

• Variables
– Location in memory where value can be stored
– Correspond to actual locations in computer's memory
– Every variable has name, type, size and value
Name Value

data type
– Syntax:
data_type variable_name;
int x ;
– When new value placed into variable, overwrites previous
value

14 Aryaf Aladwan
Cont.
• Variables
– Variable names
• Valid identifier
– Series of characters (letters, digits, underscores)
– Cannot begin with digit
– Neither spaces nor symbols are allowed
– Cannot match any keyword of c++ language such as : if,
for, while, const ….etc
– Case sensitive
Examples of variable names:
1) x
2) student_number
3) X423
4) _m
5) M_
6) Myclass
7) thisis4you

15 Aryaf Aladwan
C++ data types
Name Description Size Range

char Character or small integer. 1byte signed: -128 to 127


unsigned: 0 to 255
short int Short Integer. 2bytes signed: -32768 to 32767
unsigned: 0 to 65535
int Integer. 4bytes signed: -2147483648 to 2147483647
unsigned: 0 to 4294967295
long int Long integer. 4bytes signed: -2147483648 to 2147483647
unsigned: 0 to 4294967295
bool Boolean value. It can take one of 1byte true or false
two values: true or false.
float Floating point number. 4bytes +/- 3.4e +/- 38 (~7 digits)

double Double precision floating point 8bytes +/- 1.7e +/- 308 (~15 digits)
number.
long Long double precision floating 8bytes +/- 1.7e +/- 308 (~15 digits)
double point number.

19 Aryaf Aladwan
Variable Initialization

1) In the declaration:
int x = 5;
float r = 6.1;
char c = 'a' ;
int x ( 5 ); ➔ constructor initialization
2) After declaration:
int x , y , z;
x = 5;
y = 8;
z = x + y;
22 Aryaf Aladwan
Variable Initialization
• Correct and wrong assignments:
1) int x = 4, y = 5;
2) int x = y = 4; error
int y; int x=y=4;
3) int x , y , z;
x = y = 2;
x = 2 = y; error
y = ( x = 2);
z = ( x = 2) + 100;
4) char letter = 'ab' ; error
5) float d = 1.2 , d = 4.6; error
6) double d45 = 0.0 ;
6) int float z; error
7) int w-k; error

23 Aryaf Aladwan
26 Aryaf Aladwan
C++ Operators
• Arithmetic calculations
– *
• Multiplication
– /
• Division
• Integer division truncates remainder
– 5 / 2 evaluates to 2
– 5.0 / 2.0 evaluates to 2.5
– Division by zero cause an error
– %
• Modulus operator returns remainder
– 7 % 5 evaluates to 2
– 7.4 % 5.4 ➔ error

43 Aryaf Aladwan
30 Aryaf Aladwan
Constant Variables
• Constants are expressions with a fixed value, they are
treated just like regular variables except that their values
cannot be modified after their definition.
• Example 1

#include <iostream.h>
void main()
{
const int x = 2;
x = x + 10; Error cannot modify a
constant variable
cout<< x ;
}

34 Aryaf Aladwan
35 Aryaf Aladwan
36 Aryaf Aladwan
37 Aryaf Aladwan
C++ Operators

1) Arithmetic operators
2) Assignment operators
3) Compound Assignment
4) Relational and Equality operators
5) Logical operators
6) Increment and decrement operators

41 Aryaf Aladwan
Arithmetic operators

Operator Operation Example

+ Addition X+y

- Subtraction X–y

* Multiplication X*y

/ Division X/y

% Modulus (int type only) X%y

42 Aryaf Aladwan
44 Aryaf Aladwan
45 Aryaf Aladwan
46 Aryaf Aladwan
47 Aryaf Aladwan
Compound Assignment
operator expression Is equivalent to
+= x+=y x=x+y
-= a-=5 a=a-5
/= a/=b a=a/b
*= x*=y x = x* y
#include<iostream.h>
void main()
{ Output
int a=5 , b=3; 7
12
cout<<(a+=2) <<endl;
0
cout<<(b*=4) <<endl; 7
cout<<(a/=b) <<endl;
cout<< (a%=b) <<endl;
} Aryaf Aladwan
53
Relational and equality operators

Operator Operation Example ( a=2,b=3,c=6)


== Equal to ( a = = 5) ➔ false
!= Not equal to ( a ! = 2) ➔ false
> Greater than (b+4 > a*c) ➔ false
< Less than ( c < 7) ➔ true
>= Greater than or ( a* b >= c) ➔ true
equal
<= Less than or equal ( c<=6) ➔ true

55 Aryaf Aladwan
56 Aryaf Aladwan
Logical Operators
operator operation example
! NOT ! ( 5= = 5) ➔ false
&& AND (( 5 = = 5) && ( 3 > 6)) ➔false
|| OR (( 5 = = 5) || ( 3 > 6)) ➔ true

A B A&&b A B A || b
True True True True True True
True False False True False True
False True False False True True
False False False False False False
57 Aryaf Aladwan
Increment / Decrement operators

• The increment operator (++) increases the value of


its variable by one.
• The decrement operator (--) decreases the value
stored in its variable by one.
• a++ is the same as a+=1 or a=a+1
• b-- is the same as b-=1 or b=b-1
• Can be written as :
1) Prefix ➔ ++a , --a
2) Suffix ➔ a++ , a--

63 Aryaf Aladwan
68 Aryaf Aladwan
69 Aryaf Aladwan
Operator Precedence
• Rules of operator precedence
– Operators in parentheses evaluated first
• Nested/embedded parentheses
– Operators in innermost pair first
– Operators applied from left to right
operator operations
() Parentheses
! Logical NOT
*,/,% Multiplication , division and modulus
+,- Addition and subtraction
> , < , >= , <= Relational operators
= = , != Equal to , Not equal to
&& Logical AND
|| Logical OR
/=, *= , += , -=, %= Compound assignment

72 Aryaf Aladwan
73 Aryaf Aladwan
74 Aryaf Aladwan

You might also like