Operators & Expressions PDF
Document Details
Tags
Summary
This document is a lecture on operators and expressions in computer programming, likely for a COMSCI 1101 course. It covers arithmetic, assignment, increment/decrement, reference, and special operators, along with their usage and precedence in C programming.
Full Transcript
OPERATORS & EXPRESSIONS COMSCI 1101 LECTURE TABLE OF CONTENTS. 01 02 03 Overview of Operators Arithmetic & Relational & Logical & Expressions Assignment Operator Operator...
OPERATORS & EXPRESSIONS COMSCI 1101 LECTURE TABLE OF CONTENTS. 01 02 03 Overview of Operators Arithmetic & Relational & Logical & Expressions Assignment Operator Operator 04 05 06 Increment & Decrement Expression and Type Special Operators Operators Conversion OVERVIEW OF OPERATORS & EXPRESSIONS OPERATORS & EXPRESSIONS ▪ There are many different type of operators: Assignment Arithmetic Relational Logical Special OPERATORS & EXPRESSIONS ▪ An expression is an syntactically correct combinations of operands and operators. ▪ An expression may consist of one or more operands, and zero or more operators to produce a value. ▪ Just as operands have a type and a value, expressions also have a type and value. ARITHMETIC & ASSIGNMENT OPERATOR ARITHMETIC OPERATOR ▪ There are two basic types of arithmetic operators: unary and binary. ▪ A unary operator requires only one operand, binary operators requires two. ▪ There is only one unary arithmetic operator, and there are five (5) binary operators. ARITHMETIC OPERATOR Operator Meaning Example - Unary minus -x * Multiplication x * y / Division x / y % Modulus x % y + Addition x + y - Subtraction x – y ARITHMETIC OPERATOR ▪ Operators follow rules of precedence and associativity, which determine precisely how expressions are evaluated. ▪ All operators on the same line have equal precedence and all operators on a given line have higher precedence than those on the lines below them. ▪ Expressions within parentheses () are always evaluated first. PRECEDENCE TABLE Precedence Operator Associativity 1 () Left-to-Right 2 -(unary) Right-to-Left 3 * / % Left-to-Right 4 + - Left-to-Right #include #include void main(){ clrscr(); int a = 2, b = 5, c = 7, result = 0; result = a + b – c * a / b; printf(“Result: %d\n”,result); getch(); } Result: 5 _} INTEGER ARITHMETIC ▪ Whenever an arithmetic expressions consist of an operator and two integer operands, the expression will be evaluated using integer arithmetic. ▪ In integer arithmetic, all decimal portions of numbers are lost (or truncated). INTEGER ARITHMETIC ▪ Whenever an arithmetic expressions consist of an operator and two integer operands, the expression will be evaluated using integer arithmetic. ▪ In integer arithmetic, all decimal portions of numbers are lost (or truncated). #include #include void main(){ clrscr(); int x = 3, y; y = x / 2; printf(“Value of y is %d”,y); getch(); } Value of y is 1 _ #include #include void main(){ clrscr(); int a = 20, b = 2, c = 25, d = 4, result; result = a – b; printf(“a – b = %d\n”,result); result = b * c; printf(“b * c = %d\n”,result); result = a / d; printf(“a / d = %d\n”,result); result = c + b; printf(“c + b = %d\n”,result); result = -c / (b + c) * d - a; printf(“-c / (b + c) * d – a = %d\n”,result); getch(); } a – b = 18 b * c = 50 a / d = 5 c + b = 27 -c / (b + c) * d – a = -20 _ ASSIGNMENT OPERATOR ▪ The equal sign (=) is the basic operator in C. ▪ Simple assignment expressions are of the form: variable = expression ▪ The value of the expression on the right side of the equal sign is computed then assigned to the variable on the left side of the equal sign. ASSIGNMENT OPERATOR ▪ Syntax: variable = expression ▪ Examples: float x; int a,b; x = 1.25; a = b = 10; ASSIGNMENT OPERATOR ▪ The assignment operator may be used more than once in an expression. ▪ The associativity of this operator is from right to left. #include #include void main(){ clrscr(); double x, y; int a, b, c; x = 1.25; printf(“The value of x is %lf\n”,x); y = x; printf(“The value of y is %lf\n”,y); a = 10; printf(“The value of a is %d\n”,a); c = b = a; printf(“The value of b is %d\n”,b); printf(“The value of c is %d\n”,c); getch(); } The value of x is 1.250000 The value of y is 1.250000 The value of a is 10 The value of b is 10 The value of c is 10 _} ASSIGNMENT OPERATOR Operator Example Same As = x = 4 x = 4 += x += 2 x = x + 2 -= x -= 2 x = x – 2 *= x *= 2 x = x * 2 /= x /= 2 x = x / 2 %= x %= 2 x = x % 2 PRECEDENCE TABLE Precedence Operator Associativity 1 () Left-to-Right 2 -(unary) Right-to-Left 3 * / % Left-to-Right 4 + - Left-to-Right 5 = += -= *= /= %= Right-to-Left #include #include void main(){ clrscr(); int x = 10; x += 5; printf(“The value of x is %d\n”,x); x -= 2; printf(“The value of x is %d\n”,x); getch(); } The value of x is 15 The value of x is 13 _} RELATIONAL & LOGICAL OPERATOR CONDITION ▪ An assumption on which tests the validity or effect of something else. ▪ Integer are used to represent true or false because there is no actual boolean or logical data type in C ▪ An expression that is either true (represented by 1) or false (represented by 0). RELATIONAL OPERATOR ▪ Relational operators are used to test specific conditions. ▪ There are six relational operators in C. ▪ This operator require us to use two operands. RELATIONAL OPERATOR Operator Meaning Example == Equal to x == y != Not equal to x != y < Less than x < y > Greater than x > y = y RELATIONAL OPERATOR ▪ Although all of the operators listed on the slide are called relational operators, the first two operators, == and !=, are often referred to as equality operators. PRECEDENCE TABLE Precedence Operator Associativity 1 () Left-to-Right 2 -(unary) Right-to-Left 3 * / % Left-to-Right 4 + - Left-to-Right 5 < >= Left-to-Right 6 == != Left-to-Right 7 = += -= *= /= %= Right-to-Left #include #include void main(){ clrscr(); int a = 1, b = 20, d = 100, result; result = a < b; printf(“a < b = %d\n”,result); result = (a + 10) >= d; printf(“(a + 10) >= d = %d\n”,result); result = a == b; printf(“a == b = %d\n”,result); result = b != d; printf(“b != d = %d\n”,result); result = 5 < b < 10; printf(“5 < b < 10 = %d\n”,result); getch(); } a < b = 1 (a + 10) >= d = 0 a == b = 0 b != d = 1 5 < b < 10 = 1 _} LOGICAL OPERATOR ▪ Logical operators when applied to expressions, also yield a 0 or 1 (false or true) result. ▪ Logical operator connect two or more relational expressions into one or reverse the logic of an expression. ▪ C supports three logical operators. LOGICAL OPERATOR Operator Meaning Example && And (x b) || (b > c) = %d\n”,result); result = !(c < d); printf(“!(c < d) = %d\n”,result); result = (a - b) && 1; printf(“(a – b) && 1 = %d\n”,result); result = d || b && a; printf(“d || b && a = %d\n”,result); getch(); } (a < b) && (b < c) = 1 (a > b) || (b > c) = 0 !(c < d) = 0 (a – b) && 1 = 1 d || b && a = 1 _} INCREMENT & DECREMENT OPERATOR INCREMENT OPERATOR ▪ Increment operators are unary operators, which means it requires only one operand to use. ▪ Using double plus symbol (++), increment operator increment or add 1 to the operand value. ▪ Increment operator can be classified into two types: ▪ Pre-Increment Operators ▪ Post-Increment Operator INCREMENT OPERATOR ▪ Pre-Increment: Value is first incremented and then used inside the expression. ▪ Example: int y = 2; x = ++y; #include #include void main(){ clrscr(); int y = 2, result; result = ++y; printf(“Value of ++y is %d\n”,result); printf(“Final value of y is %d\n”,y); getch(); } Value of ++y is 3 Final value of y is 3 _} INCREMENT OPERATOR ▪ Post-Increment: Value is first used in an expression and then incremented. ▪ Example: int y = 2; x = y++; #include #include void main(){ clrscr(); int y = 2, result; result = y++; printf(“Value of y++ is %d\n”,result); printf(“Final value of y is %d\n”,y); getch(); } Value of y++ is 2 Final value of y is 3 _} DECREMENT OPERATOR ▪ Like increment operator, decrement operator is a unary operator that requires only one operand. ▪ Using double minus symbol (--), decrement operator decrement or subtract 1 to the operand value. ▪ Decrement operator can be classified into two types: ▪ Pre-Decrement Operators ▪ Post-Decrement Operator DECREMENT OPERATOR ▪ Pre-Decrement: Value is first decremented and then used inside the expression. ▪ Example: int y = 2; x = --y; #include #include void main(){ clrscr(); int y = 2, result; result = --y; printf(“Value of --y is %d\n”,result); printf(“Final value of y is %d\n”,y); getch(); } Value of --y is 1 Final value of y is 1 _} DECREMENT OPERATOR ▪ Post-Decrement: Value is first used in an expression and then decremented. ▪ Example: int y = 2; x = y--; #include #include void main(){ clrscr(); int y = 2, result; result = y--; printf(“Value of y-- is %d\n”,result); printf(“Final value of y is %d\n”,y); getch(); } Value of y-- is 2 Final value of y is 1 _} PRECEDENCE TABLE Precedence Operator Associativity 1 () Left-to-Right 2 ++ -- ! -(unary) Right-to-Left 3 * / % Left-to-Right 4 + - Left-to-Right 5 < >= Left-to-Right 6 == != Left-to-Right 7 && Left-to-Right 8 || Left-to-Right 9 = += -= *= /= %= Right-to-Left SPECIAL OPERATOR SPECIAL OPERATOR ▪ Special operators are used in special situations. ▪ There are different kinds of special operators in C. ▪ Reference Operator ▪ Dereference Operator ▪ SizeOf Operator ▪ Ternary Operator ▪ Comma Operators REFERENCE OPERATOR ▪ Reference operator is one of the operators that returns the address (or reference) of the variable. ▪ Using the symbol (&), reference operator returns the address of the specified variable. PRECEDENCE TABLE Precedence Operator Associativity 1 () Left-to-Right 2 ++ -- ! -(unary) & Right-to-Left 3 * / % Left-to-Right 4 + - Left-to-Right 5 < >= Left-to-Right 6 == != Left-to-Right 7 && Left-to-Right 8 || Left-to-Right 9 = += -= *= /= %= Right-to-Left #include #include void main(){ clrscr(); int a = 4; printf(“Address of a is 0x%p\n”,&a); getch(); } Address of a is 0xFFF4 _} DEREFERENCE OPERATOR ▪ Deference operator returns the value of a variable pointed by the specified pointer. ▪ It can also be used to return the address of the pointed variable. ▪ By using the symbol (*) to an operand, we can return the value of a variable pointed by the specified pointer. PRECEDENCE TABLE Precedence Operator Associativity 1 () Left-to-Right 2 ++ -- ! -(unary) & * Right-to-Left 3 * / % Left-to-Right 4 + - Left-to-Right 5 < >= Left-to-Right 6 == != Left-to-Right 7 && Left-to-Right 8 || Left-to-Right 9 = += -= *= /= %= Right-to-Left #include #include void main(){ clrscr(); int a = 4; int *ptr; printf(“Address of a is 0x%p\n”,&a); printf(“Value of a is %d\n”,a); ptr = &a; printf(“\nAddress of pointer ptr is 0x%p\n”,ptr); printf(“Value of pointer ptr is %d\n”,*ptr); getch(); } Address of a is 0xFFF4 Value of a is 4 Address of pointer ptr is 0xFFF4 Value pointed by pointer ptr is 4 _} #include #include void main(){ clrscr(); int a = 4; int *ptr; ptr = &a; a = 20; printf(“Address of pointer ptr is 0x%p\n”,ptr); printf(“Value pointed by pointer ptr is %d\n”,*ptr); *ptr = 30; printf(“\nAddress of a is 0x%p\n”,&a); printf(“Value of a is %d\n”,a); getch(); } Address of pointer ptr is 0xFFF4 Value pointed by pointer ptr is 20 Address of a is 0xFFF4 Value of a is 30 _} SIZEOF OPERATOR ▪ Sizeof operator is used to determine or compute the operand’s size in bytes. ▪ It can be used to any data type variables like the primitive data types (int, char, float, or double) as well as compound data types. PRECEDENCE TABLE Precedence Operator Associativity 1 () Left-to-Right 2 ++ -- ! -(unary) & * sizeof Right-to-Left 3 * / % Left-to-Right 4 + - Left-to-Right 5 < >= Left-to-Right 6 == != Left-to-Right 7 && Left-to-Right 8 || Left-to-Right 9 = += -= *= /= %= Right-to-Left #include #include void main(){ clrscr(); int a = 4; char b = 3; float c = 2; double d = 1; printf(“Size of a is %lu\n”,sizeof(a)); printf(“Size of b is %lu\n”,sizeof(b)); printf(“Size of c is %lu\n”,sizeof(c)); printf(“Size of d is %lu\n”,sizeof(d)); printf(“Size of a + d is %lu\n”,sizeof(a + d)); getch(); } Size of a is 2 Size of b is 1 Size of c is 4 Size of d is 8 Size of a + d is 8 _} SIZEOF OPERATOR ▪ Since the TurboC application is a 16-bit-based application (we only use an emulator to be able to run in 32/64 bit systems), the int data type will return a 2 bytes size. ▪ But today’s compilers will likely return 4 bytes for an int data type. ▪ For example MinGW GCC Compiler will return 4 bytes size of int data type. TERNARY OPERATOR ▪ Ternary operator is a conditional operator. ▪ It helps on creating a short way of writing conditional statements. ▪ Ternary operator has three arguments. ▪ Condition ▪ Statement if True ▪ Statement if False TERNARY OPERATOR ▪ Syntax: condition ? statement_if_true : statement_if_false PRECEDENCE TABLE Precedence Operator Associativity 1 () Left-to-Right 2 ++ -- ! -(unary) & * sizeof Right-to-Left 3 * / % Left-to-Right 4 + - Left-to-Right 5 < >= Left-to-Right 6 == != Left-to-Right 7 && Left-to-Right 8 || Left-to-Right 9 ? : Righ-to-Left 10 = += -= *= /= %= Right-to-Left #include #include void main(){ clrscr(); int a = 4, b = 2, result; result = (a > b) ? a : b; printf(“Result: %d\n”,result); getch(); } Result: 4 _} #include #include void main(){ clrscr(); int a = 6, b = 3, result; result = (a > 5) ? ((b > 2) ? b : 0) : 0; printf(“Result: %d”,result); getch(); } Result: 3 _} #include #include void main(){ clrscr(); int a = 3; (a % 2 == 0) ? printf(“Even\n”) : printf(“Odd\n”); getch(); } Odd _} COMMA OPERATOR ▪ A comma can be used as a separator or as a operator. ▪ It has the least precedence of all C operators. PRECEDENCE TABLE Precedence Operator Associativity 1 () Left-to-Right 2 ++ -- ! -(unary) & * sizeof Right-to-Left 3 * / % Left-to-Right 4 + - Left-to-Right 5 < >= Left-to-Right 6 == != Left-to-Right 7 && Left-to-Right 8 || Left-to-Right 9 ? : Right-to-Left 10 = += -= *= /= %= Right-to-Left 11 , Left-to-Right COMMA OPERATOR ▪ As a separator: int a = 4, b = 2, c = 3; ▪ Can also be written as: int a = 4; int b = 2; int c = 3; COMMA OPERATOR ▪ Comma operator, as a operator, returns the rightmost value of an expression but first evaluate the rest of the expressions and discard their values. ▪ As a operator: int a = (1, 2, 3, 4, 5); ▪ The value of a will be 5. COMMA OPERATOR ▪ The following expressions are different: ▪ int a = 1, 2, 3, 4, 5; ▪ int a = (1, 2, 3, 4, 5); #include #include void main(){ clrscr(); int a; a = (printf(“Hello World!\n”), 15); printf(“%d”,a); getch(); } Hello World! 15 _} #include #include void main(){ clrscr(); int a, b, c; c = (a = 15, b = 12, a + b); printf(“Output: %d”,c); getch(); } Output: 27 _} EXPRESSION & TYPE CONVERSION TYPE CONVERSION ▪ Type conversion is a way of converting one datatype to another. ▪ There are different way of type conversion in C. ▪ Implicit Type Conversion ▪ Explicit Type Conversion TYPE CONVERSION ▪ Implicit type conversion is automatically done by the compiler. ▪ This generally takes place in a expression with more than one datatype. ▪ All datatypes in the expression will be coverted to the largest datatype used within the expression. TYPE CONVERSION ▪ Basic datatype upgrade sequence: ▪ char -> int -> float -> double #include #include void main(){ clrscr(); char a = ‘A’; int b = 2, result; result = a + b; printf(“Output: %d”,result); getch(); } Output: 67 _} #include #include void main(){ clrscr(); int a = 3, b = 2; double result; result = a / b; printf(“Output: %lf”,result); getch(); } Output: 1.000000 _} #include #include void main(){ clrscr(); int a = 3; double b = 2,result; result = a / b; printf(“Output: %lf”,result); getch(); } Output: 1.500000 _} TYPE CONVERSION ▪ Explicit type conversion specifies on which datatype to be converted. ▪ This process is also called type casting. ▪ Good practices of programming advise to use explicit type conversion rather than implicit type conversion. #include #include void main(){ clrscr(); double a = 2.5; int result; result = a + 1.5; printf(“Output: %d”,result); getch(); } Output: 4 _} #include #include void main(){ clrscr(); double a = 2.5; int result; result = (int) a + 1.5; printf(“Output: %d”,result); getch(); } Output: 3 _} THANK YOU!