0% found this document useful (0 votes)
38 views22 pages

Lect31 Operators Expressions

This document discusses operators and expressions in computer programming. It covers relational operators like < and >, logical operators like && and ||, assignment operators, increment and decrement operators, bitwise operators, and special operators. It also discusses operator precedence and control structures like if statements and switch statements. Evaluation of expressions using these operators follows specific rules like short circuit evaluation for logical operators.

Uploaded by

aggarwalmegha
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views22 pages

Lect31 Operators Expressions

This document discusses operators and expressions in computer programming. It covers relational operators like < and >, logical operators like && and ||, assignment operators, increment and decrement operators, bitwise operators, and special operators. It also discusses operator precedence and control structures like if statements and switch statements. Evaluation of expressions using these operators follows specific rules like short circuit evaluation for logical operators.

Uploaded by

aggarwalmegha
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

COMPUTER PROGRAMMING I (TA C162)

Lecture 31 Operators p & Expressions p

Todays Agenda

Operators and Expressions


Relational Operators, p , Logical g Operators p Assignment Operators Increment and Decrement Operators Bitwise Operators, Special Operators

Control Structure

Conditional

If statement , Switch statement, Conditional operator statement t t t

Tuesday, April 06, 2010

Biju K Raveendran@BITS Pilani

Control Structures

Controls the flow of execution in program It is a combination of individual instructions into a single logical unit Only one entry point and one exit point S l i control Selection l structure

A control structure that chooses among alternative program statements An expression A i th that t is i either ith t true or f false l What is TRUE and What is FALSE in C?

Condition

0 FALSE and All Non Non-Zero Zero values TRUE


Biju K Raveendran@BITS Pilani 3

Tuesday, April 06, 2010

Relational & Equality Operators


O Operator t < <= < > >= == != Is less than Is less than or equal to Is greater than Is greater than or equal to Is equal to Is not equal to M Meaning i

Tuesday, April 06, 2010

Biju K Raveendran@BITS Pilani

Relational & Equality Operators


Most conditions will have one of the following forms

variable <relational operator / equality operator> variable variable <relational operator / equality operator> constant

The variable / constant can be integer, float/double or character

Tuesday, April 06, 2010

Biju K Raveendran@BITS Pilani

Examples:
5 <= 7 5 < -8 11 < 8+4 x+y == a+b TRUE FALSE TRUE TRUE

if value of x+y is equal to value of a+b

-20 >= 0 FALSE 5.8 <= 3.5 f >= 2.8 9 >= 0 A <= < Z a <=ch && ch <= z

Logical Operators
Operator && || ! Meaning Logical AND Logical OR Logical NOT

Used to test more than one condition


Example: a<b && y==5 Expression is TRUE only if a<b is TRUE and y==5 is TRUE
If either or both of them are FALSE then expression becomes FALSE

Logical expression

An expression p that uses one or more of the logical g operators. p


Biju K Raveendran@BITS Pilani 7

Tuesday, April 06, 2010

Logical Operators

Examples

in_range = (n > -10 && n < 10); is letter = ( is_letter (A A < <= ch && ch <= < Z) Z ) || (a <= ch && ch <= z); even = (n % 2 == 0);

Tuesday, April 06, 2010

Biju K Raveendran@BITS Pilani

Operand 1 Non-Zero Non Zero (TRUE) Non-Zero (TRUE) Zero (FALSE) Zero (FALSE) Operand 1 Non-Zero (TRUE) Non-Zero (TRUE) Zero (FALSE) Zero (FALSE)

Operand 2 Non-Zero Non Zero (TRUE) Zero (FALSE) Non-Zero (TRUE) Zero (FALSE) Operand 2 Non-Zero (TRUE) Zero (FALSE) Non-Zero Non Zero (TRUE) Zero (FALSE)

Operand 1 && Operand 2 1 (TRUE) 0 (FALSE) 0 (FALSE) 0 (FALSE) Operand 1 || Operand 2 1 (TRUE) 1 (TRUE) 1 (TRUE) 0 (FALSE) ! Operand 1 0 (FALSE) 1 (TRUE)

Operand 1 Non-Zero (TRUE) Zero (FALSE)

Tuesday, April 06, 2010

Biju K Raveendran@BITS Pilani

DeMorgans theorem

Complement of Expr1 && Expr2 is written as


Comp1 || Comp2 Comp1 complement of Expr1 Comp2 complement of Expr2 Comp1 && Comp2 C Comp1 1 complement l t of f Expr1 E 1 Comp2 complement of Expr2

