Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty - - PowerPoint PPT Presentation

computer programming
SMART_READER_LITE
LIVE PREVIEW

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty - - PowerPoint PPT Presentation

IIT Bombay Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session: Loops and Assignment Expressions Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay


slide-1
SLIDE 1

IIT Bombay

Computer Programming

  • Dr. Deepak B Phatak
  • Dr. Supratik Chakraborty

Department of Computer Science and Engineering IIT Bombay Session: Loops and Assignment Expressions

1

  • Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay
slide-2
SLIDE 2

IIT Bombay

  • Iteration idioms in programming
  • Necessity and convenience of iteration
  • “while …”, “do … while …” and “for …” loops in C++
  • Use of “break” statements in loops

2

  • Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Quic ick Recap of f Rele levant Topics

slide-3
SLIDE 3

IIT Bombay

  • Closer look at “for” loops

Use of assignment expressions and its variants Use of “,” separated expressions

3

  • Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Overv rview of f Th This is Le Lecture

slide-4
SLIDE 4

IIT Bombay

“for …” St Statement: Our Si Simple Vie iew

Part of program before iteration for ( iteration initialization ; loop condition ; instructions to execute at end of every iteration) { Block of statements (“for” loop body ) } Part of program after iteration

  • Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

4

Semi-colons not to denote end of executable statements But to separate three parts inside for ( ….. ) Note absence of semi-colon

slide-5
SLIDE 5

IIT Bombay

“for …” St Statement: C++ ++ St Standard Vie iew

Part of program before iteration for (iteration initialization ; loop condition ; instructions to execute at end of every iteration) { Block of statements (“for” loop body ) } Part of program after iteration

  • Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

5

initialization expression update expression

slide-6
SLIDE 6

IIT Bombay

Appears Nonsensical?

  • We needed assignment statements to initialize variables

before entering loop

  • We needed assignment statements to update after each

iteration Is it meaningful to have initialization expression and update expression? What if I write a + b*c for initialization/update expression? Which variable is initialized/updated here?

  • Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

6

slide-7
SLIDE 7

IIT Bombay

Assignment as An Operator

  • C++ allows “=“ (assignment) to be viewed as an operator in

an expression, with side effects Assignment: x = (y + z) As a statement: x = (y + z) ; Assign the value of expression y+z to x As an operator: x = (y + z) Side effect: Value of expression (y+z) is stored in x Type and value: Same as those of (y + z) … RHS of “=“

  • Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

7

Semi-colon present Semi-colon absent

slide-8
SLIDE 8

IIT Bombay

“for …” St Statement: Our Enhanced Vie iew

Part of program before iteration for ( count = 1.0 ; loop condition ; count = (count + 1)) { Block of statements (“for” loop body ) } Part of program after iteration

  • Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

8

Expressions with side-effects

slide-9
SLIDE 9

IIT Bombay

More on Assignment as An Operator

  • Need operator precedence

What is (a = b + c) as an expression ?

  • Precedence of = lower than that of arithmetic and logical
  • perators we have seen so far

(a = b + c) as an expression is (a = (b + c)) An expression with side-effect: a is assigned the value of b+c Type and value of (a = b + c) is same as that of (b + c)

  • Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

9

slide-10
SLIDE 10

IIT Bombay

More on Assignment as An Operator

  • Need associativity

Right-to-left associative (a = b = c = a + 1) is evaluated as (a = (b = (c = (a + 1)))) Type and value same as that of (a + 1)

  • Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

10

slide-11
SLIDE 11

IIT Bombay

Sp Special Assignment Operators

  • Increment

Post-increment: x++ Similar to x = x + 1 But, value is that of x before incrementing

  • Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

11

Value of y: 10 Value of x: 2

y = x++;

Value of y: 2 Value of x: 3 x++ as an expression

slide-12
SLIDE 12

IIT Bombay

Sp Special Assignment Operators

  • Increment

Pre-increment: ++x Similar to x = x + 1 Value is that of x after incrementing

  • Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

12

Value of y: 10 Value of x: 2

y = ++x;

Value of y: 3 Value of x: 3 ++x as an expression

slide-13
SLIDE 13

IIT Bombay

Sp Special Assignment Operators

  • Decrement

Post-decrement: x-- Similar to x = x - 1 Value is that of x before decrementing

  • Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

13

Value of y: 10 Value of x: 2

y = x--;

Value of y: 2 Value of x: 1 x-- as an expression

slide-14
SLIDE 14

IIT Bombay

Sp Special Assignment Operators

  • Decrement

Pre-decrement: --x Similar to x = x - 1 Value is that of x after decrementing

  • Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

14

Value of y: 10 Value of x: 2

y = --x;

Value of y: 1 Value of x: 1

  • -x as an

expression

slide-15
SLIDE 15

IIT Bombay

Compound Assignment Operators

  • Increment/decrement variable by an expression

x += (y + z) same as x = x + (y + z) x -= (2*w) same as x = x – (2* w)

  • Can have similar operators from other arithmetic operators

x *= 2 same as x = x*2 x /= y same as x = x/y x %= 5 same as x = x/5

  • Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

15

slide-16
SLIDE 16

IIT Bombay

In Increment and Decrement Operators

  • Precedence and associativity:
  • Post-increment/post-decrement same precedence, left-to-right

associative

  • Pre-increment/pre-decrement same precedence, right-to-left

associative

  • Pre-increment/pre-decrement has lower precedence than

post-increment/post-decrement

  • All have higher precedence than other arithmetic and logical
  • perators we have seen
  • Exception: pre-increment/pre-decrement same precedence as ! (lo
  • +=, -=, /=, %= have lowest precedence (same as that of =), right-to-

left associative

  • Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

16

slide-17
SLIDE 17

IIT Bombay

In Increment and Decrement Operators

  • Precedence and associativity:
  • Post-increment/post-decrement same precedence, left-to-right

associative

  • Pre-increment/pre-decrement same precedence, right-to-left

associative

  • Pre-increment/pre-decrement has lower precedence than

post-increment/post-decrement

  • All have higher precedence than other arithmetic and logical
  • perators we have seen
  • Exception: pre-increment/pre-decrement same precedence as ! (lo
  • +=, -=, /=, %= have lowest precedence (same as that of =), right-to-

left associative

  • Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

17

Moral of the story: Try not to mix increment/decrement operators with other operators, if possible Convenient idioms for increment and decrement Use them mostly for that purpose

slide-18
SLIDE 18

IIT Bombay

Use of f “,” Operator

  • Using side effects of multiple expressions when only one is

allowed

  • (x++ , y = z+2 , z + 5) is one expression
  • Component expressions evaluated in left-to-right order
  • Two side-effects

x is incremented y is assigned the value of z + 2

  • One type and value: Same as rightmost expression, i.e. z + 5
  • Often used in initialization and update of “for” loops
  • Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

18

slide-19
SLIDE 19

IIT Bombay

“for …” St Statement: Our Enhanced Vie iew

Part of program before iteration for ( count = 1.0, i = 2 ; loop condition ; count += 5, i-- , j++) { Block of statements (“for” loop body ) } Part of program after iteration

  • Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

19

Evaluates as a single expression Two side effects

slide-20
SLIDE 20

IIT Bombay

Su Summary

  • Assignment as a statement and as an expression
  • Variants of assignment statements
  • Use in loops (and other places too) in C++
  • Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

20