Transcript Chapter 4

1
4.3
Essentials of Counter-Controlled
Repetition
• Example:
int counter = 1;
// initialization
while ( counter <= 10 ) { // repetition condition
printf( "%d\n", counter );
++counter;
// increment
}
– The statement
int counter = 1;
• Names counter
• Declares it to be an integer
• Reserves space for it in memory
• Sets it to an initial value of 1
 2000 Prentice Hall, Inc.
All rights reserved.
1
/* Fig. 4.5: fig04_05.c
2
3
Summation with for */
2
Outline
#include <stdio.h>
1. Initialize variables
4
5
int main()
6
{
7
2. for repetition
structure
int sum = 0, number;
8
9
10
for ( number = 2; number <= 100; number += 2 )
sum += number;
11
12
printf( "Sum is %d\n", sum );
13
14
return 0;
15 }
Sum is 2550
Program Output
 2000 Prentice Hall, Inc.
All rights reserved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/* Fig. 4.7: fig04_07.c
Counting letter grades */
#include <stdio.h>
3
Outline
1. Initialize variables
int main()
{
int grade;
int aCount = 0, bCount = 0, cCount = 0,
dCount = 0, fCount = 0;
printf(
printf(
"Enter the letter grades.\n"
"Enter –1 to exit.\n" );
2. Input data
);
2.1 Use switch loop to
update count
while ( ( grade = getchar() ) != ‘-1’ ) {
switch ( grade ) {
/* switch nested in while */
case 'A': case 'a':
++aCount;
break;
/* grade was uppercase A */
/* or lowercase a */
case 'B': case 'b':
++bCount;
break;
/* grade was uppercase B */
/* or lowercase b */
case 'C': case 'c':
++cCount;
break;
/* grade was uppercase C */
/* or lowercase c */
case 'D': case 'd':
++dCount;
break;
/* grade was uppercase D */
/* or lowercase d */
 2000 Prentice Hall, Inc.
All rights reserved.
33
4
34
case 'F': case 'f':
35
++fCount;
36
break;
/* grade was uppercase F */
/* or lowercase f */
2.1 Use switch loop to
update count
37
38
case '\n': case' ':
39
Outline
/* ignore these in input */
3. Print results
break;
40
41
default:
/* catch all other characters */
42
printf( "Incorrect letter grade entered." );
43
printf( " Enter a new grade.\n" );
44
break;
45
46
}
}
47
48
printf( "\nTotals for each letter grade are:\n" );
49
printf( "A: %d\n", aCount );
50
printf( "B: %d\n", bCount );
51
printf( "C: %d\n", cCount );
52
printf( "D: %d\n", dCount );
53
printf( "F: %d\n", fCount );
54
55
56 }
return 0;
 2000 Prentice Hall, Inc.
All rights reserved.
5
Enter the letter grades.
Enter the EOF character to end input.
A
B
C
C
A
D
F
C
E
Incorrect letter grade entered. Enter a new grade.
D
A
B
Outline
Program Output
Totals for each letter grade are:
A: 3
B: 2
C: 3
D: 2
F: 1
 2000 Prentice Hall, Inc.
All rights reserved.
1
/* Fig. 4.9: fig04_09.c
2
3
6
Outline
Using the do/while repetition structure */
#include <stdio.h>
1. Initialize variable
4
5
int main()
6
{
7
2. Loop
3. Print
int counter = 1;
8
9
do {
10
printf( "%d
11
", counter );
} while ( ++counter <= 10 );
12
13
return 0;
14 }
1
2
3
4
5
6
7
8
9
10
Program Output
 2000 Prentice Hall, Inc.
All rights reserved.
7
4.10 Logical Operators
• && ( logical AND )
– Returns true if both conditions are true
• || ( logical OR )
– Returns true if either of its conditions are true
• ! ( logical NOT, logical negation )
– Reverses the truth/falsity of its condition
– Unary operator, has one operand
• Useful as conditions in loops
Expression
true && false
true || false
!false
Result
false
true
true
 2000 Prentice Hall, Inc.
All rights reserved.
8
4.11 Confusing Equality (==) and
Assignment (=) Operators
• lvalues
– Expressions that can appear on the left side of an equation
– Their values can be changed, such as variable names
• x = 4;
• rvalues
– Expressions that can only appear on the right side of an
equation
– Constants, such as numbers
• Cannot write 4 = x;
• Must write x = 4;
– lvalues can be used as rvalues, but not vice versa
• y = x;
 2000 Prentice Hall, Inc.
All rights reserved.