Pert 5 Operasi Penyeleksian Kondisi
Pert
5 Operasi Penyeleksian Kondisi
- Pernyataan IF
BU :
If (kondisi)
{
Pernyataan;
}
Contoh
:
#include
<stdio.h>
#include
<conio.h>
#include
<iostream.h>
main
()
{
double
tot_beli,pot=0,jum_bayar=0;
clrscr();
cout<<”Total
Pembelian Rp. “;cin>>tot_beli;
if
(tot_beli>=50000)
pot=0.2
* tot_beli;
cout<<”Besarnya
Potongan Rp. “<<pot<<endl;
jum_bayar=tot_beli-pot;
cout<<”jumlah
yang harus dibayarkan Rp. “<<jum_bayar;
getch();
}
B.
Pernyataan IF…ELSE
BU :
If
(kondisi)
Pernyataan
1;
Else
Pernyataan
2;
Atau
:
If
(kondisi)
{
Pernyataan
1;
……
;
}
Else
{
Pernyataan
2;
……… ;
}
Contoh
:
#include
<stdio.h>
#include
<conio.h>
#include
<iostream.h>
main
()
{
double
tot_beli,pot=0,jum_bayar=0;
clrscr();
cout<<”Total
Pembelian Rp. “;cin>>tot_beli;
if
(tot_beli>=50000)
pot=0.2
* tot_beli;
else
pot=0.05*tot_beli;
cout<<”Besarnya
Potongan Rp. “<<pot<<endl;
jum_bayar=tot_beli-pot;
cout<<”jumlah
yang harus dibayarkan Rp. “<<jum_bayar;
getch();
}
C.
Pernyataan NESTED IF
BU :
If (kondisi 1)
{
Pernyataan 1;
If (kondisi)
{
Pernyataan;
}
}
Else if (kondisi 2)
{
Pernytaan 2;
If(kondisi)
{
Pernyataan;
}
}
Else
{
Pernyataan 3;
If(kondisi)
{
Pernyataan;
}
}
Contoh
:
#include
<stdio.h>
#include
<conio.h>
#include
<iostream.h>
main
()
{
Float
pt, js=0,km=0,tot=0;
Clrscr();
Cout<<”Pendapatan
Hari ini Rp. “;cin>>pendapatan;
If
(pt>=0 && pt<=200000)
{
Js=10000;
Km=0.1*pt;
}
Else
{
If
(pt<=500000)
{
Js=20000;
Km=0.15*pt;
}
Else
{
Js=30000;
Km=0.2*pt;
}
}
Tot=km+js;
Cout<<”Uang
jasa Rp. “<<js<<endl;
Cout<<”Uang
Komisi Rp. “<<km<<endl;
Cout<<”============================”<<endl;
Cout<<”hasil
total Rp. “<<tot<<endl;
Getch();
}
D.
Pernyataan IF – ELSE Majemuk
BU :
If (kondisi 1)
{
Pernyataan 1;
Pernytaan 2;
}
Else if (kondisi 2)
{
Pernyataan 3;
Pernytaan 4;
}
Else
{
Pernyataan 5;
Pernytaan 6;
}
Contoh
:
#include
<stdio.h>
#include
<conio.h>
#include
<iostream.h>
main
()
{
Float
pt, js=0,km=0,tot=0;
Clrscr();
Cout<<”Pendapatan
Hari ini Rp. “;cin>>pt;
If
(pt>=0 && pt<=200000)
{
Js=10000;
Km=0.1*pt;
}
Else
If (pt<=500000)
{
Js=20000;
Km=0.15*pt;
}
Else
{
Js=30000;
Km=0.2*pt;
}
Tot=km+js;
Cout<<”Uang
jasa Rp. “<<js<<endl;
Cout<<”Uang
Komisi Rp. “<<km<<endl;
Cout<<”============================”<<endl;
Cout<<”hasil
total Rp. “<<tot<<endl;
Getch();
}
Comments