Giáo trình tin học chương 2
lượt xem 61
download
Control Structures • Sequential execution – Statements executed in order • Transfer of control – Next statement executed not next one in sequence • 3 control structures (Bohm and Jacopini) – Sequence structure • Programs executed sequentially by default – Selection structures • if, if/else, switch – Repetition structures • while, do/while, for
Bình luận(0) Đăng nhập để gửi bình luận!
Nội dung Text: Giáo trình tin học chương 2
- 1 Chapter 2 - Control Structures Outline Control Structures if Selection Structure if/else Selection Structure while Repetition Structure Formulating Algorithms: Case Study 1 (Counter-Controlled Repetition) Formulating Algorithms with Top-Down, Stepwise Refinement: Case Study 2 (Sentinel-Controlled Repetition) Formulating Algorithms with Top-Down, Stepwise Refinement: Case Study 3 (Nested Control Structures) Assignment Operators Increment and Decrement Operators Essentials of Counter-Controlled Repetition for Repetition Structure switch Multiple-Selection Structure do/while Repetition Structure break and continue Statements Logical Operators Confusing Equality (==) and Assignment (=) Operators Structured-Programming Summary 2003 Prentice Hall, Inc. All rights reserved.
- 2 Control Structures • Sequential execution – Statements executed in order • Transfer of control – Next statement executed not next one in sequence • 3 control structures (Bohm and Jacopini) – Sequence structure • Programs executed sequentially by default – Selection structures • if, if/else, switch – Repetition structures • while, do/while, for 2003 Prentice Hall, Inc. All rights reserved.
- 3 Control Structures • C++ keywords – Cannot be used as identifiers or variable names C++ Keyw o rd s Keywords common to the C and C++ programming languages auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while C++ only keywords asm bool catch class const_cast delete dynamic_cast explicit false friend inline mutable namespace new operator private protected public reinterpret_cast static_cast template this throw true try typeid typename using virtual wchar_t 2003 Prentice Hall, Inc. All rights reserved.
- 4 if Selection Structure • in C++ If student’s grade is greater than or equal to 60 Print “Passed” if ( grade >= 60 ) cout = 60 print “Passed” nonzero - true Example: false 3 - 4 is true 2003 Prentice Hall, Inc. All rights reserved.
- 5 if/else Selection Structure • if – Performs action if condition true • if/else – Different actions if conditions true or false • Pseudocode if student’s grade is greater than or equal to 60 print “Passed” else print “Failed” • C++ code if ( grade >= 60 ) cout
- 6 if/else Selection Structure • Ternary conditional operator (?:) – Three arguments (condition, value if true, value if false) • Code could be written: cout = 60 ? “Passed” : “Failed” ); Condition Value if true Value if false false true grade >= 60 print “Failed” print “Passed” 2003 Prentice Hall, Inc. All rights reserved.
- 7 Nested if/else structures • Example if ( grade >= 90 ) // 90 and above cout = 80 ) // 80-89 cout = 70 ) // 70-79 cout = 60 ) // 60-69 cout
- 8 if/else Selection Structure • Compound statement – Set of statements within a pair of braces if ( grade >= 60 ) cout
- 9 while Repetition Structure • Example int product = 2; while ( product
- 10 Formulating Algorithms (Counter-Controlled Repetition) • Counter-controlled repetition – Loop repeated until counter reaches certain value • Definite repetition – Number of repetitions known • Example A class of ten students took a quiz. The grades (integers in the range 0 to 100) for this quiz are available to you. Determine the class average on the quiz. 2003 Prentice Hall, Inc. All rights reserved.
- 2 // Class average program with counter-controlled repetition. 3 #include 4 using std::cout; 5 using std::cin; 6 using std::endl; 7 // function main begins program execution 8 int main() 9 { 10 int total; // sum of grades input by user 11 int gradeCounter; // number of grade to be entered next 12 int grade; // grade value 13 int average; // average of grades 14 // initialization phase 15 total = 0; // initialize total 16 gradeCounter = 1; // initialize loop counter 17 // processing phase 18 while ( gradeCounter grade; // read grade from user 21 total = total + grade; // add grade to total 22 gradeCounter = gradeCounter + 1; // increment counter 23 } 2003 Prentice Hall, Inc. All rights reserved.
- 24 // termination phase 25 average = total / 10; // integer division 26 // display result 27 cout
- 13 Formulating Algorithms (Sentinel-Controlled Repetition) • Suppose problem becomes: Develop a class-averaging program that will process an arbitrary number of grades each time the program is run – Unknown number of students – How will program know when to end? • Sentinel value – Indicates “end of data entry” – Loop ends when sentinel input – Sentinel chosen so it cannot be confused with regular input • -1 in this case 2003 Prentice Hall, Inc. All rights reserved.
- 14 Formulating Algorithms (Sentinel-Controlled Repetition) • Top-down, stepwise refinement – Begin with pseudocode representation of top Determine the class average for the quiz – Divide top into smaller tasks, list in order Initialize variables Input, sum and count the quiz grades Calculate and print the class average 2003 Prentice Hall, Inc. All rights reserved.
- 15 Formulating Algorithms (Sentinel-Controlled Repetition) • Many programs have three phases – Initialization • Initializes the program variables – Processing • Input data, adjusts program variables – Termination • Calculate and print the final results – Helps break up programs for top-down refinement 2003 Prentice Hall, Inc. All rights reserved.
- 16 Formulating Algorithms (Sentinel-Controlled Repetition) • Refine the initialization phase Initialize variables goes to Initialize total to zero Initialize counter to zero • Processing Input, sum and count the quiz grades goes to 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) 2003 Prentice Hall, Inc. All rights reserved.
- 17 Formulating Algorithms (Sentinel-Controlled Repetition) • Termination Calculate and print the class average goes to 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” 2003 Prentice Hall, Inc. All rights reserved.
- 18 Nested Control Structures • Problem statement A college has a list of test results (1 = pass, 2 = fail) for 10 students. Write a program that analyzes the results. If more than 8 students pass, print "Raise Tuition". • Notice that – Program processes 10 results • Fixed number, use counter-controlled loop – Two counters can be used • One counts number that passed • Another counts number that fail – Each test result is 1 or 2 • If not 1, assume 2 2003 Prentice Hall, Inc. All rights reserved.
- 19 Nested Control Structures • Top level outline Analyze exam results and decide if tuition should be raised • First refinement 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 • Refine Initialize variables to Initialize passes to zero Initialize failures to zero Initialize student counter to one 2003 Prentice Hall, Inc. All rights reserved.
- 20 Nested Control Structures • Refine Input the ten quiz grades and count passes and failures to While student counter is less than or equal to ten Input the next exam result If the student passed Add one to passes Else Add one to failures Add one to student counter 2003 Prentice Hall, Inc. All rights reserved.
CÓ THỂ BẠN MUỐN DOWNLOAD
-
Giáo trình Tin học đại cương: Phần 1 - ĐH Sư phạm TP.HCM
166 p | 805 | 116
-
Giáo trình Tin học đại cương: Phần 2 - ĐH Sư phạm TP.HCM
193 p | 266 | 93
-
Giáo trình Tin học văn phòng: Phần 1
23 p | 358 | 87
-
Giáo trình Tin học cơ bản: Phần 2 - Tập đoàn Microsoft
168 p | 298 | 85
-
Giáo trình Tin học đại cương A1: Phần 2 - NXB ĐHQG TP.HCM
231 p | 202 | 52
-
Giáo trình Tin học văn phòng: Phần 2
17 p | 241 | 44
-
Giáo trình Tin học (Sách dùng cho các trường dạy nghề hệ 12 - 24 tháng): Phần 1 - Tiêu Kim Cương (chủ biên)
66 p | 152 | 42
-
Giáo trình Tin học đại cương A1: Phần 1 - NXB ĐHQG TP.HCM
53 p | 174 | 41
-
Giáo trình Tin học (Sách dùng cho các trường dạy nghề hệ 12 - 24 tháng): Phần 2 - Tiêu Kim Cương (chủ biên)
207 p | 167 | 38
-
Giáo trình Tin học đại cương: Phần 2 - Đại học Sư phạm TP.HCM
230 p | 140 | 32
-
Giáo trình Tin học đại cương: Phần 2 - ĐH Nông Nghiệp I
79 p | 88 | 10
-
Giáo trình Tin học đại cương: Phần 2
79 p | 93 | 7
-
Giáo trình Tin học căn bản (Ngành Thiết kế đồ họa – Trình độ Cao đẳng) - Trường CĐ Kinh tế - Kỹ thuật Vinatex TP. HCM
214 p | 20 | 7
-
Giáo trình Tin học căn bản (Ngành/Nghề: Tiếng Anh thương mại – Trình độ Cao đẳng) - Trường CĐ Kinh tế - Kỹ thuật Vinatex TP. HCM
214 p | 24 | 6
-
Giáo trình Tin học (Trình độ: Trung cấp nghề) - Trường Trung cấp nghề Củ Chi
268 p | 36 | 6
-
Giáo trình Tin học căn bản (Ngành Thiết kế thời trang – Trình độ Cao đẳng) - Trường CĐ Kinh tế - Kỹ thuật Vinatex TP. HCM
214 p | 16 | 5
-
Giáo trình Tin học: Phần 2 - CĐ Du lịch Hà Nội
81 p | 31 | 4
-
Giáo trình Tin học (Ngành/Nghề: Công nghệ thông tin – Trình độ: Trung cấp) - Trường CĐ Kinh tế - Kỹ thuật Vinatex TP. HCM
214 p | 13 | 4
Chịu trách nhiệm nội dung:
Nguyễn Công Hà - Giám đốc Công ty TNHH TÀI LIỆU TRỰC TUYẾN VI NA
LIÊN HỆ
Địa chỉ: P402, 54A Nơ Trang Long, Phường 14, Q.Bình Thạnh, TP.HCM
Hotline: 093 303 0098
Email: support@tailieu.vn