Fundamentals of Programming Session 7 Instructor: Reza - - PowerPoint PPT Presentation

fundamentals of programming
SMART_READER_LITE
LIVE PREVIEW

Fundamentals of Programming Session 7 Instructor: Reza - - PowerPoint PPT Presentation

Fundamentals of Programming Session 7 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2013 These slides have been created using Deitels slides Sharif University of Technology Outlines Formulating Algorithms Case


slide-1
SLIDE 1

Fall 2013

Instructor: Reza Entezari-Maleki

Email: entezari@ce.sharif.edu

Sharif University of Technology

1

Fundamentals of Programming

Session 7

These slides have been created using Deitel’s slides

slide-2
SLIDE 2

Outlines

 Formulating Algorithms Case Study…

3.

Nested Control Structures  Assignment Operators  Increment and Decrement Operators  Repetition Essentials  for Repetition Statement

2

slide-3
SLIDE 3

 We’ll once again formulate the algorithm using pseudocode

and write a corresponding C program.

 In this case study we’ll see the only other structured way

control statements may be connected in C, namely through nesting of one control statement within another.

 Consider the following problem statement:

 A college offers a course that prepares students for the state

licensing exam for real estate brokers. Last year, 10 of the students who completed this course took the licensing

  • examination. Naturally, the college wants to know how well its

students did on the exam. You have been asked to write a program to summarize the results. You have been given a list of these 10 students. Next to each name a 1 is written if the student passed the exam and a 2 if the student failed.

3

Formulating Algorithms Case Study 3 (Nested

Control Structures)

slide-4
SLIDE 4

4

Formulating Algorithms Case Study 3 (Nested

Control Structures) …

slide-5
SLIDE 5

5

Formulating Algorithms Case Study 3 (Nested

Control Structures) …

slide-6
SLIDE 6

6

Formulating Algorithms Case Study 3 (Nested

Control Structures) …

slide-7
SLIDE 7

7

Formulating Algorithms Case Study 3 (Nested

Control Structures) …

slide-8
SLIDE 8

8

Formulating Algorithms Case Study 3 (Nested

Control Structures) …

slide-9
SLIDE 9

 C

provides several assignment

  • perators

for abbreviating assignment expressions.

 For example, the statement

 c = c + 3;

 can be abbreviated with the addition assignment operator += as

 c += 3;

 Any statement of the form

 variable = variable operator expression;

 where operator is one of the binary operators +, -, *, / or % (or

  • thers we’ll discuss in Chapter 10), can be written in the form

 variable operator= expression;

 Thus the assignment c += 3 adds 3 to c.

9

Assignment Operators

slide-10
SLIDE 10

 C also provides the unary increment operator, ++, and

the unary decrement operator, --.

 If a variable c is incremented by 1, the increment

  • perator ++ can be used rather than the expressions c =

c + 1 or c += 1.

 If increment or decrement operators are placed before a

variable (i.e., prefixed), they’re referred to as the preincrement or predecrement operators, respectively.

 If increment or decrement operators are placed after a

variable (i.e., postfixed), they’re referred to as the postincrement or postdecrement operators, respectively.

10

Increment and Decrement Operators

slide-11
SLIDE 11

 Preincrementing

(predecrementing) a variable causes the variable to be incremented (decremented) by 1, then the new value of the variable is used in the expression in which it appears.

 Postincrementing (postdecrementing) the variable

causes the current value of the variable to be used in the expression in which it appears, then the variable value is incremented (decremented) by 1.

 Figure 3.13 demonstrates the difference between the

preincrementing and the postincrementing versions

  • f the ++ operator.

11

Increment and Decrement Operators …

slide-12
SLIDE 12

12

Increment and Decrement Operators …

slide-13
SLIDE 13

13

Increment and Decrement Operators …

slide-14
SLIDE 14

 It’s

important to note here that when incrementing

  • r

decrementing a variable in a statement by itself, the preincrement and postincrement forms have the same effect.

 It’s only when a variable appears in the context of a larger

