Berikut adalah contoh cara menggunakan switch
sebarang pertanyaan boleh diajukan ruangan comment post ini :D
___________________
CARA 1: Guna integer
#include
int main(){
int color;
printf("Enter an integer to choose a color(red=1,green=2,blue=3):\n");
scanf("%d",&color);
switch(color){
case 1: printf("you chose red color\n");
break;
case 2:printf("you chose green color\n");
break;
case 3:printf("you chose blue color\n");
break;
default:printf("you did not choose any color\n");
}
getch();
return 0;
}
________________________
CARA 2: Guna character
#include
char color;
int main(){
printf("Choose a color(r=RED,g=GREEN,b=BLUE):\n");
scanf("%c",&color);
switch(color){
case 'r': printf("you chose red color\n");
break;
case 'g':printf("you chose green color\n");
break;
case 'b':printf("you chose blue color\n");
break;
default:printf("you did not choose any color\n");
}
getch();
}
beza dia satu je.. untuk cara character mestilah gunakan tanda ' manakala int tak perlu.....
No comments:
Post a Comment