Expressions An Expression is a sequence of operands

  • Slides: 18
Download presentation
Expressions ·An Expression is a sequence of operands and operators that reduces to a

Expressions ·An Expression is a sequence of operands and operators that reduces to a single value. ·An operator is a language-specific syntactical token that requires action to be taken. For example, multilply (*) is an operator. ·There is no limit to the number of operator and operand sets that you can combine to form an expression. ·Example: 5 + 3 * 4 ·The value of above expression is 17. ·Multiply, divide, and modulus operators have the highest priority among binary operators and have the fourth level in the Precedence Table 1

 • The modulus ( % ) operator divides the first operand by the

• The modulus ( % ) operator divides the first operand by the second operand returns the remainder rather than the quotient. • For eg. 5 % 2 returns 1 • For modulus operator both operands must be integer types. Contd. . 2

Following example will general compile error because Modulus operands must be integers. #include <stdio.

Following example will general compile error because Modulus operands must be integers. #include <stdio. h> int x; int main () { x = 7. 5 % 3; return 0; } 3

 • The assignment operator = assigns a value to a variable. For example,

• The assignment operator = assigns a value to a variable. For example, • x= y • If y has the value of 6, then the value of the variable y (which is 6 )is assigned to the variable x. • The value of the expression x =y is also 6 • The left operand must always be a single variable 4

 • A compound assignment is a shorthand notation for a simple assignment. •

• A compound assignment is a shorthand notation for a simple assignment. • Compound assignment operators are: *=, /=, %=, +=, and -= • For example: For x*=y, equivalent simple expression is: x = x * y • Similarly, • x /= y is x = x/y • x -= y+ 4 is x = x- ( y+ 4) • x %= y is x = x % y • x *= y+2 is x = x* ( y+ 2). 5

 • The postfix increment/decrement operates at the second level of the Precedence Table

• The postfix increment/decrement operates at the second level of the Precedence Table ( see inside front cover) • a ++ Here a is an operand while ++ is a postfix operator. • a++ has the same effect as a =a+1. • Although the result of both expression is the same, there is a major difference. For example: If variable a contains 4 before the expression is evaluated, the value of the expression a ++ is 4 • After the evaluating the expression and its side effects, then only a becomes 5. • 6

#include <stdio. h> int main (void) /* example of Postfix increment*/ { int a;

#include <stdio. h> int main (void) /* example of Postfix increment*/ { int a; a = 5; printf("%dt", a); printf("%dt", a++); printf("%dn", a); return 0; } /* Results: 5 5 6 */ 7

Prefix increment/decrement are also shorthand notations for adding or subtracting 1 similar to the

Prefix increment/decrement are also shorthand notations for adding or subtracting 1 similar to the Postfix operators. • The one major difference is that in Prefix operators, the effect takes place before the expression that contains the operator is evaluated. • Which one to choose? • When you need the values to be the current contents of a variable, use a postfix operator. • When you need the values to be the new contents of the variable, use a prefix operator. 8

#include <stdio. h> int main (void) /* example of Prefix increment*/ { int a;

#include <stdio. h> int main (void) /* example of Prefix increment*/ { int a; a = 5; printf("%dt", a); printf("%dt", ++a); printf("%dn", a); return 0; } /* Results: 5 6 6 */ 9

The sizeof operator tell you the size, in bytes, of whatever type is specified.

The sizeof operator tell you the size, in bytes, of whatever type is specified. For example: sizeof(int), sizeof ( -746. 565), • sizeof operator returns different values according to the specific hardware. • For example: int a; int b = sizeof(a); // will give 2 bytes in most PCs while 4 bytes for mainframes and 16 bytes for supercomputers. 10

Precedence is used to determine the order in which different operators in a complex

Precedence is used to determine the order in which different operators in a complex expression are evaluated. • 2+ 5*4 means 2 + (5 * 4) • The value of above expression is 22 because multiplicity has a higher precedence. • -a++ means ( - ( a ++) ) • ++ is evaluated first because postfix increment has a higher precedence. 11

#include <stdio. h> int main (void) { int a = 2; int b =

#include <stdio. h> int main (void) { int a = 2; int b = 3; int c = 4; printf ("a * b + c is: %dn", a * b + c); printf ("a * (b + c) is: %dn", a * (b + c)); return 0; } /* a * b + c is: 10 a * (b + c) is: 14 */ 12

Associativity can be either from the left or the right. • Associativity is used

Associativity can be either from the left or the right. • Associativity is used only when the operators have the same level precedence. • Example of left-to-right: * / % * • 4* 8 / 2 % 4 * 2 means ((((4* 8) / 2) % 4)* 2 ) • Similarly for right-to left, • a +=b *=c -=5 means (a +=(b *=(c -=5 ))) 13

A Side Effect is an action that results from the evaluation of an expression.

A Side Effect is an action that results from the evaluation of an expression. int x = 3; x = x +2; • Here if the original value of x is 3, the value of the expression on the right is 5, the whole expression also has a value of 5 and as a side effect x receives the value 5. 14

Implicit type conversion : C will automatically convert any intermediate type values from one

Implicit type conversion : C will automatically convert any intermediate type values from one format to another according to the promotion hierarchy. • This is done automatically by the compiler • For Example : To add integer and float, first integer is converted to float and then after adding, the float will be converted to integer if the value has to be stored in as an integer. 15

Explicit type conversion is when you convert data from one type to another type

Explicit type conversion is when you convert data from one type to another type yourself rather than the compiler. • Uses cast expression operator • Example program: –contd. . 16

char a. Char = '�'; int. Num 1= 100, int. Num 2 = 45;

char a. Char = ''; int. Num 1= 100, int. Num 2 = 45; double flt. Num 1= 100. 0 , double flt. Num 2= 45. 0; double flt. Num 3; flt. Num 3 = (double)(int. Num 1 / int. Num 2); /* result 2. 00 */ flt. Num 3 = (double)int. Num 1 / int. Num 2; /* result 2. 22 */ a. Char = (char)(flt. Num 1 / flt. Num 2); /* result 2 */ 17

 • Statement causes an action to be performed. • An expression becomes a

• Statement causes an action to be performed. • An expression becomes a statement when there is a semilcolon ( ; ) at the end. • A compound statement consists of zero or more statements. It is also known as block • Main part of a program is an example of a compound statement 18