Phùng Văn Minh -20095.2 Cơ bản về lệnh lặp có biến đếm • Lệnh lặp có biến đếm – Biến điều khiển • Biến được sử dụng để xác định vòng lặp còn tiếp tục không – Giá trị khởi tạo của biến đi
Trang 2Phùng Văn Minh -2009
5.2 Cơ bản về lệnh lặp có biến đếm
• Lệnh lặp có biến đếm
– Biến điều khiển
• Biến được sử dụng để xác định vòng lặp còn tiếp tục không
– Giá trị khởi tạo của biến điều khiển
– Sự tăng/giảm của biến
– Điều kiện
• Kiểm tra việc lặp còn tiếp tục không
Trang 4Phùng Văn Minh -2009
• Cấu trúc lệnh for
– Cú pháp: for (Expression1, Expression2, Expression3)
• Expression1 = tên biến điều khiển
– Có thể chứa nhiều biến
• Expression2 = điều kiện lặp
• Expression3 = tăng hoặc giảm biến điều khiển
– Nếu Expression1 có nhiều biến, Expression3 phải có các biến tương ứng
– ++counter và counter++ là như nhau
– Miền của biến
• Expression1 có thể được dùng như là phần thân của vòng lặp
for.
• Khi vòng lặp kết thúc biến bị hủy
Trang 5Phùng Văn Minh -2009
for ( int counter = 1; counter <= 5; counter++ )
giá trị khởi tạo của biến điều khiển
Trang 6Phùng Văn Minh -2009
counter++
Khai báo khởi trị của
biến điều khiển
Xác định xem biến
điều khiển đã tới
giá trị cuối chưa counter <= 10 Console.WriteLine
Tăng biến đếm.
Hình 5.4 Sơ đồ khối của lệnh for
Trang 710 // initialization, repetition condition and incrementing
11 // are all included in the for structure
12 for ( int counter = 1 ; counter <= 5 ; counter++ )
Trang 8• Đa số các trường hợp sử dụng toán tử > hoặc >=
• Hộp thông báo - Message boxes
– Các nút - Buttons
• OK – nút OK
• OKCancel - 2 nút OK và Cancel
• YesNo – 2 nút Yes và No
• AbortRetryIgnore – 3 nút Arbort, Retry và Ignore
• YesNoCancel – 3 nút Yes, No và Cancel
• RetryCancel - 2 nút Retry và Cancel
Trang 9Phùng Văn Minh -2009
• Hộp thông báo - Massages boxes
– Các biểu tương - Icons
• Exclamation – Biểu tượng hình tam giác có dấu chấm
thang
• Question – Biểu tượng hình dấu hỏi chấm
• Error – Biểu tượng hình vòng tròn có dấu chữ thập
• Information – Biểu tượng dấu chấm thang
• Định dạng - Formatting
– (Biến : định dạng)
• Bảng 5.9 danh sách các định dạng
Trang 1016 MessageBox.Show( "The sum is " + sum,
17 "Sum Even Integers from 2 to 100" ,
to display
Trang 11Phùng Văn Minh -2009
Messa g eBox Ic ons Ic on Desc rip tion
exclamation point Typically used to caution the user against potential problems
informational message to the user
mark Typically used to ask the user a question
red circle Helps alert user of errors or important messages
Fig 5.6 Ic ons for messa ge d ia logs.
Trang 12Phùng Văn Minh -2009
Messa g eBox Buttons De sc rip tio n
MessageBoxButton OK Specifies that the dialog should include an OK button
MessageBoxButton OKCancel Specifies that the dialog should include OK and Cancel
buttons Warns the user about some condition and allows the user to either continue or cancel an operation
MessageBoxButton YesNo Specifies that the dialog should contain Yes and No
buttons Used to ask the user a question
MessageBoxButton YesNoCancel Specifies that the dialog should contain Yes, No and
Cancel buttons Typically used to ask the user a question but still allows the user to cancel the operation
MessageBoxButton RetryCancel Specifies that the dialog should contain Retry and Cancel
buttons Typically used to inform a user about a failed operation and allow the user to retry or cancel the operation
MessageBoxButton AbortRetryIgnore Specifies that the dialog should contain Abort, Retry and
Ignore buttons Typically used to inform the user that one
of a series of operations has failed and allow the user to abort the series of operations, retry the failed operation or ignore the failed operation and continue
Fig 5.7 Buttons for messa ge d ia logs
Trang 1326 MessageBox.Show( output, "Compound Interest" ,
27 MessageBoxButtons OK , MessageBoxIcon Information );
Trang 14Phùng Văn Minh - 2009.
Outline
Interest.cs
Program Output
Trang 15Phùng Văn Minh -2009
Forma t C od e Desc rip tion
C or c Formats the string as currency Precedes the number with an appropriate currency
symbol ($ in the US) Separates digits with an appropriate separator character
(comma in the US) and sets the number of decimal places to two by default
D or d Formats the string as a decimal Displays number as an integer
N or n Formats the string with commas and two decimal places
E or e Formats the number using scientific notation with a default of six decimal places
F or f Formats the string with a fixed number of decimal places (two by default)
G or g General Either E or F
X or x Formats the string as hexadecimal
Fig 5.9 string forma tting c od es
Trang 1710 char grade; // one grade
11 int aCount = 0 , // number of As
19 Console.Write( "Enter a letter grade: " );
20 grade = Char.Parse( Console.ReadLine() );
21
22 switch ( grade )
23 {
24 case 'A' : // grade is uppercase A
25 case 'a' : // or lowercase a
Cả hai trường hợp đều tăng biến đếm aCount
Câu lệnh break thoát khỏi lệnh switch
Cả case ‘B’ và case ‘b’ đều thêm 1 cho biến bCount
Vòng lặp for lặp 10 lần Mỗi biến làm việc như biến đếm khởi trị bằng 0
Trang 1851 "Incorrect letter grade entered." +
52 "\nGrade not added to totals." );
66 } // end class SwitchTest
Nếu không trùng với các trường hợp trên thì
thực hiện trường hợp default
Hiện kết quả
Trang 19Phùng Văn Minh - 2009.
Outline
SwitchTest.cs Program Output
Enter a letter grade: A
Enter a letter grade: c
Enter a letter grade: F
Enter a letter grade: z
Incorrect letter grade entered.
Grade not added to totals.
Enter a letter grade: D
Enter a letter grade: d
Enter a letter grade: B
Enter a letter grade: a
Enter a letter grade: C
Totals for each letter grade are:
Trang 21Phùng Văn Minh -2009
• Khác nhau giữa while và do/while
– Sử dụng lệnh lặp while
• Kiểm tra điều kiện
• Thực thi công việc
• Việc lặp có thể không được thực hiện lần nào
– Sử dụng lệnh lặp do/while
• Thực hiện công việc
• Kiểm tra điều kiện
• Lệnh lặp được thực hiện ít nhất một lần
• Luôn phải dùng ngoặc ({) tránh nhầm lẫn
Trang 24• Được dùng để bỏ qua phần sau của câu kệnh lặp và bắt đầu lặp
ở câu lệnh đầu của các lệnh trong lệnh lặp
Trang 2526 MessageBox.Show( output, "Demonstrating the break statement" ,
27 MessageBoxButtons OK , MessageBoxIcon Information );
Trang 26Phùng Văn Minh - 2009.
Outline
BreakTest.cs
Program Output
Trang 2724 MessageBox.Show( output, "Using the continue statement" ,
25 MessageBoxButtons OK , MessageBoxIcon Information );
Trang 28Phùng Văn Minh - 2009.
Outline
ContinueTest.cs
Program Output
Trang 29Phùng Văn Minh -2009
5.8 Các toán tử logic và điều kiện
• Các toán tử
– AND logic (&)
– AND điều kiện (&&)
Trang 30Phùng Văn Minh -2009
5.8 Các toán tử logic và điều kiện
exp ression1 exp ression2 exp ression1 &&
exp ression2
Fig 5.16 Truth ta b le for the && (logic al AND) operator
exp ression1 exp ression2 exp ression1 ||
exp ression2
Fig 5.17 Truth tab le for the || (logic al OR) operator
Trang 31Phùng Văn Minh -2009
5.8 Các toán tử logic và điều kiện
exp ression1 exp ression2 exp ression1 ^
exp ression2
Fig 5.18 Truth ta b le for the log ic a l exc lusive OR (^) operator
exp ression !exp ression
Fig 5.19 Truth ta b le for op era tor! (logic al NOT)
Trang 327 // main entry point for application
8 static void Main( string [] args )
9 {
10 // testing the conditional AND operator (&&)
11 Console.WriteLine( "Conditional AND (&&)" +
12 "\nfalse && false: " + ( false && false ) +
13 "\nfalse && true: " + ( false && true ) +
14 "\ntrue && false: " + ( true && false ) +
15 "\ntrue && true: " + ( true && true ) );
16
17 // testing the conditional OR operator (||)
18 Console.WriteLine( "\n\nConditional OR (||)" +
19 "\nfalse || false: " + ( false || false ) +
20 "\nfalse || true: " + ( false || true ) +
21 "\ntrue || false: " + ( true || false ) +
22 "\ntrue || true: " + ( true || true ) );
23
24 // testing the logical AND operator (&)
25 Console.WriteLine( "\n\nLogical AND (&)" +
26 "\nfalse & false: " + ( false & false ) +
27 "\nfalse & true: " + ( false & true ) +
28 "\ntrue & false: " + ( true & false ) +
29 "\ntrue & true: " + ( true & true ) );
30
Trang 3333 "\nfalse | false: " + ( false | false ) +
34 "\nfalse | true: " + ( false | true ) +
35 "\ntrue | false: " + ( true | false ) +
36 "\ntrue | true: " + ( true | true ) );
37
38 // testing the logical exclusive OR operator (^)
39 Console.WriteLine( "\n\nLogical exclusive OR (^)" +
40 "\nfalse ^ false: " + ( false ^ false ) +
41 "\nfalse ^ true: " + ( false ^ true ) +
42 "\ntrue ^ false: " + ( true ^ false ) +
43 "\ntrue ^ true: " + ( true ^ true ) );
44
45 // testing the logical NOT operator (!)
46 Console.WriteLine( "\n\nLogical NOT (!)" +
47 "\n!false: " + ( ! false ) +
48 "\n!true: " + ( ! true ) );
49 }
50 }
Conditional AND (&&)
false && false: False
false && true: False
true && false: False
true && true: True
Conditional OR (||)
false || false: False
false || true: True
true || false: True
true || true: True
Trang 34Phùng Văn Minh - 2009.
Outline
LogicalOperators.cs Program Output
false & false: False
false & true: False
true & false: False
true & true: True
Logical OR (|)
false | false: False
false | true: True
true | false: True
true | true: True
Logical exclusive OR (^)
false ^ false: False
false ^ true: True
true ^ false: True
true ^ true: False
Logical NOT (!)
!false: True
!true: False
Trang 36Phùng Văn Minh -2009
5.9 Cấu trúc chương trình tổng thể
• 3 dạng điều khiển cần thiết
– Có nhiều cách cài đặt các điều khiển
Trang 37Fig 5.21 Prec ed enc e a nd a ssoc ia tivity of the op era to rs d isc ussed so fa r
Trang 39If (một lựa chọn)
else/if hai lựa chọn
T F
switch (nhiều lựa
chọn)
.
break break
break
break
T T
T
F F
F
.
Trang 40while structure
T F
do/while structure F
T
for structure/foreach structure
Trang 41Phùng Văn Minh -2009
5.9 Cấu trúc chương trình tổng thể
Rules for Forming Struc tured Progra ms
1) Begin with the “simplest flowchart” (Fig 5.24)
2) Any rectangle (action) can be replaced by two rectangles (actions) in sequence
3) Any rectangle (action) can be replaced by any control structure (sequence, if,
if/else, switch, while, do/while, for or foreach, as we will see in
Chapter 8, Object-Oriented Programming)
4) Rules 2 and 3 may be applied as often as you like and in any order
Fig 5.23 Rules for forming struc tured progra ms
Trang 42Phùng Văn Minh -2009
5.9 Cấu trúc chương trình tổng thể
Hình 5.24 Sơ đồ khối cơ bản nhất
Trang 43Phùng Văn Minh -2009
5.9 Cấu trúc chương trình tổng thể
.
Hình 5.25 Thêm các lệnh tuần tự
Trang 44Phùng Văn Minh -2009
5.9 Cấu trúc chương trình tổng thể
Thêm lệnh lựa chọn
Thêm lệnh lựa chọn
Hình 5.26 Thêm lệnh lựa chọn
Trang 45Phùng Văn Minh -2009
5.9 Cấu trúc chương trình tổng thể
Hình 5.27 Các khối rời nhau, lồng nhau và chồng lênh nhau
Các khối lệnh rời nhau
Khối chồng lênh nhau (không
tốt)
Khối lồng nhau
Trang 46Phùng Văn Minh -2009
5.9 5.9 Cấu trúc chương trình tổng thể
Hình 5.28 sơ đồ khối không có cấu trúc