CP264 Lecture 3 Operations and Flow PDF

Summary

This document covers Lecture 3 on operations and flow controls in C programming, explaining arithmetic and relational operators. It discusses control flow constructs such as sequence, selection, and repetition, along with special control statements like break, continue, and goto. The document also covers various assignment operators and provides code examples.

Full Transcript

Lecture 3 Operations and flow controls 1. Operations, operators, expressions 1. Arithmetic operations 2. Relational (Boolean) operations 3. Logic operations 4. Bitwise operations 2. Flow controls 1. Flow constructs: Sequence, Selection, Repetition 2. Special flow control statem...

Lecture 3 Operations and flow controls 1. Operations, operators, expressions 1. Arithmetic operations 2. Relational (Boolean) operations 3. Logic operations 4. Bitwise operations 2. Flow controls 1. Flow constructs: Sequence, Selection, Repetition 2. Special flow control statement: break, continue, goto 1. Operations, operators, and expressions Arithmetic operations – Arithmetic operators: *, /, +, -, – Operands of basic data types char, int, float, double, etc. – Operations return the result of the same type as operands – Modular operator % applies to integer data types, char, int, long. – Arithmetic expressions: infix notation of operands and operator, constants, variables, arithmetic operators, parenthesis. Example: int a = 9, b = 3, result; OPERATION OPERATOR SYNTAX COMMENT RESULT Multiply * a * b result = a * b 27 Divide / a / b result = a / b 3 Addition + a + b result = a + b 12 Subtraction - a - b result = a – b 6 Modulus % a % b result = a % b 0 When computing is done? Constant operations and evaluations are done by compiler. Example: int c = (5 + 4)*(5-3); printf(“%d”, c); // output 18, the value 18 is computed by compiler at compile time. The compiler evaluates the right side 18, and generates instruction to write 18 to variable c. The actual computing is done at compile time. Operation computing involving with variable operands are computed at runtime time. Namely, compilers generate instructions to do the operations at runtime. Example: int a = 5, b =3; int c = (a+b)*(a-b); printf(“%d”, c); // output 18, the value 18 is computed when running the program. Resulted data type of arithmetic operations Arithmetic operations on the same data types result the same type. Example: int / int gives int, e.g. 5/2 gives 2 float / float gives float, e.g. 1.0/2.0 gives 0.5 Arithmetic operations on different data types will convert a low type to a high type (called type conversion), then do the computing. Compilers generate instructions for type conversions – Type conversion of variables refers to changing the data type of one data type to another data type. Conversion from low to high by order double, float, long, int, short, char. – Forced conversion (also called type casting) is to tell compiler the to do conversion explicitly using syntax (datatype) Example: 3 + 2.4 is evaluated as 3.0+2.4, gives 5.4 1.0/2 is evaluated as 1.0/2.0, gives 0.5. typecasting (int) 1.0/2 converts 1.0 to int type value 1, then compute 1/2, gives 0. More examples of type conversions int a = 2.8; // compiler changes 2.8 to 2, a will have value 2 float b = 2.6; int a = b; // implicit casting, conversion at runtime, a have value 2 int a = (int) b; // explicit casting, conversion at runtime, a have value 2 int a = 2; float b = 2.6; int c = a + b; a will be casted to floating number and with b, and then cast the result to int value and assign to c int c = (float) a + b; // the same as int c = a + b; int a=5, b=2, c ; float f1 = 1.5, f2 = 3.6, f3; f1 = b; // implicit casting, f1 = 2.0, f1 = (float) b; // explicit casting b = (int) f2; // b = 3 f3 = a + f1; // f3 has value 6.5 c = a + f1; // c has value 6 c = f1 + f2; // has value 5 c = (int) f1 + (int) f2; // c has value 4 Overflow, underflow, correctness of operations. Overflow happens when the result is out of the range a data type. Example int a = 2147483647; // = 231-1 int b = a+1; Overflow happens as 2147483648 is out of the range of int type. As a result, b won’t be 2147483648. Underflow (floating point underflow) happens when operation produces a result smaller than the smallest floating number the of float or double type. Correctness of computing – The result of an operation on non-floating types is correct if there is no overflows. – Operations on floating data types will cause precision loss in general. Correctness is measured by tolerance. Example, tolerance 0.000001 means correctness to 5 fraction digits. Relational operations – Relational operators: , >=, GREATER THAN 7 > 9 GIVES 0 >= GREATER THAN OR EQUAL TO 100 >= 100 GIVES 1

Use Quizgecko on...
Browser
Browser