Lec2+lec3

351 views

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
351
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Lec2+lec3

  1. 1. Variable declaration & initialization
  2. 2. • Declaration: int x; ↙ ↘ data type identifier name(for more than one variables separate them with commas)• Initialization: x=9;(value is stored )
  3. 3. • Declaration & initialization in one step: int x=9;• = is an assignment operatorInt x,y=6,z=2;x=y; ////x=6y=z; ///y=2z=x; ///z=6
  4. 4. • Its associativity is right (it means right side of =(assignment operator )is evaluated first.)Keywords/reserve words:• Lower case letters• E.g for,while,int,char,floatVariable:• It can be modified. they are used to store constant values• int→2 bytes e.g 1,2,3 etc• float → 4 bytes e.g 1.1,2.3 etc• char →1 byte e.g A,S,D, 1 ,2,3, +,*,& etc
  5. 5. #include<iostream.h>main(){cout<<“hello” ;}
  6. 6. Operators• Is anything that perform some action on some variable or constant1.Unary operators2.Binary operators3.Arithmatic operators1.Unary operators:++, -- ,+ , -They increment the value by 1.(a)Unary minus operator:-It is used to reverse the sign of a value.it makes a +ve value to –ve value and vice versa.e.gint a, b;a=87;b=-a;It has higher precedence over binary operator.
  7. 7. 1.Increment operator:(A)Prefix increment:b=++aa=a+1b=a(B)postfix increment:b=a++b=aa=a+12.Decrement operator:(A)Prefix decrement:--yb=--aa=a-1b=a
  8. 8. (B) postfix decrement:b=a--b=aa=a-12.Binary operators:• The operators that operate on two data items.• E.g multiplication etc3.Arithmatic operators: +, - ,* ,/ , %• Remainder operator :modulus
  9. 9. Order of precedence• * , / ………………….left to right• + , - ………………….left to right• ( ) parenthesis ………………….left to right• Parenthesis within parenthesis(innermost first)• 1+2+3-4+5• 7+c*d/c• a/b/c• 2+a/b*5• 6+12/6*2-1
  10. 10. Type casting:Conversion of one data type of numeric value to another1.Implicit type casting2.Explicit type castingArithmatic expressions:2(a+b) →2*(a+b)5/9(F-32) →5/9*(F-32)
  11. 11. Assignment operators: =• Variable=expressionint a, b, ca=200 ,b=100c=a;a=b;b=c;
  12. 12. • Compound assignment: x=y=6;Relational operator:• Also called comparison operator• After comparing two variables or expressions results into 0 /1 or true /false• ==• !=• <• <=• >• >=
  13. 13. Logical operator:1. and operator && (.)2. or operator || (+)3. not operator ! (-)
  14. 14. Conditional operator:• It consists of “?” and “:”.• Its syntax is {condition}?{exp1}:{exp2}WhereCondition…..represents test conditionExp1 & exp2….represents two expressions which may be arithmetic expression or constant value.
  15. 15. Sizeof operator:void main(){int x;cout<<sizeof(x);}2

×