Materi bahasan : switch – case, strcpy
o
Switch – case
Penggunaaannya untuk memeriksa data yang bertipe karakter
atau integer
o Struktur switch – case
Switch ( variabel kondisi )
{
Case pilihan-1
:
Perintah;
Break;
Case pilihan-2
:
Perintah;
Break;
Case pilihan-n
:
Perintah;
Break;
Default :
Perintah;
}
Keterangan :
Contoh program :
#include
"iostream.h"
#include "conio.h"
#include "stdio.h"
main()
{
char hari[10];
int kode;
cout<<"Masukkan Kode [1..7] :
";cin>>kode;
switch(kode)
{
case 1:
strcpy(hari,"Minggu");
break;
case 2:
strcpy(hari,"Senin");
break;
case 3:
strcpy(hari,"Selasa");
break;
case 4:
strcpy(hari,"Rabu");
break;
case 5:
strcpy(hari,"Kamis");
break;
case 6:
strcpy(hari,"Jumat");
break;
case 7:
strcpy(hari,"Sabtu");
break;
default:
strcpy(hari,"Salah Kode");
}
cout<<"Nama Hari :
"<<hari;
getch();
}
|
0 komentar:
Posting Komentar