Title: Chapter 2 - Control Structures
1??????? ????? ? ?????- 2
2??? ???? ????? ??????
- ????? ?????
- ??????? ?????? ?? ?????? ?? ????? ??? ???? ????
?? ???? - ????? ???? ? ?????? ??
- ????? ????? ?? ???? ?? ????? ????? ????? ?????
???? ???? - ??????? ??? ?????? ?? ?? ???? ?? ??????????? ??
???? - ??????? ????? if, if-else, switch
- ??????? ??? ? ??????if, if-else, switch
- ??????? ????? ????? while, do/while , for
3????? ?? ??????? ?? ???????
- ????? ?? ??????? ?? ???????
- ???? ?? ????? ????? ?? ??? ?? ????? ??????? ??
????? ???? ???? - ????? ?? ???????? ????? ???? ???
- ????? ????? ????? ??????? ???? ?? ?? ???? ?????
- ????
- ???? ???? 10 ???? ??????? ?????? ???? ???????
????? ??? ???? ?? ????? ????
4????? ?? ??????? ?? ???????
- ???????? ????
- Set total to zero
- Set grade counter to one
- While grade counter is less than or equal to ten
- Input the next grade
- Add the grade into the total
- Add one to the grade counter
- Set the class average to the total divided by
tenPrint the class average
5?????? ???? ???
- int total // sum of grades entered by user
- int gradeCounter // number of the grades to be
entered next - int grade // grade value entered by user
- int average // average of grades
- // initialization phase
- total 0 // initialize total
- gradeCounter 1 // initialize loop counter
- // processing phase
- while ( gradeCounter lt 10 ) // loop 10 times
- cout ltlt "Enter grade " // prompt for input
- cin gtgt grade // input next grade
- total total grade // add grade to
total - gradeCounter gradeCounter 1 //
increment counter by - // end while
- // termination phase
- average total / 10 // integer division yields
integer result - // display total and average of grades
- cout ltlt "\nTotal of all 10 grades is " ltlt total
ltlt endl - cout ltlt "Class average is " ltlt average ltlt endl
6????? ?? ???? (??????/?????)
- ?????
- ?????? ?? ??????? ?? ??????? ????? ???? ?? ??????
???. ????? ????????? ???? ?? ????? ????? ??????
???? ??? ?????? ?? ??? ???? - ???? ????? ( ?????)
- ????? ???? ???? ?? ?? ???? ?? ???
- ???? ?? ???? ????? ????? ????? ?? ????
- ????? ???? ???? ???? ?????? ??? ?? ?? ?????? ????
???? ?????? ????
7????? ???? ?? ????? Top-Down Design
- ??? ??? ????? ?? ?? ???? ??? ?? ???? ????
- Determine the class average for the quiz
- ??? ??? ??? ?? ?? ????? ?????? ??????
- Initialize variables
- Input, sum and count the quiz grades
- Calculate and print the class average
8????? ???? ?? ????? ????????
- ???? ???? ?????? ?? ????? ?? ?? ??? (???) ???
- ????? ?? ????? ???? Initialization
- ?????? ????? ???????? ?????? ????? ?? ????
- ??????
- ?????? ??????? ?? ????? ?????? ??? ? ????? ????
??? ???? ????? ?? ???? - ?????
- ????? ????? ?????? ? ??? ?? ???
- ????? ???? ?? ????? ? ????? ??? ?? ??? ??? ?????
- Initialize variables
- ??
- Initialize total to zero
- Initialize counter to zero
9????? ??? ?? ???
- ?????
- Input, sum and count the quiz grades
- ??
- Input the first grade (possibly the sentinel)
- While the user has not as yet entered the
sentinel - Add this grade into the running total
- Add one to the grade counter
- Input the next grade (possibly the sentinel)
- ?????
- Calculate and print the class average
- ??
- If the counter is not equal to zero
- Set the average to the total divided by the
counter - Print the average
- Else
- Print No grades were entered
10???? ????? ?? ????
- cout ltlt "Enter grade or -1 to quit "
- cin gtgt grade
- while ( grade ! -1 )total total grade
- gradeCounter gradeCounter 1 cout ltlt "Enter
grade or -1 to quit " - cin gtgt grade
- // end while
11????????? ?????? ?? ?? ??
- ?????
- ????? ?? ????? ????? ??????????(10 ???) ?? ?????
???? ???. ?????? ?? ??????? ?? ??? 8 ??? ?? ?????
???? ??? ?????? ????? ????? ?? ?????? ???? ???
???. ????? ?? ??? ?? 1 (????) ?? 2( ?????) ?????
?? ??? - ?? ???? ??? ??
- ??? ?????? ????? 10 ??? ?? ?????? ?? ??? ?? ??
???? ????????? ?? ???? ???? - ?? ???? ?? ?? ??????? ??????? ??? ??? ????
????? ????? ????????? ? ????? ???????? ?????
???????. - ????? ???????? 1 ?? 2 ???. ??? 1 ???? ? ??? ??
???? 2 ??? - ???? ??? ????? ?????
- Analyze exam results and decide if tuition should
be raised
12????????? ?????? ?? ?? ??
- ????? ?????
- Initialize variables
- Input the ten quiz grades and count passes and
failures - Print a summary of the exam results and decide
if tuition should be raised - ?????
- Initialize variables
- ??
- Initialize passes to zero
- Initialize failures to zero
- Initialize student counter to one
13????????? ?????? ?? ?? ??
- ?????
- Input the ten quiz grades and count passes and
failures - ??
- While student counter is less than or equal to
tenInput the next exam result - If the student passed
- Add one to passesElse Add one to failures
- Add one to student counter
- ?????
- Print a summary of the exam results and decide if
tuition should be raised - ??
- Print the number of passes
- Print the number of failures
- If more than eight students passed Print Raise
tuition
14- 1. Initialize variables
- 2. Input data and count passes/failures
15- 3. Print results
- Program Output
Enter result (1pass,2fail) 1 Enter result
(1pass,2fail) 1 Enter result (1pass,2fail)
1 Enter result (1pass,2fail) 1 Enter result
(1pass,2fail) 2 Enter result (1pass,2fail)
1 Enter result (1pass,2fail) 1 Enter result
(1pass,2fail) 1 Enter result (1pass,2fail)
1 Enter result (1pass,2fail) 1 Passed 9 Failed
1 Raise tuition
16???????? ????? ??????
- ?????? ???????c c 3 ?? ?? ???? ?? ???? c
3 ???? - ?????? ?? ??? ???
- variable variable operator expression
- ?? ?? ???? ?? ???
- variable operator expression
- ??? ????
- d - 4 (d d - 4)
- e 5 (e e 5)
- f / 3 (f f / 3)
- g 9 (g g 9)
17???????? ?????? ? ????
- ?? ???? ?? ??? c c1 ?? ????? ??????? ???? ?
???? ???? c - ?? ???? ????? ?? ???? c-- ?? ?? ??? c -1
??????? - Preincrement
- ????? ??? ?? ????? ???? ?? ???? (c or --c)
- ????? ????? ????? ????? ?? ??? ? ??? ??? ??????
??? ??? ?? ????? ?? ???? - Posincrement
- ????? ??? ?? ????? ???? ?? ???? (c or c--)
- ??? ?????? ??? ??? ????? ????? ?? ????? ??? ?????
?? ????? ?? ??? ??? ????? ???? ????? c ????? 5
???? - ????? cout ltlt c ??? 6 ?? ??? ?? ???. ?????
????? c ?????? ????? ? ??? ??? ?? ???. - ????? cout ltlt c ??? 5 ?? ??? ?? ???. ?????
????? c ??? ??? ? ??? ?????? ?? ????.
18???????? ?????? ? ????
- ??? ????? ?? ?????? ??????? ???? ?????
Preincrement ? Postincrement??? ?????? ????? - c
- cout ltlt c
- and
- c
- cout ltlt c
19??????? ????? ?? ???????
- ??????? ????? ?? ???????
- ????? ??????? ???? ???????
- ????? ????? ?????
- ???? ?? ????? ???? ?? ?? ???? ????? ??????? ?????
??? - ??? ?????? ?? ???? ??????? ?? ?? ?? ??? ?????
???? ???? ?? ??? - ????
- int counter 1 //initialization
- while (counter lt 10) //repetition condition
- cout ltlt counter ltlt endl
- counter //increment
-
20??????? ????? ?? ???????
- ?????
- int counter 1
- ????? counter ?? ????? ?? ???
- ????? ?? ??? ??? ?? integer ???
- ???? ?? ????? ???????? ?? ?? ??? ????? ?????? ??
??? - ????? ?????? ?? ?? ????? 1 ???? ?? ???
21???? ????? for
- ??? ??? ???? ????? for ???? ???
- for ( initialization LoopContinuationTest
increment ) - statement
- ????
- for( int counter 1 counter lt 10 counter )
- cout ltlt counter ltlt endl
- ????? ?? 1 ?? 10 ?? ??? ?? ???
22???? ????? for
- ???? for ?? ?? ???? ?? ???? while ?????? ???????
??? - initialization
- while ( loopContinuationTest)
- statement
- increment
-
- ?? ???? ????? ????? ?? ?? ??? ????? ???? ? ???
???? ????? ???? - for (int i 0, j 0 j i lt 10 j, i)
- cout ltlt j i ltlt endl
23???? ????? for - ????
- Program to sum the even numbers from 2 to 100
24?????? ?????? switch
- ??????? ?? ??? ????? ????? ???? ??? ?? ?????
????? ????? ?? ????? ???? ???? ?????
Â
25- 1. Initialize variables
- 2. Input data
- 2.1 Use switch loop to update count
26- 2.1 Use switch loop to update count
- 3. Print results
27 Enter the letter grades. Enter the EOF
character to end input. a B c C A d f C E Incorrec
t letter grade entered. Enter a new
grade. D A b  Totals for each letter grade are
A 3 B 2 C 3 D 2 F 1
28???? ????? do/while
- ???? ????? do/while ????? ???? whiles ???
- ??? ????? ?? ????? ???? ? ?? ?? ????? ???? ??
????? ?? ??? - ?????? ??????
- do
- statement
- while ( condition )
- ???? (??? ???? ????? ????? counter 1 )
- do
- cout ltlt counter ltlt " "
- while (counter lt 10)
- ??? ???? ????? ?? 1 ?? 10 ?? ??? ?? ???
- ??? ??????? ???? ????? ????? ???? ?? ????
Â
29??????? break
- Break
- ?? ???? ??? ?????? ?????? ???????? ?? ???? ??
?????? ?????? ???? ?? ??? - ????? ?????? ?? ????? ??? ?? ???? ?? ??????
?????? ????? ?? ????. - ?? ??? ??? ???? ?????? ?? ??? ????? ??????? ?????
??? ?? ????? ??? ?? ????? ???? ???
30????? continue
- Continue
- ?? ??????? ?? ??? ?????? ????? ????? ???? ????
????? ????? ? ????? ???? ?? ???? ?? ???. - ?? ??? ??? ??? ??? ?????? ???? ?? ??????? ?? ???
????? ??????
31???????? ?????
- (logical AND)
- Returns true if both conditions are true
- (logical OR)
- Returns true if either of its conditions are true
- ! (logical NOT, logical negation)
- ????? ????? ???? ?? ????? ?? ???
- ????? ??? ???. ???? ??? ?? ?????? ??? ?? ???
- ???? ???? ????? ????? ??
- Expression Result
- true false falsetrue false
true!false true
32????? ????? ? ??????
- ???? ?????? ?????? ?? ?????? ??? ?? ?? ?? ??? ??
??????? ?? ????. - ??? ?????? ???? ??????? ????? ??? ???? ????
- ?? ?????? ?? ????? ????? ??? ?? ????? ?? ???????
??? ? ????? ??????? ??? - ????? ??? ????? false ? ????? ??? ??? ????? true
??????? ?? ??? - ????
- if ( payCode 4 )
- cout ltlt "You get a bonus!" ltlt endl
- If was replaced with
- if ( payCode 4 ) cout ltlt "You get a bonus!" ltlt
endl - Sets paycode to 4
- 4 is nonzero, so the expression is true and a
bonus is awarded, regardless of paycode.
33????? ????? ? ??????
- Lvalues
- ??????? ????? ?? ?? ??? ?? ????? ?????? ???? ??
????? - ????? ???? ???? ????? ???
- ??????? ???? ???? ?? ??? ???? ???x4
- Rvalues
- ??????? ????? ?? ???? ?? ??? ???? ????? ??????
???? ??????? ??? - ?????? ???? ???? ?? ??? ???? ??? pi3.1515
- Lvalue ?? ?????? ?? ??? rvalue ??????? ??? ???
????? ?? ???? ????
34?????? ????? ???? ?????
- ??? ????? ????? ???? ????? ?? ????? ?? ??????
????? ????????? ????? ??? - ?????? ??? ? ????? ????? ?????? ??? ?????????
???? ??? - ????? ?????? ????? ?????????
- ??????? ?????? ??????? ??? ?? ?????? ?? ????
????? ? ?? ???? ????? ?????. single-entry,
single-exit - ??? ?????? ?????
- ?? ???? ???? ??????? ???? ???? ? ?? ???? ??
?????? ???? ?? ?? ?? ?????? ???? ???? - ?? ?????? ?? ?? ?????? ?? ??? ?????? ???? ????
???? ??????? ????? ????? ???? - ?? ?????? ?? ?? ?????? ?? ?? ?????? ??????
??????? ???? - ?????? 2? 3?? ?? ????? ? ????? ???? ??? ??????
??? ????? ???? ?? ?????? ?????? ?? ?? ???? ????
??? ????
Â
Â
35?????? ????? ???? ?????
36?????? ????? ???? ?????
- ??? ?????? ?? ?? ?? ???? ?? ?????? ??????? ?????
??? - Sequence ?????
- Selection ??????
- if, if/else, or switch
- ?? ??????? ?? ?? ???? ?? ????? if ????? ???? ???
- Repetition ?????
- while, do/while or for
- ?? ??? ????? ????? ?? ???? ?? while ????? ????
???.
37?????
- ?? ??????? ?? ??????? ????? ? ?????? ?????? ??
??????? ?? ??? ????? N??? ?????? ??? ? ??? ?? ??
?? ???? ??????? ????? ???? ??? ???? ????? ???. - ???? 100 2255