expression that preincrementing and postincrementing have different effects (and similarly for predecrementing and postdecrementing).

 Of the expressions we’ve studied thus far, only a simple variable

name may be used as the operand of an increment or decrement

  • perator.

 Attempting to use the increment or decrement operator on an

expression other than a simple variable name is a syntax error e.g. writing ++(x+1).

14

Increment and Decrement Operators …

slide-15
SLIDE 15

Question

 Consider following code:

int i = k; i += i++; printf(“%d”, i);

 For k = 1, what is the final value of i?

3

 Answer above question considering the second line as

i -= --i;

15

slide-16
SLIDE 16

 Counter-controlled repetition requires:

 The name of a control variable (or loop counter).  The initial value of the control variable.  The increment (or decrement) by which the control

variable is modified each time through the loop.

 The condition that tests for the final value of the control

variable (i.e., whether looping should continue).

16

Repetition Essentials

slide-17
SLIDE 17

17

Repetition Essentials …

slide-18
SLIDE 18

18

Repetition Essentials …

slide-19
SLIDE 19

 You could make the program in Fig. 4.1 more concise by

initializing counter to 0 and by replacing the while statement with

while while ( ++c ++counter <= <= 10 10 ) ) printf tf( ( "%d "%d\n" n", co , counter ); );

 This code saves a statement because the incrementing is

done directly in the while condition before the condition is tested.

 Also, this code eliminates the need for the braces around

the body of the while because the while now contains

  • nly one statement.

 Some programmers feel that this makes the code too

cryptic and error prone.

19

Repetition Essentials …

slide-20
SLIDE 20

 The for repetition statement handles all the details of

counter-controlled repetition.

 To illustrate its power, let’s rewrite the program of Fig. 4.1.  The result is shown in Fig. 4.2.

20

for Repetition Statement

slide-21
SLIDE 21

21

for Repetition Statement …

slide-22
SLIDE 22

22

for Repetition Statement …

slide-23
SLIDE 23

 Notice that Fig. 4.2 uses the loop-continuation condition

counter <= 10.

 If you incorrectly wrote counter < 10, then the loop

would be executed only 9 times.

 This is a common logic error called an off-by-one error.

23

for Repetition Statement …

slide-24
SLIDE 24

24

for Repetition Statement …

slide-25
SLIDE 25

 The general format of the for statement is

 for

for ( ( expression expression1; ; expression expression2; ; expression expression3 ) ) statement statement

where expression1 initializes the loop-control variable, expression2 is the loop-continuation condition, and expression3 increments the control variable.

 In most cases, the for statement can be represented with

an equivalent while statement as follows:

expression1; while while ( expression2 ) { statement expression3; }

25

for Repetition Statement …

slide-26
SLIDE 26

 Often, expression1 and expression3 are comma-separated

lists of expressions.

 The commas as used here are actually comma operators that

guarantee that lists of expressions evaluate from left to right.

 The comma operator is most often used in the for

statement.

 Its primary use is to enable you to use multiple initialization

and/or multiple increment expressions.

 For example, there may be two control variables in a single

for statement that must be initialized and incremented.

26

for Repetition Statement …

slide-27
SLIDE 27

 The three expressions in the for

for statement are optional.

 If expression2 is omitted, C assumes that the condition is

true, thus creating an infinite loop.

 One may omit expression1 if the control variable is

initialized elsewhere in the program.

 expression3 may be omitted if the increment is calculated by

statements in the body of the for statement or if no increment is needed.

 The increment expression in the for statement acts like a

stand-alone C statement at the end of the body of the for.

27

for Repetition Statement …

slide-28
SLIDE 28

 Therefore, the expressions

counter = counter + 1 counter += 1 ++counter counter++

are all equivalent in the increment part of the for statement.

 Many C programmers prefer the form counter++ because

the incrementing occurs after the loop body is executed, and the postincrementing form seems more natural.

 Because the variable being preincremented or postincremented

here does not appear in a larger expression, both forms of incrementing have the same effect.

 The two semicolons in the for statement are required.

28

for Repetition Statement …

slide-29
SLIDE 29

29

for Repetition Statement …