Complement of Expr1 || Expr2 is written as


Complement of

age > 25 && (status == S || status == D) is age <= < 25 || (status != ! S S && status ! != D) D)
Tuesday, April 06, 2010 Biju K Raveendran@BITS Pilani 10

Assignment Operators
Simple Assignment Operators a = a+1 a = a-1 a =a*(b+1) a = a/(b+1) a = a%b Shorthand Operators a+= 1 a-= 1 a*=b+1 a/=b+1 a%=b %

b*=b; b =b; is equivalent to??


Short hand statement is more concise and easier to read
Tuesday, April 06, 2010 Biju K Raveendran@BITS Pilani 11

Assignment Sample Explanation operator expression


Assume: int i c = 3, d = 5, e = 4, f = 6, g = 12; += -= *= /= %= % c += 7 d -= 4 e *= 5 f /= 3 g % %= 9 c = c + 7 d = d - 4 e = e * 5 f = f / 3 g = g % 9

Assigns

10 to c 1 to d 20 to e 2 to f 3 to g

Tuesday, April 06, 2010

Biju K Raveendran@BITS Pilani

12

Increment and Decrement Operators


++ Adds one to the operand -- Subtracts one from the operand Both are unary operators

++i i --i is

i equivalent is i l t to: t i = i+1 i 1 is equivalent to: i = i-1 ++i and i++ are same??

13

What is Postfix and Prefix?


In postfix the expression is evaluated first using the original value of the variable and then the variable is incremented (or decremented) by one. Ex. m = n++; In p prefix the variable is incremented (or ( decremented) ) first and then the expression is evaluated using the new value of the variable. Ex. m = n;
Tuesday, April 06, 2010 Biju K Raveendran@BITS Pilani 14

Postfix and Prefix Examples


j =10; i =j++; What is the values of i and j? j =10; i=++j; What is the values of i and j?

Tuesday, April 06, 2010

Biju K Raveendran@BITS Pilani

15

i = 10; j = 20; k = i++ + j++ j++; What will be the value of i, j and k? i = 10; j = 20; k = i++ + ++j; What will be the value of i, j and k? i = 10; j = 20; k = ++i + ++j; Wh t will What ill b be th the value l of fi i, j and d k?
Tuesday, April 06, 2010 Biju K Raveendran@BITS Pilani 16

Operator
++ ++ ---

Sample expression Explanation


++a a++ --b b--

Increment a by 1, then use the new value of a in the expression in which a resides. resides Use the current value of a in the expression in which a resides, then increment a by 1. Decrement b by 1, then use the new value of b in the expression e p ess o in which w c b resides. es des. Use the current value of b in the expression in which b resides, then decrement b by 1.

Tuesday, April 06, 2010

Biju K Raveendran@BITS Pilani

17

Bitwise Operators
Operator & | ^ << >>

Meaning Bitwise AND Bitwise OR Bitwise exclusive OR Shift left Shift right

Manipulation of data at bit level May not be applied to float values

18

Special Operators

The Size of Operator


Returns the number of bytes the operand occupies

Examples: p=sizeof(value); q=sizeof(float); r=sizeof(long int);

Tuesday, April 06, 2010

Biju K Raveendran@BITS Pilani

19

The comma operator


Used to link the related expressions together. Evaluation starts from left to right. g And the value of the right most expression is the value of the combined expression.

Example: value = (a = 6 6, b = 7 7, c = 8 8, a+b+c); value contains 21 Use o of co comma a ope operator ato for (i = 1, j = 15; i<j; i++, j--)

Tuesday, April 06, 2010

Biju K Raveendran@BITS Pilani

20

Operators (),[], ., -> Postfix ++, Postfix --

Associativity Rank L to R R to L 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
21

Prefix ++, Prefix --, !, ~, sizeof, Unary +, R to L Unary -, Unary *, Unary & (type) *, /, % Binary plus(+) and Binary minus (-) <<, >> <, <=, >, >= ==, != Binary & Binary ^ Binary | && || ? ?: =, *=, /=, %=, +=, -=, &=, ^=, |=, <<=, >>= , R to L L to R L to R L to R L to R L to R L to R L to R L to R L to R L to R R t to L R to L L to R

Short Circuit Evaluation


a || b is true, if a is true, a && b is false, if a is false

Tuesday, April 06, 2010

Biju K Raveendran@BITS Pilani

22

You might also like