Upload
ajithkk199
View
234
Download
0
Embed Size (px)
Citation preview
8/12/2019 Variables Types Operators
1/31
Variables, Types & Operators
Object-Oriented Programming in C++
(ICTCO10306)
1
8/12/2019 Variables Types Operators
2/31
Simple C++ Program#include
using namespace std; //
int main()
{
// Declarations
// Statements
return 0;}
Fromlastweek
2
8/12/2019 Variables Types Operators
3/31
Sample C++ Program#include
using namespace std; void main()
{
int number;
cout number;cout
8/12/2019 Variables Types Operators
4/31
Did you try this?
Problem: To determine the average of threenumbers
Task: Request, from the user, three numbers,compute the average of the three numbers,and print out the original values and thecomputed average
4
8/12/2019 Variables Types Operators
5/31
Declaring a Variable
Variable provides you a named storage in Computer Memory
Declaring a variableint number;
5
Type Identifier (This is the name we give toThe variable)
8/12/2019 Variables Types Operators
6/31
Identifiers
Valid Identifierssequence of one or more letters, digits or
underscore characters (_)Neither spaces nor punctuation marks orsymbols can be part of an identifier
Are there invalid identifiers?
6
8/12/2019 Variables Types Operators
7/31
8/12/2019 Variables Types Operators
8/31
Reserved words You cannot use any of these as an identifierasm, auto, bool, break, case, catch, char, class,const, const_cast, continue, default, delete, do,double, dynamic_cast, else, enum, explicit, export,extern, false, float, for, friend, goto, if, inline, int,long, mutable, namespace, new, operator, private,protected, public, register, reinterpret_cast, return,short, signed, sizeof, static, static_cast, struct,
switch, template, this, throw, true, try, typedef,typeid, typename, union, unsigned, using, virtual,void, volatile, wchar_t, while
8
8/12/2019 Variables Types Operators
9/31
Identifiers
Identifiers are case sensitiveExample
Int year;Int Year; // these are identified as two differentvariables by C++ compiler
Always try to give meaningful phrases as identifiers
9
8/12/2019 Variables Types Operators
10/31
Data type for the variable
Try this in C++ using int variables A = 5
B = 2C = A / B ?
What data type is suitable for this application?
10
8/12/2019 Variables Types Operators
11/31
Fundamental Data Types in
C++
Try again with the suitable data type
Source http://www. cplusplus.com 11
8/12/2019 Variables Types Operators
12/31
Signed (+/-) Vs. UnsignedExamples
unsigned short int number_of_people;
signed int my_mccount_balance; //is thiscorrect?short int Year;
12
8/12/2019 Variables Types Operators
13/31
String of Characters as a
single variableWhat is a string
Set of characters placed in order
A special type of variable to store a completestring of charactedsCannot be handled by
Iostream.h
You need < string.h >
13
8/12/2019 Variables Types Operators
14/31
Initializing a Variable
Why Initialize?There can be stray values / residuals from
previous programsExample:int a = 0;
It is a good programming practice to intialize avariable as soon as you declare it
14
8/12/2019 Variables Types Operators
15/31
Initializing a Variable
How do you initialize a variable?Int a = 5;
Int b(5);Both methods are valid in c++
Variables a and b will be initialized to value 5
15
8/12/2019 Variables Types Operators
16/31
Where do you declare a variable?
1. Definitely before you use them1. Within the main function2. Before the main functionExample:#include
short unsigned int year = 2013;
int main()
{
short unsigned int month;
month = 11;
cout
8/12/2019 Variables Types Operators
17/31
Scope of a variable
short unsigned int year = 2013;This is a Global Variable (Visible everywhere in
the program)short unsigned int month;This is a local variable (Can use only in mainfunction)
17
8/12/2019 Variables Types Operators
18/31
Example:#include Using namespace std;short unsigned int year = 2013;// Global variableint main()
{short unsigned int month;// Local variablemonth = 11;cout
8/12/2019 Variables Types Operators
19/31
Operators in C++
Name the operators you already know
Exampleint a,b;a = 2 + (b = 5);a = b = 5;
19
8/12/2019 Variables Types Operators
20/31
Arithmetic operators
+ Addition- Subtraction* Multiplication
/ Division% Modulo division (Remainder of the division)
20
8/12/2019 Variables Types Operators
21/31
Compound AssignmentUsing these you can reduce length of codesUsed to modify value of a variable
21
expression is equivalent tovalue += increase; value = value + increase;
a -= 5; a = a - 5;
a /= b; a = a / b;
price *= units + 1; price = price * (units +1);
8/12/2019 Variables Types Operators
22/31
Increase and decrease(++, --)
Also called increment and decrement operatorsExample
C++;++C; // These are different operations
22
Example 1 Example 2
B=3;A=++B;// A contains 4, B contains 4
B=3;A=B++;// A contains 3, B contains 4
8/12/2019 Variables Types Operators
23/31
Relational and equality operators
Comparison between values or variables
Do not confuse between == and = operatorsEvaluate the expression: ((b=2) == a)
23
== Equal to
!= Not equal to
> Greater than
< Less than>= Greater than or equal to
8/12/2019 Variables Types Operators
24/31
Relational and equality operators
Comparison between values or variables
Do not confuse between == and = operatorsEvaluate the expression: ((b=2) == a)
24
== Equal to
!= Not equal to
> Greater than
< Less than>= Greater than or equal to
8/12/2019 Variables Types Operators
25/31
Comma Operator
A = (B=3, B+2);1. Will assign B=3;2. Then evaluate a = B+2
This is equivalent toB=3;
A=B+2;
25
8/12/2019 Variables Types Operators
26/31
Logical Operations: And(&&)
A && BExpression is Trueiff A and B areboth true
T F
T T F
F F F
26
8/12/2019 Variables Types Operators
27/31
Logical Operations: Or(||)
A || BExpression is Trueif either A or B areTrueNote: Also True
when A and B areboth True
T F
T T T
F T F
27
8/12/2019 Variables Types Operators
28/31
8/12/2019 Variables Types Operators
29/31
Logical Operations: Exercises
A = 1, B = 1, C = 01. A || B2. A && C3. A || B && C
4. ( (5 == 5) && (3 > 6) ) 29
8/12/2019 Variables Types Operators
30/31
Precedence of operators
30
Level Operator Description Grouping
1 :: scope Left-to-right
2() [] . -> ++ -- dynamic_cast static_castreinterpret_cast const_cast typeid
postfix Left-to-right
3
++ -- ~ ! sizeof new delete unary (prefix)
Right-to-left* &indirectionand reference(pointers)
+ -unary signoperator
4 (type) type casting Right-to-left
5 .* ->* pointer-to-member
Left-to-right
6 * / % multiplicative Left-to-right
7 + - additive Left-to-right
8 > shift Left-to-right
8/12/2019 Variables Types Operators
31/31
Precedence of operators
31
9 < > = relational Left-to-right
10 == != equality Left-to-right
11 & bitwise AND Left-to-right
12 ^ bitwise XOR Left-to-right
13 | bitwise OR Left-to-right
14 && logical AND Left-to-right
15 || logical OR Left-to-right
16 ?: conditional Right-to-left
17= *= /= %= += -= >>=