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
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
Fall 2013
Sharif University of Technology
1
Session 7
These slides have been created using Deitel’s slides
Formulating Algorithms Case Study…
3.
2
We’ll once again formulate the algorithm using pseudocode
In this case study we’ll see the only other structured way
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
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
4
5
6
7
8
C
provides several assignment
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
variable operator= expression;
Thus the assignment c += 3 adds 3 to c.
9
C also provides the unary increment operator, ++, and
If a variable c is incremented by 1, the increment
If increment or decrement operators are placed before a
If increment or decrement operators are placed after a
10
Preincrementing
Postincrementing (postdecrementing) the variable
Figure 3.13 demonstrates the difference between the
11
12
13
It’s
important to note here that when incrementing
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
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
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
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
The condition that tests for the final value of the control
16
17
18
You could make the program in Fig. 4.1 more concise by
while while ( ++c ++counter <= <= 10 10 ) ) printf tf( ( "%d "%d\n" n", co , counter ); );
This code saves a statement because the incrementing is
Also, this code eliminates the need for the braces around
Some programmers feel that this makes the code too
19
The for repetition statement handles all the details of
To illustrate its power, let’s rewrite the program of Fig. 4.1. The result is shown in Fig. 4.2.
20
21
22
Notice that Fig. 4.2 uses the loop-continuation condition
If you incorrectly wrote counter < 10, then the loop
This is a common logic error called an off-by-one error.
23
24
The general format of the for statement is
for
for ( ( expression expression1; ; expression expression2; ; expression expression3 ) ) statement statement
In most cases, the for statement can be represented with
expression1; while while ( expression2 ) { statement expression3; }
25
Often, expression1 and expression3 are comma-separated
The commas as used here are actually comma operators that
The comma operator is most often used in the for
Its primary use is to enable you to use multiple initialization
For example, there may be two control variables in a single
26
The three expressions in the for
If expression2 is omitted, C assumes that the condition is
One may omit expression1 if the control variable is
expression3 may be omitted if the increment is calculated by
The increment expression in the for statement acts like a
27
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
29