Lecture 1(c).

Download Report

Transcript Lecture 1(c).

Lecture 1
Variable, Operators and
Expressions
Variable
• In C++ a variable is a place to store
information.
• A variable is a location in your computer
memory in which you can store a value and
from with you can later retrieve the value.
• Every variable has a name, type and value
• When a variable in C++ is defined, you
must tell the compiler what kind of
variable is it. (integer, character etc)
• This information tells the compiler how
much room to set aside and what kind of
value you want to store in your variable.
Basic Data Types
• Following are the five Basic Data Types:
Size
–
–
–
–
–
Int
char
float
double
void
TYPE int
• Stores only numeric data
• It cannot store decimal numbers with
fractional part. E.g 1.4 and 14.0
• (-) sign must be written when using
negative number but with positive number
(+) sign is not mandatory.
• Leading zeros and commas cannot be used
when writing integers.
TYPE int(cont..)
• The storage space occupied by int data
type depends on the operating system,
compiler and microprocessor. (4bytes or
2bytes)
int sample values
4578
-4578
0
TYPE char
• Stores character data, which includes letters,
digits, and special symbols.
• Char data occupies 1byte of memory space
• A character variable can store only one character
at a time.
• Char must be enclose in single quotation marks.
char sample values
‘B’
‘d’
‘4’
‘?’ ‘*’
TYPE float and double
• To store decimal numbers, float data type
is used.
• Occupies 4 bytes.
• Floating value are precise up to 5-6 digits
where as double provides 10 digits
precision.
Naming Variables (identifier)
• A variable name is combination of
alphabet, digits, or underscore.
• The first character in the variable name
must be an alphabet.
• No commas or blanks are allowed within a
variable name.
• No special symbols other than underscore
can be used in a variable name.
Correct or Incorrect Name
•
•
•
•
•
•
•
•
X
Abc124
My variable
124abc
My ^ Variable
My_Variable
MyVariable
My Varibale!
What Does a Variable Declaration Do?
int ageOfDog;
A declaration tells the compiler to allocate
enough memory to hold a value of this data type,
and to associate the identifier with this location.
4 bytes for ageOfDog
Syntax for Declarations
Variable Declaration
Datatype (variable) TypeName Identifier , Identifier . . ;
Example1
Variable Declaration and Initialization
int main( )
{
int x=5;
int y;
y=5;
cout<< “The value of x and y”<<x<<y;
}
What is initialization :
Example 2
Receiving keyboard input and screen output
//include iostream + conio header files
int main( )
{
int myage;
cout<<“ Enter your age”;
cin>>myage;
cout<<myage;
}
Constants
• A constant is the quantity that does not
change. This quantity can be stored at a
location in the memory.
• A variable can be considered as the name
given to the location in memory where
constant is stored.
• Key words : Reserve words for C
Memory Depiction
float y = 12.5;
y
12.5
1001
1002
1003
1004
Memory Depiction
float y = 12.5;
int Temperature = 32;
y
Temperature
12.5
32
1001
1002
1003
1004
1005
1006
Memory Depiction
float y = 12.5;
int Temperature = 32;
char Letter = 'c';
y
12.5
Temperature
Letter
32
'c'
1001
1002
1003
1004
1005
1006
1007
Memory Depiction
float y = 12.5;
y
int Temperature = 32;
char Letter = 'c';
Temperature
int Number;
Letter
Number
12.5
32
'c'
-
1001
1002
1003
1004
1005
1006
1007
1008
1009
Constants Keyword
#include <stdio.h>
const float PI = 3.14159;
int main()
{
printf("Hello, world\n %d", XYZ);
getch();
return 0;
}
OPERATORS
• In C++ you can combine variables and/or
numbers using the arithmetic operators.
Arithmetic in C
C operation
Algebraic
C
Addition(+)
f+7
f+7
Subtraction (-)
p-c
p-c
Multiplication(*)
bm
b*m
Division(/)
x/y, x ,x y
x/y
y
Modulus(%)
r mod s
r%s
y
Power (Exponent) x
x^y
Assignment Statement
• The assignment statement takes the form:
variable = expression;
• Expression is evaluated and its value is assigned to the
variable on the left side
• In C++ = is called the assignment operator.
• Integer division truncates remainder
• Variable = 7 / 5 ;
• 7 / 5 evaluates to 1
Modulus operator returns the remainder
• 7 % 5 evaluates to 2
• SomeVar = 7 % 5;
Definition
int NewStudents = 6;
NewStudents
6
Definition
int NewStudents = 6;
int OldStudents = 21;
NewStudents
6
OldStudents
21
Definition
int NewStudents = 6;
int OldStudents = 21;
int TotalStudents;
NewStudents
6
OldStudents
21
TotalStudents
-
Assignment Statement
int NewStudents = 6;
int OldStudents = 21;
int TotalStudents;
NewStudents
6
OldStudents
21
TotalStudents
?
TotalStudents = NewStudents + OldStudents;
Assignment Statement
int NewStudents = 6;
int OldStudents = 21;
int TotalStudents;
NewStudents
6
OldStudents
21
TotalStudents
27
TotalStudents = NewStudents + OldStudents;
Assignment Statement
int NewStudents = 6;
int OldStudents = 21;
int TotalStudents;
NewStudents
6
OldStudents
?
TotalStudents
27
TotalStudents = NewStudents + OldStudents;
OldStudents = TotalStudents;
Assignment Statement
int NewStudents = 6;
int OldStudents = 21;
int TotalStudents;
NewStudents
6
OldStudents
27
TotalStudents
27
TotalStudents = NewStudents + OldStudents;
OldStudents = TotalStudents;
Assignment Statement
sum = 5 + 3;
sum= num1 + num2;
sum= 2 + num1;
More Assignment Statement
• There is a shorthand notion that combines
the assignment operator (=) and an
arithmetic operator so that a given variable
can have its value changed by adding,
subtracting, multiplying, or dividing by a
special value.
– Variable Operator = Expression
– Same as
• Variable = Variable Operator (Expression)
Examples
•
•
•
•
count += 2; // count = count + 2;
count -= 2; // count = count - 2;
count *= 2;
count *= val1 + val2; // count = count*val1 + val2
• count =+ 2; // count = 2+ count;
Precedence order
• Highest to lowest
• ()
• *, /, %
• +, -
Example
Algebra:
z = pr%q+w/x-y
C:
z = p * r % q + w / x – y ;
Precedence:
1
2
4
3
5
Evaluate the Expression
7
*
10
-
5
%
3
*
4
+
9
Evaluate the Expression
7 * 10 (7 * 10) 70 70 70
70
5
5
5
(5
70
% 3 *
means
% 3 *
% 3 *
% 3) *
2
*
(2
*
8
(70 - 8)
62
What would be the precedence of parenthesis:
Evaluate (4+5)/5-10%4
4
4
4
4
4
4)
+
+
+
+
+
+
+
+
+
9
9
9
9
9
9
9
9
9
71
EXAMPLE :
Increment & Decrement Operators
• Increment operator: increment variable by 1
• Decrement operator: decrement variable by 1
• Pre-increment: ++variable
• Post-increment: variable++
• Pre-decrement: --variable
• Post-decrement: variable—
• Int x; :
++x;
: cout<<x++;
x++; : x = x + 1; : cout<<++x;
x--;
Decision Making
• Checking falsity or truth of a statement
• Equality operators have lower precedence
than relational operators
• Relational operators have same precedence
• Both associate from left to right
Decision Making
• Equality operators
• ==
• !=
• Relational operators
•
•
•
•
<
>
<=
>=
Summary of precedence order
Operator
()
* / %
+ < <= > >=
== !=
=
Associativity
left to right
left to right
left to right
left to right
left to right
left to right