Thứ Bảy, 31 tháng 1, 2015

Switch / case Day3

                                  Lệnh điều kiện Switch/Case trong C
I.Khái niệm
 -Về cơ bản, lệnh điều kiện "switch/case" tương tự vs "if...else" để biểu diễn biểu thức điều kiện có khả năng xảy ra nhiều kết quả khác nhau.
-Nếu một trong số các câu lệnh trong biểu thức "switch" nào thỏa mãn, máy tính sẽ thực hiện câu lệnh đó.Trong trường hợp không có câu lệnh nào thỏa mãn, máy sẽ thực hiện lệnh cuối cùng
II.Cấu trúc
 -Cơ bản thì cấu trúc lệnh đk switch như sau:
 
  Switch (Biểu thức){
       Case hằng số 1:
               Câu lệnh;
               Break;
       Case hằng số 2:
               Câu lệnh;
               Break;
       Case hằng số 3:
               Câu lệnh;
               Break;
       Default:
               Câu lệnh;
}
 -Lệnh Break; dùng để thoát khỏi Case, trường hợp lệnh thỏa mãn nằm ở giữa thì lệnh này sẽ giúp kết thúc chương trình và đưa ra kết quả thỏa mãn cần tìm.
-Từ khóa Default: có chức năng tương tự Else, khi không có Case nào thỏa mãn, thì máy tính sẽ thực hiện lệnh trong Default
III.Bài tập
**Bài 1**: Declare two variables x and y. Assign values to these variables. Number x should be printed only if it

is less than 2000 or greater than 3000, and number y should be printed only if it is between 100 and 500.

**Lưu đồ:



**Code:

#include <stdio.h> #include <stdlib.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char *argv[]) { int x; int y; printf("Nhap vao gia tri x = "); scanf("%d", &x); printf("Nhap vao gia tri y = "); scanf("%d", &y); if (x < 2000) { printf("\nGia tri thoa man x = %d", x); } else if (x > 3000) { printf("\nGia tri x thoa man = %d", x); } if (100<y<500) { printf("\nGia tri y thoa man = %d", y); } else printf("Error!!"); return 0; }


Lưu ý: Ở bài này chúng ta cần để ý điều kiện để thỏa mãn giá trị x,vì x không thể đồng thời thỏa mãn (x <2000 và x>3000) cho nên ta phải dùng else if cho một trong hai điều kiện


**Bài 2**: Write a program to show your computer’s capabilities. The user types in a letter of the alphabet and your program should display the corresponding language or package available. Some sample input ans output is given below :
                           Input                                  Output
                           A or a                                Ada
                           B or b                                 Basic
                           C or c                                 COBOL
                           D or d                                dBASE III
                           f or F                                  Fortran
                           p or P                                 Pascal
                           v or V                                Visual C++
Using the ‘switch’ statement to choose and display the appropriate message. Use the
default label to display a message if the input does not match any of the above         
letters.

**Lưu đồ:



**Code:

#include <stdio.h> #include <stdlib.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char *argv[]) { char letter; printf("Nhap vao ky tu : "); scanf("%s", &letter); switch(letter){ case 'A': case 'a': printf("\nAda"); break; case 'B': case 'b': printf("\nBasic"); break; case 'C': case 'c': printf("\nCOBOL"); break; case 'D': case 'd': printf("\ndBASE III"); break; case 'F': case 'f': printf("\nFortran"); break; case 'P': case 'p': printf("\nPascal"); break; case 'V': case 'v': printf("\nVisual C++"); break; default: printf("\nKhong tim thay ky tu thoa man!!"); } return 0; }



**Bài 3**: Accept values in three variables and print the highest value.
**Lưu đồ:


Lưu ý: Như trong định nghĩa lưu đồ đã học ở những bài trước, đây chỉ là sơ đồ tư duy cơ bản, có thể hơi khác về quá trình lập lý luận nhưng kết quả vẫn đúng với tư duy ban đầu.

**Code:

#include <stdio.h> #include <stdlib.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char *argv[]) { int a,b,c; printf("nhap vao a = "); scanf("%d", &a); printf("nhap vao b = "); scanf("%d", &b); printf("nhap vao c = "); scanf("%d", &c); if(a > b && a > c) { printf("\nGia tri lon nhat = %d", a); } else if(b > c && b > a) { printf("\nGia tri lon nhat = %d", b); } else if(c > a && c > b) { printf("\nGia tri lon nhat = %d", c); } else printf("Nothing"); return 0; }

Lưu ý: Ở bài này thì lệnh "Else" chỉ là cho đủ cú pháp, còn không có thể có trường hợp xảy ra lệnh đó được, vì trong 3 số có giá trị được gán cho thuộc tập xác định, chắn chắc có MỘT số lớn hơn cả 2 số còn lại.

Không có nhận xét nào:

Đăng nhận xét