This preview shows page 1. Sign up to view the full content.
Unformatted text preview: 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 Data and variables are called operands. Types of Operators
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 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 Arithmetic operators + (Addition) (subtraction) / (Division) * (Multiplication) % ( Modulo operation) 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. 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 Rules in arithmetic operations 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 Rule # 2 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 Rule # 3 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 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 Sometimes we are needed to force the compiler to Type casting 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 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? Priority Rules
1. 2. 3. 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 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 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=y1; 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; Increment and decrement operators
++ is increment operator.  decrement operator. Modes ++a and  a are in preincrement mode. a++ and a are in post increment mode. Usage b= ++a ; or a=a+1; b=a; b= a++; or b=a; a=a+1; Problem 10 To understand increment and decrement #include<iostream.h> // preprocessor directive void main() // function main { int a=10,b,c=10,d; b=++a; // eqt : a=a+1; b=a; (preincrement) cout<<"b is "<<b<<endl; d=c++; // eqt : d=c; c=c+1; (postincrement) cout<<"d is "<<d<<endl; } Output: b is 11 d is 10 operators 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. Logical operators && Logical AND  Logical OR ! Logical NOT You would understand this when you are taught Control structures. Just know these operators for now. Equality operators == equal to != not equal to You would understand this when you are taught Control structures. Just know these operators for now. Conditional operators expr1 ? Expr2 : expr3 You would understand this when you are taught Control structures. Just know these operators for now. 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. Algorithm : A procedure for solving a problem in terms of : 1. The actions to be executed, 2. the order in which these actions are to be executed 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. Flow chart It is a pictorial representation of the flow of the logic of the program. Logic means the approach of the problem. 5 lectures in a nutshell Lec 1 : Introduction to computers Lec 3 : Variables and Data types Lec 2 : Simple c++ programs ( 2 problems) 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 " ...
View Full
Document
 Spring '09

Click to edit the document details