- Switch ...case dùng để biểu diễn cấu trúc điều kiện( cũng như if ...else)
-Cấu Trúc :
switch (expression) {
case constant1:
block of instructions 1
break;
case constant2:
block of instructions 2
break;
.
.
.
default:
default block of instructions
}
Trong đó:
Switch mô tả lại biểu thức, theo sau switch là một biểu thức được đặt trong ngoặc tròn có thể nhận 2 giá trị là hằng số nguyên và hằng kí tự.
Case biểu diễn hằng số và sau đó là các câu lệnh
Kết thúc mỗi case là lệnh Break dùng để thoát khỏi case.
Nếu không có case nào thỏa mãn thì sẽ thực hiện lệnh default.
VD : Thực hiện điều kiện sắp xếp số điểm thỏa mãn :
II. 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.
Bài làm
#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,y;
printf("Nhap X =");
scanf("%d",&x);
printf("Nhap Y=");
scanf("%d",&y);
if(x>2000&&x<3000){printf("\nX= %d Thoa Man",x);
}
else {printf("\nX=%d khong thoa man",x);
}
if(y>100&&y<500){printf("\nY= %d Thoa Man",y);
}
else {printf("\nY= %d Khong thoa man",y);
}
return 0;
}
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++
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.
#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 a;
printf("Input :");
scanf("%c",&a);
switch(a){
case 'A':
printf("Ada");
break;
case'B':
printf(" Basic");
break;
case'C':
printf("COBOL");
break;
case'D':
printf("dBASE III");
break;
case'F':
printf("Fortran");
break;
case'P':
printf("Pascal");
break;
case'V':
printf("Visual C++");
break;
}
return 0;
}
Bài 3 :
#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 A =");
scanf("%d",&a);
printf("Nhap B =");
scanf("%d",&b);
printf("Nhap C =");
scanf("%d",&c);
if(a>b&&a>c){
printf("A= %d la so lon nhat.",a);
}
if(b>a&&b>c){
printf("B= %d la so lon nhat.",b);
}
if(c>a&&c>b){
printf("C= %d la so lon nhat",c);
}
return 0;
}
Kết luận : Qua bài ta học thêm được một cấu trúc điều kiện tương tự if ...else tuy nhiên dù tương tự nó cũng có mặt ưu và nhược vì vậy ta nên sử dụng những cấu trúc nào mà bản thân hoàn thiện nhất
#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 a;
printf("Input :");
scanf("%c",&a);
switch(a){
case 'A':
printf("Ada");
break;
case'B':
printf(" Basic");
break;
case'C':
printf("COBOL");
break;
case'D':
printf("dBASE III");
break;
case'F':
printf("Fortran");
break;
case'P':
printf("Pascal");
break;
case'V':
printf("Visual C++");
break;
}
return 0;
}
Bài 3 :
Accept values in three variables and print the highest value.
Bài làm
#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 A =");
scanf("%d",&a);
printf("Nhap B =");
scanf("%d",&b);
printf("Nhap C =");
scanf("%d",&c);
if(a>b&&a>c){
printf("A= %d la so lon nhat.",a);
}
if(b>a&&b>c){
printf("B= %d la so lon nhat.",b);
}
if(c>a&&c>b){
printf("C= %d la so lon nhat",c);
}
return 0;
}
Kết luận : Qua bài ta học thêm được một cấu trúc điều kiện tương tự if ...else tuy nhiên dù tương tự nó cũng có mặt ưu và nhược vì vậy ta nên sử dụng những cấu trúc nào mà bản thân hoàn thiện nhất




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