Bài giảng Tin học đại cương A (dành cho khối tự nhiên): Control Structures

25 225 0
Bài giảng Tin học đại cương A (dành cho khối tự nhiên): Control Structures

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Bài giảng Tin học đại cương A (dành cho khối tự nhiên): Control Structures sau đây sẽ trình bày các kiến thức về cấu trúc điều khiển trong ngôn ngữ lập trình C. Đây là tài liệu tham khảo hữu ích cho các bạn đang học Tin học đại cương A khối tự nhiên. Mời các bạn đón đọc.

Control Structures Nguyen Dung Falculty of Information Technology Hue University of Science Content Selection  if statement  switch statement Iteration (loop)  for statement  while statement  … while statement Jump:  break  continue  return  goto if statement Syntax 1st: if ( expression ) {B;} zero exp nonzero B if statement Example: int a = 5, b = 9, max; if (max < b) max = b; max = a; if ( max < b ){ max = b;} printf(“Max is: %d”,max); Max is: if statement Syntax 2nd: if ( exp ) {B1;} else {B2;} zero nonzero exp B2 B1 if statement float a, b, x; printf(“a = ");scanf("%f",&a); printf(“b = ");scanf("%f",&b); if (a == ){ if ( b != ) printf(“Equation hasn’t root.”); else printf(“Equation has countless root.”); } else{ x = -b/a; printf(“Root of equation is x = %f”,x); } a = b = Root of equation is x = -3.00 Excercises Xác định xem số nguyên chẵn hay lẻ Xác định học lực dựa vào điểm trung bình sinh viên, biết: ĐTB [0, 4) [4, 5) [5, 6.5) [6.5, 8) [8, 9) [9, 10] Học lực Kém Yếu Trung bình Khá Giỏi Xuất sắc Xác định số có giá trị lớn hai số thực a, b theo cách: sử dụng lệnh if không sử dụng lệnh if Hiển thị số tự nhiên từ đến dạng chữ exp== exp1 B1; Non-Zero Zero exp== exp2 B2; Non-Zero Zero … Zero exp== expn Bn; Non-Zero Zero B0; switch statement Syntax: expression switch (exp) { case (exp1): B1; break; case (exp2): B2; break; case (expN): BN; break; [default: B0;] } Constant-expression Example: void main() { Nhap so: Sau int x; printf("Nhap so: "); scanf("%d", &x); switch(x){ case 0: printf(“Khong”);break; case 1: printf(“Mot”);break; case 2: printf(“Hai”);break; case 3: printf(“Ba”);break; case 4: printf(“Bon”);break; case 5: printf(“Nam”);break; case 6: printf(“Sau”);break; case 7: printf(“Bay”);break; case 8: printf(“Tam”);break; case 9: printf(“Chin”);break; } getch(); } 10 while statement Syntax: while (exp) { B; } exp B; Non-Zero Zero 11 Example Find “greatest common divisor” of a and b Then find “least common multiple” of a and b Input: a, b Output: GCD(a,b) and LCM(a,b) Method: 𝑎 𝑖𝑓 𝑏 = 𝑎 𝐺𝐶𝐷 𝑎, 𝑏 = 𝐺𝐶𝐷 𝑎 − 𝑏, 𝑏 𝑖𝑓 𝑎 > 𝑏 𝐺𝐶𝐷 𝑎, 𝑏 − 𝑎 𝑖𝑓 𝑎 < 𝑏 𝑎 𝑖𝑓 𝑏 = 𝐺𝐶𝐷 𝑎, 𝑏 = 𝐺𝐶𝐷 𝑏, 𝑏 𝑚𝑜𝑑 𝑎 𝑖𝑓𝑏 ≠ 𝑎𝑏 𝐺𝐶𝐷 𝑎, 𝑏 = 𝐿𝐶𝑀 (𝑎, 𝑏) 12 Implement unsigned int x, y, a, b; printf("Nhap x, y: "); scanf("%d,%d", &x, &y); if (x == && y == 0) printf(“Both a and b are 0\n"); else if (x * y == 0){ printf(“GCD(%d,%d) is: %d\n", x, y, x+y); } else{ unsigned int r = 0; a = x;b = y; while(b!=0){ while(a != b){ r = a % b; if (a > b) a = b; a -= b; b = r; else } b -= a; } printf(“GCD(%d,%d): %d\n", x, y, a); printf(“LCM(%d,%d) is: %d\n",x, y,(x*y)/a); } 13 while statement Syntax: { B; B; } while (exp); Non-Zero exp Zero Different with while statement??? 14 Example Enter x: 6 is even Press ESC to exit Enter x: 7 is odd Press ESC to exit #include #include void main() { int x; char c; do{ printf(“\nEnter x: ”); scanf(“%f”, &x); if (x%2==0){ printf(“%d is even”,x); } else printf(“%d is odd”,x); printf(“\nPress ESC to exit ”); c = getch(); } while (c!=27); } 15 for statement Syntax: for(exp0;exp1;exp2) { B; } exp0; Zero exp1 Non-Zero B; exp2; 16 Example Find the following sum: S = + + + + n = 𝒏 𝒊=𝟏 𝒊 Input: n Output: sum of n number from to n Method: s = 0; For i = to n s = s + i; 17 Implement #include Enter n: #include Sum is: 21 void main() { int n, s=0; printf(“Enter n: "); scanf("%d", &n); for(int i = 1; i

Ngày đăng: 23/11/2017, 17:52

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan