chƯƠng 5 cẤu trÚc ĐiỀu khiỂn
DESCRIPTION
CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN. Các phát biểu lặp. Phát biểu lặp được dùng khi thực hiện nhiều lần một “công việc” nào đó. expr. S. Đ. statement1 statement2. other statements. Phát biểu while. Phát biểu while. Dạng của phát biểu while : while ( expr ) { // statements; }. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/1.jpg)
CHƯƠNG 5
CẤU TRÚC ĐIỀU KHIỂN
![Page 2: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/2.jpg)
Các phát biểu lặp
Phát biểu lặp được dùng khi thực hiện nhiều lần
một “công việc” nào đó.
![Page 3: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/3.jpg)
Phát biểu while
expr
statement1statement2
...
other statements
S
Đ
![Page 4: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/4.jpg)
Dạng của phát biểu while:
while (expr)
{
//statements;
}
Phát biểu while
![Page 5: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/5.jpg)
![Page 6: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/6.jpg)
Khi while thực thi :
B1. Tính toán biểu thức expr.
B2. Nếu kết quả của expr là TRUE (0), thì sang B3.
Nếu kết quả của expr là FALSE (=0), thì sang B4.
B3. Thực thi statement1, statement2,… thân của
while. Quay trở về B1.
B4. Kết thúc while.
(các phát biểu sau while tiếp tục thực thi).
Phát biểu while
![Page 7: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/7.jpg)
![Page 8: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/8.jpg)
![Page 9: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/9.jpg)
![Page 10: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/10.jpg)
Cần xác định các yếu tố:
- Điều kiện lặp = điều kiện “làm” điều kiện “dừng”.
- Làm những gì ?
- Yếu tố làm expr thay đổi ?
Phát biểu while
![Page 11: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/11.jpg)
Tính tổng: n
S1
...3
1
2
11
Lặp không dừng !!
![Page 12: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/12.jpg)
S = 1 (?!!)
![Page 13: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/13.jpg)
![Page 14: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/14.jpg)
Có bao nhiêu số dương < N chia hết cho 3 ?
![Page 15: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/15.jpg)
Hãy cho biết số nguyên N có bao nhiêu chữ số ?
n = 7023 n = 702 n = 70
n = 7
n = 0
(1) (2)
(3)
(4)
/10 /10
/10
/10
![Page 16: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/16.jpg)
![Page 17: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/17.jpg)
???
![Page 18: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/18.jpg)
![Page 19: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/19.jpg)
dem = (n==1);
![Page 20: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/20.jpg)
![Page 21: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/21.jpg)
???
![Page 22: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/22.jpg)
(BT15/A04)....2222 STính:
21 S
12 222 SS
Phân tích theo “chiều ngược”:
23 2222 SS
. . .34 22222 SS
![Page 23: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/23.jpg)
2tS
ts SS 222
ts SS 2222
. . .
st SS
st SS
ts SS 22222
![Page 24: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/24.jpg)
2tS
ts SS 222
ts SS 2222
. . .
st SS
st SS
ts SS 22222
![Page 25: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/25.jpg)
2tS
ts SS 222
ts SS 2222
. . .
st SS
st SS
ts SS 22222
ts SS 2
st SS
ts SS 2
st SS
ts SS 2
2tS
. . .
![Page 26: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/26.jpg)
ts SS 2
st SS
ts SS 2
st SS
ts SS 2
. . .
ts SS 2
st SS
ts SS 2
st SS
ts SS 2
2tS
. . .
2sS
st SS
Thực hiện đến khi nào ?
![Page 27: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/27.jpg)
22sS
222 sS
. . .
st SS
st SS
2222 sS
2sS
st SS
1.414214
1.847759
1.961571
1.990369
ts SS 433546.0
ts SS 113811.0
ts SS 028799.0
![Page 28: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/28.jpg)
![Page 29: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/29.jpg)
![Page 30: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/30.jpg)
Phát biểu for
BThuc
CacPhatBieu
PhatBieuKhac
S
Đ
dem = gtbd
dem += gtbn
![Page 31: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/31.jpg)
Dạng của phát biểu for:
for ( i = gtbd; BieuThuc; i += gtbn)
{
//Cac phat bieu
}
Phát biểu for
![Page 32: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/32.jpg)
Tính tổng: S= 1 + +2 + ... + n
![Page 33: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/33.jpg)
Tính tổng: n
S1
...3
1
2
11
![Page 34: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/34.jpg)
Cấu trúc lặp for dựa trên biến đếm với giá trị khởi đầu, thay đổi biến đếm và biểu thức. Dạng:
for ( gán trị đầu cho biến đếm ; biểu thức ; thay đổi giá trị biến đếm theo bước nhảy
)
{//Cac phat bieu
}
Phát biểu for
![Page 35: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/35.jpg)
Khi phát biểu for thực thi:
B1. Gán trị ban đầu: dem = gtbd
B2. Tính toán biểu thức BThuc.
B3. Nếu BThuc là TRUE (0), thì sang B4.
Nếu BThuc là FALSE (=0), thì sang B6.
B4. Các phát biểu (CacPhatBieu) thân của for thực thi.
B5. Thay đổi giá trị biến đếm (dem += gtbn).
Quay trở về B2.
B6. Kết thúc for, PhatBieuKhac sau for tiếp tục thực thi
Phát biểu for
![Page 36: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/36.jpg)
![Page 37: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/37.jpg)
(1) (2)
(3) (4)
(i>n)
![Page 38: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/38.jpg)
Có bao nhiêu số dương < N chia hết cho 3 ?
![Page 39: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/39.jpg)
(1) (2)
(3) (4)
(in)
![Page 40: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/40.jpg)
![Page 41: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/41.jpg)
Hãy cho biết số nguyên N có bao nhiêu chữ số ?
![Page 42: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/42.jpg)
![Page 43: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/43.jpg)
Không nên !!
![Page 44: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/44.jpg)
![Page 45: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/45.jpg)
Phát biểu do-while
expr
statement1statement2
...
other statements
S
Đ
![Page 46: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/46.jpg)
Phát biểu do-while tính biểu thức sau thực thi các phát
biểu trong phần thân. Dạng:
do
{
//statements
}
while ( expr );
Phát biểu do-while
![Page 47: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/47.jpg)
![Page 48: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/48.jpg)
Khi phát biểu do-while thực thi:
B1. Các phát biểu (statements) thân của do-while thực thi.
B2. Tính toán biểu thức expr.
B3. Nếu expr là TRUE (0), quay trở về B1.
Nếu expr là FALSE (=0), kết thúc do-while.
(Các phát biểu khác sau while tiếp tục thực thi)
Như vây, thân của do-while thực hiện ít nhất 1 lần.
Phát biểu do-while
![Page 49: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/49.jpg)
![Page 50: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/50.jpg)
![Page 51: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/51.jpg)
Nhâp đến đâu cộng đến đó !?!!
![Page 52: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/52.jpg)
Phát biểu break
- Phát biểu break kết thúc cấu trúc lặp gần nhất mà nó
xuất hiện trong đó.
- Kết thúc cấu trúc lặp mà không cần biết kết quả của
expr.
- Được dùng trong trường hợp thoát khỏi vòng lặp mà
không phải kiểm tra tất cả mọi trường hợp.
- Phát biểu break thường xuất hiện cùng với phát biểu
if.
![Page 53: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/53.jpg)
![Page 54: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/54.jpg)
![Page 55: CHƯƠNG 5 CẤU TRÚC ĐIỀU KHIỂN](https://reader035.vdocuments.pub/reader035/viewer/2022070417/56815438550346895dc23b08/html5/thumbnails/55.jpg)