Ngôn ngữ lập trình C++ - Chương 2: Các kiểu dữ liệu cơ bản - Các cấu trúc điều khiển

Phép tăng - Increment operator (++)

có thể được dùng thay cho c += 1

Phép giảm - Decrement operator (--)

có thể được dùng thay cho c -= 1

Tăng/giảm trước – Preincrement/Predecrement

++c hoặc --c

Giá trị của biến bị thay đổi, sau đó biểu thức chứa nó được tính giá trị.

Biểu thức có giá trị là giá trị của biến sau khi tăng/giảm

Tăng/giảm sau - Postincrement/Predecrement

c++ hoặc c--

Biểu thức chứa biến được thực hiện, sau đó biến được thay đổi.

Biểu thức có giá trị là giá trị của biến trước khi tăng/giảm

 

ppt76 trang | Chuyên mục: C/C++ | Chia sẻ: dkS00TYs | Lượt xem: 1589 | Lượt tải: 1download
Tóm tắt nội dung Ngôn ngữ lập trình C++ - Chương 2: Các kiểu dữ liệu cơ bản - Các cấu trúc điều khiển, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
 động 10 byte bool	giá trị Boolean, true hoặc false wchar_t	ký tự 2 byte, lưu bảng chữ cái quốc tế 2.2 Các phép toán cơ bản phép gán – assignation (=) 	x = 5;	//x: lvalue, 5: rvalue là biểu thức có giá trị là giá trị được gán các phép toán số học - Arithmetic operators (+, -, *, /, %) các phép gán kép - Compound assignation operators (+=, -=, *=, /=, %=, >>=, , =, > ). 2.2 Các phép gán tắt Các biểu thức gán tắt - Assignment expression abbreviations Phép gán cộng c = c + 3; viết tắt thành c += 3; Các lệnh có dạng variable = variable operator expression; có thể được viết lại thành variable operator= expression; Các phép gán khác d -= 4 (d = d - 4) e *= 5 (e = e * 5) f /= 3 (f = f / 3) g %= 9 (g = g % 9) 2.2 Các phép tăng và giảm Phép tăng - Increment operator (++) có thể được dùng thay cho c += 1 Phép giảm - Decrement operator (--) có thể được dùng thay cho c -= 1 Tăng/giảm trước – Preincrement/Predecrement ++c hoặc --c Giá trị của biến bị thay đổi, sau đó biểu thức chứa nó được tính giá trị. Biểu thức có giá trị là giá trị của biến sau khi tăng/giảm Tăng/giảm sau - Postincrement/Predecrement c++ hoặc c-- Biểu thức chứa biến được thực hiện, sau đó biến được thay đổi. Biểu thức có giá trị là giá trị của biến trước khi tăng/giảm 2.2 Các phép tăng và giảm Ví dụ: nếu c = 5 cout 4 5 using std::cout; 6 using std::endl; 7 8 // function main begins program execution 9 int main() 10 { 11 int c; // declare variable 12 13 // demonstrate postincrement 14 c = 5; // assign 5 to c 15 cout = 65 ) ++seniorFemales; || (logical OR) true nếu ít nhất một trong hai điều kiện là true 	if ( semesterAverage >= 90 || finalExam >= 90 ) cout y thì in y ra màn hình nếu không, in x ra màn hình Các cấu trúc điều khiển - Control StructuresKhái niệm Thực thi tuần tự - Sequential execution Các lệnh được thực hiện theo thứ tự tuần tự Chuyển điều khiển - Transfer of control Lệnh tiếp theo được thực thi không phải lệnh tiếp theo trong chuỗi lệnh. 3 cấu trúc điều khiển Cấu trúc tuần tự - Sequence structure theo mặc định, chương trình chạy tuần tự từng lệnh Các cấu trúc chọn lựa - Selection structures if, if/else, switch Các cấu trúc lặp - Repetition structures while, do/while, for Các cấu trúc điều khiển Các từ khóa của C++ Không thể dùng làm tên biến hoặc tên hàm Các cấu trúc điều khiển Sơ đồ khối - Flowchart mô tả thuật toán bằng hình vẽ gồm các ký hiệu đặc biệt được nối bằng các mũi tên (flowlines) Hình chữ nhật (ký hiệu hành động) kiểu hành động bất kỳ ký hiệu oval Bắt đầu hoặc kết thúc một chương trình, hoặc một đoạn mã (hình tròn) Các cấu trúc điều khiển có đúng 1 đầu vào, 1 đầu ra Kết nối đầu ra của một cấu trúc điều khiển với đầu vào của cấu trúc tiếp theo xếp chồng các cấu trúc điều khiển Cấu trúc lựa chọn if Cấu trúc lựa chọn - Selection structure chọn giữa các tuyến hành động khác nhau ví dụ bằng mã giả: If student’s grade is greater than or equal to 60 	Print “Passed” Nếu điều kiện thỏa mãn (có giá trị true) lệnh Print được thực hiện, chương trình chạy tiếp lệnh tiếp theo Nếu điều kiện không thỏa mãn (có giá trị false) lệnh Print bị bỏ qua, chương trình chạy tiếp Cách viết thụt đầu dòng làm chương trình dễ đọc hơn C++ bỏ qua các ký tự trắng (tab, space, etc.) Cấu trúc lựa chọn if Dịch sang C++ If student’s grade is greater than or equal to 60 Print “Passed” 	if ( grade >= 60 )  cout = 60 )  cout = 60 ? “Passed” : “Failed” ); Cấu trúc chọn lựa if/else Các cấu trúc if/else lồng nhau lệnh này nằm trong lệnh kia, kiểm tra nhiều trường hợp Một khi điều kiện thỏa mãn, các lệnh khác bị bỏ qua if student’s grade is greater than or equal to 90 Print “A” else  if student’s grade is greater than or equal to 80	 Print “B”	else  if student’s grade is greater than or equal to 70 	 Print “C”	 else 	 if student’s grade is greater than or equal to 60 	 Print “D” else Print “F” Cấu trúc chọn lựa if/else Ví dụ	 	if ( grade >= 90 ) // 90 and above cout = 80 ) // 80-89 cout = 70 ) // 70-79 cout = 60 ) // 60-69 cout = 60 ) cout 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 // function main begins program execution 10 int main() 11 { 12 int total; // sum of grades input by user 13 int gradeCounter; // number of grade to be entered next 14 int grade; // grade value 15 int average; // average of grades 16 17 // initialization phase 18 total = 0; // initialize total 19 gradeCounter = 1; // initialize loop counter 20 fig02_07.cpp(2 of 2)fig02_07.cppoutput (1 of 1) 21 // processing phase 22 while ( gradeCounter > grade; // read grade from user 25 total = total + grade; // add grade to total 26 gradeCounter = gradeCounter + 1; // increment counter 27 } 28 29 // termination phase 30 average = total / 10; // integer division 31 32 // display result 33 cout 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 using std::fixed; 9 10 #include // parameterized stream manipulators 11 12 using std::setprecision; // sets numeric output precision 13 14 // function main begins program execution 15 int main() 16 { 17 int total; // sum of grades 18 int gradeCounter; // number of grades entered 19 int grade; // grade value 20 21 double average; // number with decimal point for average 22 23 // initialization phase 24 total = 0; // initialize total 25 gradeCounter = 0; // initialize loop counter fig02_09.cpp(2 of 3) 26 27 // processing phase 28 // get first grade from user 29 cout > grade; // read grade from user 31 32 // loop until sentinel value read from user 33 while ( grade != -1 ) { 34 total = total + grade; // add grade to total 35 gradeCounter = gradeCounter + 1; // increment counter 36 37 cout > grade; // read next grade 39 40 } // end while 41 42 // termination phase 43 // if user entered at least one grade ... 44 if ( gradeCounter != 0 ) { 45 46 // calculate average of all grades entered 47 average = static_cast( total ) / gradeCounter; 48 fig02_09.cpp(3 of 3)fig02_09.cppoutput (1 of 1) 49 // display average with two digits of precision 50 cout 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 // function main begins program execution 10 int main() 11 { 12 // initialize variables in declarations 13 int passes = 0; // number of passes 14 int failures = 0; // number of failures 15 int studentCounter = 1; // student counter 16 int result; // one exam result 17 18 // process 10 students using counter-controlled loop 19 while ( studentCounter > result; 24 fig02_11.cpp(2 of 2) 25 // if result 1, increment passes; if/else nested in while 26 if ( result == 1 ) // if/else nested in while 27 passes = passes + 1; 28 29 else // if result not 1, increment failures 30 failures = failures + 1; 31 32 // increment studentCounter so loop eventually terminates 33 studentCounter = studentCounter + 1; 34 35 } // end while 36 37 // termination phase; display number of passes and failures 38 cout 8 ) 43 cout 4 5 using std::cout; 6 using std::endl; 7 8 // function main begins program execution 9 int main() 10 { 11 int counter = 1; // initialization 12 13 while ( counter 4 5 using std::cout; 6 using std::endl; 7 8 // function main begins program execution 9 int main() 10 { 11 // Initialization, repetition condition and incrementing 12 // are all included in the for structure header. 13 14 for ( int counter = 1; counter 4 5 using std::cout; 6 using std::endl; 7 8 // function main begins program execution 9 int main() 10 { 11 int sum = 0; // initialize sum 12 13 // sum even integers from 2 through 100 14 for ( int number = 2; number 4 5 using std::cout; 6 using std::endl; 7 using std::ios; 8 using std::fixed; 9 10 #include 11 12 using std::setw; 13 using std::setprecision; 14 15 #include // enables program to use function pow 16 17 // function main begins program execution 18 int main() 19 { 20 double amount; // amount on deposit 21 double principal = 1000.0; // starting principal 22 double rate = .05; // interest rate 23 fig02_21.cpp(2 of 2) 24 // output table column heads 25 cout ( 'a' ) 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 // function main begins program execution 10 int main() 11 { 12 int grade; // one grade 13 int aCount = 0; // number of As 14 int bCount = 0; // number of Bs 15 int cCount = 0; // number of Cs 16 int dCount = 0; // number of Ds 17 int fCount = 0; // number of Fs 18 19 cout 4 5 using std::cout; 6 using std::endl; 7 8 // function main begins program execution 9 int main() 10 { 11 int counter = 1; // initialize counter 12 13 do { 14 cout 4 5 using std::cout; 6 using std::endl; 7 8 // function main begins program execution 9 int main() 10 { 11 12 int x; // x declared here so it can be used after the loop 13 14 // loop 10 times 15 for ( x = 1; x 4 5 using std::cout; 6 using std::endl; 7 8 // function main begins program execution 9 int main() 10 { 11 // loop 10 times 12 for ( int x = 1; x <= 10; x++ ) { 13 14 // if x is 5, continue with next iteration of loop 15 if ( x == 5 ) 16 continue; // skip remaining code in loop body 17 18 cout << x << " "; // display value of x 19 20 } // end for structure 21 22 cout << "\nUsed continue to skip printing the value 5" 23 << endl; 24 25 return 0; // indicate successful termination 26 27 } // end function main 1 2 3 4 6 7 8 9 10 Used continue to skip printing the value 5 Lập trình cấu trúcStructured-Programming Lập trình cấu trúc – Structured programming Chương trình dễ hiểu, test, tìm lỗi (debug) và dễ sửa đổi hơn Các quy tắc lập trình cấu trúc Chỉ sử dụng các cấu trúc điều khiển một đầu vào một đầu ra Quy tắc 1) Bắt đầu bằng một sơ đồ khối đơn giản nhất 2) Mỗi hình chữ nhật (hành động) có thể được thay bằng một chuỗi gồm 2 hình chữ nhật khác 3) Mỗi hình chữ nhật (hành động) có thể được thay bằng một cấu trúc điều khiển tùy ý (tuần tự, if, if/else, switch, while, do/while hoặc for) 4) Các quy tắc 2 và 3 có thể được áp dụng nhiều lần và theo thứ tự tùy ý       Lập trình cấu trúcStructured-Programming Mô tả quy tắc 3 (thay một hình chữ nhật tùy ý bằng một cấu trúc điều khiển) 

File đính kèm:

  • pptNgôn ngữ lập trình C++ - Chương 2 Các kiểu dữ liệu cơ bản - Các cấu trúc điều khiển.ppt
Tài liệu liên quan