• No results found

“for …” Statement: C++ Standard View

N/A
N/A
Protected

Academic year: 2022

Share "“for …” Statement: C++ Standard View"

Copied!
20
0
0

Loading.... (view fulltext now)

Full text

(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

(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

Quick Recap of Relevant Topics

(3)

IIT Bombay

• Closer look at “for” loops

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

Overview of This Lecture

(4)

IIT Bombay

“for …” Statement: Our Simple View

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

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

Note absence of semi-colon

(5)

IIT Bombay

“for …” Statement: C++ Standard View

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 initialization expression

update expression

(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?

(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 “=“

Semi-colon present Semi-colon absent

(8)

IIT Bombay

“for …” Statement: Our Enhanced View

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

Expressions with side-effects

(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 operators 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)

(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)

(11)

IIT Bombay

Special Assignment Operators

• Increment

Post-increment: x++

Similar to x = x + 1

But, value is that of x before incrementing

Value of y: 10 Value of x: 2

y = x++;

Value of y: 2 Value of x: 3

x++ as an expression

(12)

IIT Bombay

Special Assignment Operators

• Increment

Pre-increment: ++x

Similar to x = x + 1

Value is that of x after incrementing

Value of y: 10 Value of x: 2

y = ++x;

++x as an

expression

(13)

IIT Bombay

Special Assignment Operators

• Decrement

Post-decrement: x--

Similar to x = x - 1

Value is that of x before decrementing

Value of y: 10 Value of x: 2

y = x--;

Value of y: 2 Value of x: 1

x-- as an expression

(14)

IIT Bombay

Special Assignment Operators

• Decrement

Pre-decrement: --x

Similar to x = x - 1

Value is that of x after decrementing

Value of y: 10 Value of x: 2

y = --x;

--x as an

expression

(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

(16)

IIT Bombay

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 operators we have seen

Exception: pre-increment/pre-decrement same precedence as ! (lo

+=, -=, /=, %= have lowest precedence (same as that of =), right-to-

(17)

IIT Bombay

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 operators we have seen

Exception: pre-increment/pre-decrement same precedence as ! (lo

+=, -=, /=, %= have lowest precedence (same as that of =), right-to- left associative

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

(18)

IIT Bombay

Use of “,” 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

(19)

IIT Bombay

“for …” Statement: Our Enhanced View

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

Evaluates as a single expression Two side effects

(20)

IIT Bombay

Summary

• Assignment as a statement and as an expression

• Variants of assignment statements

• Use in loops (and other places too) in C++

References

Related documents

The idea in the steepest descent method is to choose a norm and then determine a descent direction such that for a unit step in that norm, the first order prediction of decrease

Hypertensive disorders of pregnancy (HDP), comprising gestational hypertension and pre-eclampsia, distress up to 10% of all the pregnancies.(3) These women have on average

Many European cities have already seen a rebound of pollution, with some       cities experiencing levels that exceed pre-lockdown concentrations... have

Pearls of high value as gems are obtained from the pearl oysters of the genus Pinctada. Green mussel, Mytilus viridis 2. Indian backwater oyster, Grassostrea

At the center of this aerial view of spring in Yellowstone National Park, water temperatures reach near 88°C which is too hot to support most life — but along the edges of the

Eight morphometric characters namely standard length (SL), fork length (FL), total length (TL), head length (HL), pre-dorsal length (PDL), pre-anal length (PAL),

It is seen that continuous aging at 400°C causes simultaneous coarsening of existing pre- cipitates and nucleation of fresh (Nb, Ti)C precipitates, which results in continued

The pre-mainsequene stars and post-AGB stars have detahed dust