1 / 27

Lecture 5

Lecture 5. Sept 11, 2002. Operators & its types. An operator is a symbol or a special character that tells the computer to perform certain mathematical or logical operations which is applied to operands to give a result. Operators are used in programs to manipulate data and variables

flann
Download Presentation

Lecture 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 5 Sept 11, 2002

  2. Operators & its types • An operator is a symbol or a special character that tells the computer to perform certain mathematical or logical operations which is applied to operands to give a result. • Operators are used in programs to manipulate data and variables • Data and variables are called operands.

  3. Types of Operators • Assignment operators • Arithmetic operators • Increment and decrement operators • Logical operators • Relational operators • Equality operators • Conditional operators • sizeof operator • Bitwise operators ( not im p ) Self study • Special operators ( not imp ) Self study

  4. Arithmetic operators • + (Addition) • - (subtraction) • * (Multiplication) • / (Division) • % ( Modulo operation)

  5. Modulus operator • This is used to find the remainder of an integer division • eg 1. 9%4 is 1 2. 3%5 is 3 3. 9%3 is 0 • The modulus operator can be used only with integers.

  6. Rules in arithmeticoperations Rule # 1 • Declaration of a real data value as an integer results in truncation of fractional part to yield an integer • Problem 4: • #include<iostream.h> void main() { int a; a=3.5; cout<<a<<endl; } Output: 3

  7. Rule # 2 • An arithmetic operation between integer and integer yields always an integer. • Pb 5: #include<iostream.h> void main() { int a, b, c; a=6; b=3; c=b/a; cout<<c<<endl; } output: 2

  8. Rule # 3 • An arithmetic operation between real and real yields always a real result. • Problem 6: #include<iostream.h> void main() { float a=3.5, b=2.2, c; c=a/b; cout<<c<<endl; } output: 1.590

  9. Rule # 4 • Operation between an integer and real always yields a real result. • Problem 7 int a=3; float b=2.2, c; c=a* b; cout<<c<<endl; output: 6.6

  10. Rule # 5 • The modulus operator can be used only with integers. • Problem 8: • int a,b,c; a=9; b=4; c=a%b; cout<<c<<endl; output: 1

  11. Type casting • Sometimes we are needed to force the compiler to explicitly convert the value of an expression to a particular data type. • Problem 9 • void main() { float a; int x=6, y=4; a= (float ) x/y; // type casting is done here cout<<a<<endl; } output : 1.5

  12. Priority of arithmetic operators • Evaluate expression i=2* b* y/ (d+1) - x / (3 * (z + y)) ? How would you do it? What would be the result?

  13. Priority Rules • BODMAS – Brackets of division, multiplication and addition, subtraction • If there are more than one set of brackets, the operations within the innermost brackets will be performed first • Incase of a tie between operations of same priority preference is given to operator which occurs first

  14. Evaluation process int b=2, d=1, x=4, y=5, z=4; i=2* b* y/ (d+1) - x / (3 * (z + y)) ? i=2* 2* 5/ (1+1) – 4/ (3 * (4 + 5)) ? i=2* 2* 5/ 2 - 4/ (3 * 9 ) i=2* 2* 5/ 2 - 4/ 27 i= 4 * 5/ 2 - 4/ 27 i= 20 / 2 - 4/ 27 i= 10 -4/ 27 i= 10 - 0.148 i= 9.852

  15. Assignment operators • Assignment operators are used to assign the result of an expression to a variable. • float a; // variable declaration a = 25.2; // ‘ = ‘ is assignment operator Short hand form: x=x+1; or x += 1; y=y-1; or y -= 1; z=z*(x+y); or z *=(x+y); y=y/(x+y); or y /=(x+y); x = x % z or x %=z;

  16. Increment and decrement operators • ++ is increment operator. • -- decrement operator. Modes • ++a and -- a are in pre-increment mode. • a++ and a– are in post increment mode.

  17. Usage • b= ++a ; or a=a+1; b=a; • b= a++; or b=a; a=a+1;

  18. Problem 10 • To understand increment and decrement operators #include<iostream.h> // preprocessor directive void main() // function main { int a=10,b,c=10,d; b=++a; // eqt : a=a+1; b=a; (pre-increment) cout<<“b is “<<b<<endl; d=c++; // eqt : d=c; c=c+1; (post-increment) cout<<“d is “<<d<<endl; } Output: b is 11 d is 10

  19. Relational operators • < less than • > greater than • <= less than or equal to • >= greater than or equal to • You would understand this when you are taught Control structures. Just know these operators for now.

  20. Logical operators • && Logical AND • | Logical OR • ! Logical NOT • You would understand this when you are taught Control structures. Just know these operators for now.

  21. Equality operators • == equal to • != not equal to • You would understand this when you are taught Control structures. Just know these operators for now.

  22. Conditional operators • expr1 ? Expr2 : expr3 • You would understand this when you are taught Control structures. Just know these operators for now.

  23. Problem Solving • Step 1 : Thorough understanding of the problem. • Step 2 : Frame an algorithm. • Step 3 : Develop a pseudo code. • Step 4 : Draw a flow chart. • Step 5 : Code the program.

  24. Algorithm : A procedure for solving a problem in terms of : • The actions to be executed, • the order in which these actions are to be executed

  25. Pseudocode : Its an informal code. • Similar to everyday english • They help programmers think out a program • Pseudocodes are then converted to the executable language statements.

  26. Flow chart – It is a pictorial representation of the flow of the logic of the program. • Logic means the approach of the problem.

  27. 5 lectures in a nutshell • Lec 1 : Introduction to computers • Lec 2 : Simple c++ programs ( 2 problems) • Lec 3 : Variables and Data types • Lec 4 : Repeated Variables and data types • Lec 5 : Operators, its types, and Problem Solving (8 problems ) “ This forms the basis, the core of problem solving using a higher level languages starts here “

More Related