BELAJAR C++ |
----------------------------------------------------------------------------------------------------------------
if
-------------------------------------------------------
#include <iostream.h>#include <conio.h>
#include <stdio.h>
main( )
{
float hrg;
float diskon;
float totalharga;
clrscr( );
puts("");
cout<<" Harga Barang : " ; cin>> hrg;
if(hrg>=150000)
{
diskon = 0.01* hrg ;
totalharga = hrg-diskon;
}
cout<<"\n----------------------------"<<endl;
cout<<" Diskonnya : "<<diskon<<endl;
cout<<" totalharganya: "<<totalharga<<endl;
getch();
}
-------------------------------------------------------------------------------------------------------------------
if - else
----------------------------------------------------------------------------
tiga posisi
----------------------------------
#include <iostream.h>----------------------------------
#include <conio.h>
#include <stdio.h>
main( )
{
float hrg, diskon, totalharga;
clrscr( );
puts("");
cout<<" Harga Barang : " ; cin>> hrg;
if(hrg>=150000)
{
diskon = 0.01* hrg ;
totalharga = hrg-diskon;
}
else if(hrg>=500000)
{
diskon =0.15* hrg;
totalharga = hrg-diskon;
}
else if(hrg>=1000000)
{
diskon =0.20*hrg;
totalharga = hrg-diskon;
}
else
{
diskon = hrg*0;
}
cout<<"\n----------------------------"<<endl;
cout<<" Diskonnya : "<<diskon<<endl;
cout<<" totalharganya: "<<totalharga<<endl;
getch();
}
----------------------------------------------------------------------------------------------------------------
SWITCH
-------------------------------------------------------
#include <iostream.h>#include <conio.h>
#include <stdio.h>
main()
{
int kdhari;
printf("masukkan kode hari [1-7]= ");
scanf("%d",&kdhari);
switch (kdhari)
{
case 1:
printf("senin");
break;
case 2:
printf("selasa");
break;
case 3:
printf("rabu");
break;
case 4:
printf("kamis") ;
break;
case 5:
printf("jumat");
break;
case 6:
printf("sabtu");
break;
case 7:
printf("minggu");
break;
default:
printf("kode tidak ada");
}
getch();
}
Labels:
BAHASA PROGRAM Cpp
Thanks for reading CONTOH PERINTAH IF, IF-ELSE dan SWITCH PADA C++. Please share...!
0 Comment for "CONTOH PERINTAH IF, IF-ELSE dan SWITCH PADA C++"