Fall 2013
Instructor: Reza Entezari-Maleki
Email: entezari@ce.sharif.edu
Sharif University of Technology
1
Fundamentals of Programming
Session 9
These slides have been created using Deitel’s slides
Fundamentals of Programming Session 9 Instructor: Reza - - PowerPoint PPT Presentation
Fundamentals of Programming Session 9 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 dowhile Repetition Statement
Fall 2013
Sharif University of Technology
1
Session 9
These slides have been created using Deitel’s slides
2
The do…while repetition statement is similar to the
In
The do…while statement tests the loop-continuation
Therefore, the loop body will be executed at least once. When a do…while terminates, execution continues
3
4
5
The break and continue statements are used to
The break statement, when executed in a while,
Program execution continues with the next statement. Common uses of the break statement are to escape
6
Figure 4.11 demonstrates the break statement in a for
When the if statement detects that x has become 5,
This terminates the for statement, and the program
The loop fully executes only four times.
7
8
9
In while and do…while statements, the loop-
In the for statement, the increment expression is
10
Earlier, we said that the while statement could be used
The
In this case, the increment is not executed before the
Figure 4.12 uses the continue statement in a for
11
12
13
What is the output of the following code?
int i; for(i=1;i<=10;i++) { if (i==5) continue; printf("%d \n", i); } printf("End\n");
Answer:
1 2 3 4 6 7 8 9 10 End
14
What is the output of the following code?
int i; i=1; while (i<=10) { if (i==5) continue; printf("%d \n", i); i++; } printf("\nEnd\n");
Answer:
1 2 3 4
15
C provides logical operators that may be used to
The logical operators are && (logical AND), ||
We’ll consider examples of each of these operators. Suppose we wish to ensure that two conditions are
16
In this case, we can use the logical operator && as
if if ( gender nder == 1 && a && age >= 65 65 ) ++ ++seniorFem
The table shows all four possible combinations of zero
Such tables are often called truth tables.
17
18
Now let’s consider the || (logical OR) operator. Suppose we wish to ensure at some point in a program
In this case, we use the || operator as in the following
if if ( ( semest mesterAverage age >= >= 90 90 || || finalExa finalExam >= >= 90 90 ) printf printf( ( "St "Student gra grade is A\n" n" ); );
19
The && operator has a higher precedence than ||. Both operators associate from left to right. An expression containing && or || operators is evaluated
Thus, evaluation of the condition
gender == 1 && && age age >= >= 65 65
will stop if gender is not equal to 1 (i.e., the entire
20
int a=3, b=4, c=4; if (c==3 || b==4 && a==3) printf("%d", (c>=b>=a ? 100 : 200));
200
21
C provides ! (logical negation) to enable a programmer to “reverse”
the meaning of a condition.
Unlike operators && and ||, which combine two conditions (and are
therefore binary operators), the logical negation operator has only a single condition as an operand (and is therefore a unary operator).
The logical negation operator is placed before a condition when
we’re interested in choosing a path of execution if the original condition (without the logical negation operator) is false, such as in the following program segment:
if if ( ! ( !( gr grade ade == == se senti ntinel nelValu alue ) ) ) ) prin rintf tf( ( "Th "The e nex next t gra grade de is % s %f\n" n", g , gra rade de ); );
The parentheses around the condition grade == sentinelValue
are needed because the logical negation operator has a higher precedence than the equality operator.
22
In most cases, you can avoid using logical negation by
For example, the preceding statement may also be
if if ( grade != ( grade != sentinelValue sentinelValue ) printf printf( ( "The next grade is %f "The next grade is %f\n" n", grade ); , grade );
23
There is one type of error that C programmers, no
That error is accidentally swapping the operators ==
What makes these swaps so damaging is the fact that
Rather, statements with these errors ordinarily compile
24
For example, suppose we intend to write
if if ( ( payCode payCode == == 4 ) printf printf( ( "You get a bonus!" "You get a bonus!" ); );
if if ( ( payCode payCode = = 4 ) printf printf( ( "You get a bonus!" "You get a bonus!" ); );
The first if statement properly awards a bonus to the
The second if statement—the one with the error—
25
26
The other side of the coin can be equally unpleasant. Suppose you want to assign a value to a variable with a
x = 1;
x == 1;
Here, too, this is not a syntax error. Rather the compiler simply evaluates the conditional
27
21 31 41 51 60
28
Figure
Small circles are used in the figure to indicate the single
Connecting individual flowchart symbols arbitrarily can
For simplicity, only single-entry/single-exit control
29
30
31
32
Figure 4.18 shows the rules for forming structured programs. Applying the rules of Fig. 4.18 always results in a structured
Notice that Rule 2 generates a stack of control statements; so
Rule 3 is called the nesting rule. Repeatedly applying Rule 3 to the simplest flowchart results
33
Rule 4 generates larger, more involved, and more
The flowcharts that emerge from applying the rules in
34
35
36
37