Professional Documents
Culture Documents
C Chapter1-1
C Chapter1-1
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
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
+ Addition X+y
- Subtraction X–y
* Multiplication X*y
/ Division 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
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
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